2009-07-03 18 views
22

複数の行と列で構成されるデータグリッドビューがあります。 各行を繰り返し、特定の列の内容を確認したい。 その列に「いいえ」という単語が含まれている場合は、行全体の赤を赤に変更したいとします。 これまでのところいくつかのコードを試してみましたが、確かにうまく機能していません。すべてのセルを繰り返し処理する必要があるのでしょうか?C#DataGridViewをオーバーして行の色を変更します。

CODE:

foreach (DataGridViewRow dgvr in dataGridView1.Rows) 
     { 
      if (dgvr.Cells["FollowedUp"].Value.ToString() == ("No")) 
      { 
       dgvr.DefaultCellStyle.ForeColor = Color.Red; 
      } 
     } 
+1

"機能しません"とは何ですか?行はありませんか?セルが見つかりません? – Colin

答えて

5
public void ColourChange() 
    { 
     DataGridViewCellStyle RedCellStyle = null; 
     RedCellStyle = new DataGridViewCellStyle(); 
     RedCellStyle.ForeColor = Color.Red; 
     DataGridViewCellStyle GreenCellStyle = null; 
     GreenCellStyle = new DataGridViewCellStyle(); 
     GreenCellStyle.ForeColor = Color.Green; 


     foreach (DataGridViewRow dgvr in dataGridView1.Rows) 
     { 
      if (dgvr.Cells["FollowedUp"].Value.ToString().Contains("No")) 
      { 
       dgvr.DefaultCellStyle = RedCellStyle; 
      } 
      if (dgvr.Cells["FollowedUp"].Value.ToString().Contains("Yes")) 
      { 
       dgvr.DefaultCellStyle = GreenCellStyle; 
      } 
     } 
    } 
+10

あなたは[Type x = null; x =新しいタイプ();]。これら2つの行を[Type x = new Type();]にマージする必要があります –

25

フックアップOnRowDataBoundイベント後、行うもの

ASPX(グリッド):

<asp:.... OnRowDataBound="RowDataBound"..../> 

コードの後ろ:

protected void RowDataBound(object sender, GridViewRowEventArgs e) 
    { 
     if (e.Row.RowIndex == -1) 
     { 
      return; 
     } 

     if(e.Row.Cells[YOUR_COLUMN_INDEX].Text=="NO"){ 
      e.Row.BackColor=Color.Red; 
     } 
    } 

FOR WinForms:

hook the **DataBindingComplete** event and do stuff in it: 

    private void dataGridView1_DataBindingComplete(object sender, 
         DataGridViewBindingCompleteEventArgs e) 
    { 
     if (e.ListChangedType != ListChangedType.ItemDeleted) 
     { 
      DataGridViewCellStyle red = dataGridView1.DefaultCellStyle.Clone(); 
      red.BackColor=Color.Red; 

      foreach (DataGridViewRow r in dataGridView1.Rows) 
      { 
       if (r.Cells["FollowedUp"].Value.ToString() 
         .ToUpper().Contains("NO")) 
       { 
        r.DefaultCellStyle = red; 
       } 
      } 
     } 
    } 
+3

申し訳ありません、これはちょうどまっすぐなWinFormsアプリです......... – Goober

+1

おっと!すべてがとてもウェブであり、3ヶ月以来私は自分自身のウェブプロジェクトに深いと思っています。すべての質問がasp.netに関する普通のようです – TheVillageIdiot

+0

誰かがダウン投票して、理由を知らないのですか? – TheVillageIdiot

2

セル値の一部として空白やその他の文字がある可能性はありますか?もしそうなら、Containsメソッドを使ってみてください。

if (dgvr.Cells["FollowedUp"].Value.ToString().Contains("No")) 
0

これは、Winformsのためのソリューションです:

private void HighlightRows() 
{ 
    DataGridViewCellStyle GreenStyle = null; 

    if (this.dgridv.DataSource != null) 
    { 
     RedCellStyle = new DataGridViewCellStyle(); 
     RedCellStyle.BackColor = Color.Red; 

     for (Int32 i = 0; i < this.dgridv.Rows.Count; i++) 
     { 
      if (((DataTable)this.dgridv.DataSource).Rows[i]["col_name"].ToString().ToUpper() == "NO") 
      { 
       this.dgridv.Rows[i].DefaultCellStyle = RedCellStyle; 
       continue; 
      } 
     } 
    } 
} 
+0

例外が発生せず、色も変わりません。なぜ助けが必要なのか分かりません。 –

0

このコードは、私のために正常に動作します:

 

foreach (DataGridViewRow row in dataGridView1.Rows) 
{ 
    if ((string)row.Cells["property_name"].Value == UNKNOWN_PROPERTY_NAME) 
    { 
     row.DefaultCellStyle.BackColor = Color.LightSalmon; 
     row.DefaultCellStyle.SelectionBackColor = Color.Salmon; 
    } 
} 
 
文字列としてキャストではなく、ToStringメソッドを呼び出す以外

私は本当にすべて見るいけませんそれは大文字小文字の区別のバグかもしれません。 CellFormattingイベントを、あなたのDataGridViewに

 
dgvr.Cells["FollowedUp"].Value.ToString().ToUpper() == "NO" 
9

を扱う:使用してみてください

dataGridView1.CellFormatting += new DataGridViewCellFormattingEventHandler(dataGridView1_CellFormatting); 

あなたのイベントハンドラは、次のようになります。あなたが上がらない。このように

private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e) 
{  
    if(dataGridView1.Columns[e.ColumnIndex].Name == "FollowedUp" && e.Value != null && e.Value.ToString() == "No") 
     dataGridView1.Rows[e.RowIndex].DefaultCellStyle.ForeColor = Color.Red; 
} 

行間を '反復'する - 表示/非表示になったときに描画/描画される色を変更するだけですquire formatting)をグリッドに表示します。

+0

このメソッドは少し遅くなりますが、列ヘッダーのクリックの並べ替えで書式設定が消去されることはありません。 –

+0

申し訳ありませんが、私のイベントハンドラコードの置き換えに何か問題があり、以前のコメントを編集できません。私は「たくさん」を「やや遅く」に置き換えます。 –

+0

ハンドラで何をしているかによって、その(パフォーマンス)決定が行われます。最終的には、ハンドラメソッドを使用することは、(あなたの言うように)列の並べ替えによって消去されないことを意味します。しかし、何千もの行を持つグリッドの場合、ユーザが決して見ることのない行を「書式設定」していないことにも注意してください。 – Rostov

0
private void Grd_Cust_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e) 
{ 
    colorCode == 4 ? Color.Yellow : Color.Brown; 
    if (e.RowIndex < 0 || Grd_Cust.Rows[e.RowIndex].Cells["FollowedUp"].Value == DBNull.Value) 
     return; 
    string colorCode = Grd_Cust.Rows[e.RowIndex].Cells["FollowedUp"].Value.ToString(); 
    e.CellStyle.BackColor = colorCode == "NO" ? Color.Red : Grd_Cust.DefaultCellStyle.BackColor; 
} 
+1

ようこそstackoverflowへ。ヒント、 '{}'ボタンを使用してコードスニペットをフォーマットすることができます。 – Leigh

関連する問題