2013-02-01 82 views
0

可能重複:
Show message in empty cells in GridView顏色空單元格不是所有的細胞

通過代碼我得到的所有單元格顏色的 我只需要一空。可有人注意我的錯在哪裏(對不起,我是C#的新手)

private void simpleButton1_Click(object sender, System.EventArgs e) 
    { 

     string[] msg = new string[60]; 
     string[] error = new string[400]; 
     for (int i = 0; i < gridView3.RowCount ; i++) 
     { 
      System.Data.DataRow Rows = gridView3.GetDataRow(i); 
      string cellvalue = Rows[0].ToString(); 
      if (cellvalue == "") 
      { 
       msg[0] = "Missing 'First Name'"; 
       error[i] = msg[0] + " - "; 
       gridView3.CustomDrawCell += new RowCellCustomDrawEventHandler(gridView3_CustomDrawCell); 

      } 
     } 
    } 
    private void gridView3_CustomDrawCell_1(object sender, DevExpress.XtraGrid.Views.Base.RowCellCustomDrawEventArgs e) 
    { 
     e.Style.BackColor = Color.LightSteelBlue; 
    } 

順便說一句,這個網格是從Excel

進口

回答

0

嗯,我想你也需要循環的細胞,即

foreach (GridViewRow row in gridView3.Rows) 
{ 
    foreach (GridViewCell cell in row.Cells) 
    { 
    if (cell.Text == "") 
    { 
    //Do stuff here color etc 
    } 
    } 
} 

一旦你有機會獲得Cell對象,你可以設置顏色出現,即(System.Drawing命名空間)

cell.BackColor = Color.Green 
+0

我在第一行得到這個錯誤 foreach語句不能對'int'類型的變量進行操作,因爲'int'不包含'GetEnumerator'的定義,或者它不可訪問 – Nejthe

0

你可以嘗試像 如果是真的,那麼做你的事情。

至於說,你可以通過你只需要註冊一次電網事件,而不是在循環第一