2017-01-19 7 views
1

グリッドビューのすべての空のセルをオレンジ色で表示する方法があるのだろうかと思っていました。私のGridViewの列は動的に生成されます。どんな助けもありがとうございます。色GridViewのすべての空のセル

ありがとうございます!

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) 
{ 
    //check if the rowtype is a datarow 
    if (e.Row.RowType == DataControlRowType.DataRow) 
    { 


     //loop all the cells in the row 
     for (int i = 0; i < e.Row.Cells.Count; i++) 
     { 
      int value = 0; 

      //try converting the cell value to an int 
      try 
      { 
       value = Convert.ToInt32(e.Row.Cells[i].Text); 
      } 
      catch 
      { 
      } 

      //check the value and set the background color 
      if (value == "") 
      { 
       e.Row.Cells[i].BackColor = Color.Green; 
      } 
      else 
      { 
       e.Row.Cells[i].BackColor = Color.White; 
      } 
     } 
    } 
} 
+0

OnRowDataBoundイベントが出発点です。ここでは、すべてのセルをループしてその値を確認できます。 –

+0

[こちら](http://stackoverflow.com/questions/31528633/how-to-change-gridview-cell-color-based-on-condition-using-c-sharp/31529889#31529889)をご覧ください。 – jsanalytics

+0

あなたはどうしたらいいか私に正確に教えてもらえますか? –

答えて

3

これを試してください:あなたが今やっているよう

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) 
    { 
     for (int i = 0; i < e.Row.Cells.Count; i++) 
     { 
      if (e.Row.Cells[i].Text == "&nbsp;") 
       e.Row.Cells[i].BackColor = Color.Orange; 
     } 
    } 

enter image description here

+0

ありがとうございました! –

1

あなたは、そのためRowDataBoundイベントを使用することができます。しかし問題がある。 int valueを作成しますが、valueを文字列if (value == "")と比較してみてください。それは動作しません。

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) 
{ 
    //check if the row is a datarow 
    if (e.Row.RowType == DataControlRowType.DataRow) 
    { 
     //cast the row back to a datarowview 
     DataRowView row = e.Row.DataItem as DataRowView; 

     //loop all columns in the row 
     for (int i = 0; i < e.Row.Cells.Count; i++) 
     { 
      //check if the string is null of empty in the source data 
      //(row[i]) instead of e.Row.Cells[i].Text 
      if (string.IsNullOrEmpty(row[i].ToString())) 
      { 
       e.Row.Cells[i].BackColor = Color.Green; 
      } 
      else 
      { 
       e.Row.Cells[i].BackColor = Color.White; 
      } 
     } 
    } 
} 
+0

助けてくれてありがとう! –

関連する問題