2010-07-01 63 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 
} 
事件