问题很多,都是围绕着这个控件的。1.主要的问题是,我想检验控件中某个单元格是否为空,想用 if(dataGridView1[0][0] == null)这样的语句测试,可是好像如果不填东西,单元格中这个对象是不存在的,不填就会报错。请问如何修改?2.我做的一个数据录入和查询工具,进入数据库的有“产品名称”“型号”“单位”“数量”“单价”“金额”,其中“型号”“单位”是可以不填写的。问题来了,数据进数据库时,如何判断这项数据是为空的?问题还是回到了问题1上。3.关于DataGridView控件。我看了看这个控件的单元格的属性(dataGridView1[0][0].xxx),发现没有能让一个单元格变色的属性。能否在用户输入之后立刻变色呢?
int rows = 2, cells = 1;
if (rows < dataGridView1.Rows.Count)
{
if (cells < dataGridView1.Rows[rows].Cells.Count)
{
if (dataGridView1.Rows[rows].Cells[cells].Value != null)
{
MessageBox.Show("为空");
}
}
}问题1和问题好像不一样吧,问题2你遍历每行每列判断下就行了啊,
private void dataGridView2_CellLeave(object sender, DataGridViewCellEventArgs e)
{
if (dataGridView2.Rows[e.RowIndex].Cells[e.ColumnIndex] != null)
{
dataGridView2.Rows[e.RowIndex].Cells[e.ColumnIndex].Style.BackColor = Color.Yellow;
}
}