2010-07-01 62 views
2

我想爲DataGridView的某些單元格爲空或空時編寫一個條件。 例如,如果單元[1]不爲空或空,則必須運行一個方法,並且... 我以某種方式編寫它,但其中一些不起作用,其中一個工作,但其中一個不起作用解決方案爲我的問題。就像你現在,DataGridView中的空和空是不同的。 此外,我的DataGridView尚未綁定到數據庫。 我怎樣才能以最好的方式做到這一點? 此致敬禮。在DataGridView中處理空單元格

回答

5

DataGridViewCell對象具有「Value」方法,該方法將callvalue作爲對象返回,可以將該值轉換爲字符串,然後將該字符串檢查爲null或空。

string val = this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value as string; 
if(string.isNullorEmpty(val) ==false) 
{ 
    // your method run. 
} 
0

單元格值是一個對象。空單元格值爲空。

DataTable/DataView在Empty時使用DbNull.Value。

字符串以長度= 0是的String.Empty

您需要驗證三個選項。

0

如果你的列和行佈局是固定的,那麼你可以調用你的函數在細胞驗證數據網格

void dataGridView2_CellValidating(object sender, DataGridViewCellValidatingEventArgs e) 
     { 
      e.Cancel = false; 
      if (e.RowIndex == 1 && e.ColumnIndex == 0) 
      { 
       if(string.IsNullorEmpty(e.FormattedValue.ToString()) 
        // method call 
      } 
     } 
0
//just replace from Value to FormattedValue like that 
string val = dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].FormattedValue as string; 
if(string.isNullorEmpty(val)) 
{ 
    // cell is empty 
} 
else 
{ 
// cell is not empty 
} 
事件