如图所示,C#datagridview,第二列选择了ARRAY则后面4列必须填入内容,不许为空。
如果选择了不是ARRAY,则后面的4列灰显,不可以编辑。
应该用到datagridview哪个事件??
如图所示,C#datagridview,第二列选择了ARRAY则后面4列必须填入内容,不许为空。
如果选择了不是ARRAY,则后面的4列灰显,不可以编辑。
应该用到datagridview哪个事件??
添加CellEndEdit事件,用下面的代码就行
private delegate void setState();
private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
DataGridViewCell cur = dataGridView1.CurrentCell;
bool setFucos = false;
if (e.ColumnIndex == 1)
{
bool readOnly = cur.Value.ToString() != "ARRAY";
for (int i = 2; i < 6; i++)
{
dataGridView1.CurrentRow.Cells[i].ReadOnly = readOnly;
dataGridView1.CurrentRow.Cells[i].Style.BackColor = readOnly ? Color.Gray : Color.White;
if (!readOnly)
{ //设置下一个单元格获取焦点
setFucos = true;
cur = dataGridView1.CurrentRow.Cells[2];
}
}
}
if (e.ColumnIndex > 1 && e.ColumnIndex < 6)
{
if (cur.Value == null || cur.Value.ToString() == "")
{
MessageBox.Show("不能为空!");
setFucos = true;
}
}
if (setFucos)
{//重置焦点
System.Timers.Timer t = new System.Timers.Timer(100);
t.Elapsed += (tsender, te) =>
{
t.Dispose();
this.Invoke(new setState(() =>
{
dataGridView1.CurrentCell = dataGridView1.CurrentRow.Cells[2];
dataGridView1.BeginEdit(true);
}));
};
t.Enabled = true;
}
}