110,566
社区成员
发帖
与我相关
我的任务
分享
private void dgv_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
{
if (e.ColumnIndex == 0) return;
DataGridViewRow dataGVrow = this.dgv.Rows[e.RowIndex];
string oneProjectName = string.Empty;
string twoProjectName = string.Empty;
if (this.dgvProjectNameSettingLevelTwo.BeginEdit(true))
{
oneProjectName = dataGVrow.Cells["一级名称"].EditedFormattedValue.ToString();
twoProjectName = dataGVrow.Cells["二级名称"].EditedFormattedValue.ToString();
}
else
{
oneProjectName = dataGVrow.Cells["一级名称"].Value.ToString();
twoProjectName = dataGVrow.Cells["二级名称"].Value.ToString();
}
DataRow dr = (dataGVrow.DataBoundItem as DataRowView).Row;
e.Cancel = false;
if (e.ColumnIndex == 2)
{
if (string.IsNullOrEmpty(oneProjectName))
{
MessageBox.Show("一级名称为空。", "错误", MessageBoxButtons.OK, MessageBoxIcon.Hand);
e.Cancel = true;
this.dgv.CurrentCell = dataGVrow.Cells["一级名称"];
this.dgv.BeginEdit(true);
if (this.dgvProjectNameSettingLevelTwo.EditingControl is TextBox)
((TextBox)dgvProjectNameSettingLevelTwo.EditingControl).SelectAll();
return;
}
}
if (e.ColumnIndex == 3)
{
if (string.IsNullOrEmpty(twoProjectName))
{
MessageBox.Show("二级名称为空。", "错误", MessageBoxButtons.OK, MessageBoxIcon.Hand);
e.Cancel = true;
this.dgv.CurrentCell = dataGVrow.Cells["二级工程名称"];
this.dgv.BeginEdit(true);
if (this.dgvProjectNameSettingLevelTwo.EditingControl is TextBox)
((TextBox)dgvProjectNameSettingLevelTwo.EditingControl).SelectAll();
return;
}
}
}
private void dgv_RowValidating(object sender, DataGridViewCellCancelEventArgs e)
{
if (e.ColumnIndex != 2 && e.ColumnIndex != 3)
return;
DataGridViewRow dataGVrow = this.dgv.Rows[e.RowIndex];
string oneProjectName = string.IsNullOrEmpty(dataGVrow.Cells["一级名称"].EditedFormattedValue.ToString()) != true ? dataGVrow.Cells["一级名称"].EditedFormattedValue.ToString() : dataGVrow.Cells["一级名称"].Value.ToString();
string twoProjectName = string.IsNullOrEmpty(dataGVrow.Cells["二级名称"].EditedFormattedValue.ToString()) != true ? dataGVrow.Cells["二级名称"].EditedFormattedValue.ToString() : dataGVrow.Cells["二级名称"].Value.ToString();
DataRow dr = (dataGVrow.DataBoundItem as DataRowView).Row;
if (string.IsNullOrEmpty(oneProjectName))
{
MessageBox.Show("一级名称为空。", "错误", MessageBoxButtons.OK, MessageBoxIcon.Hand);
e.Cancel = true;
this.dgv.CurrentCell = dataGVrow.Cells["一级名称"];
this.dgv.BeginEdit(true);
if (this.dgv.EditingControl is TextBox)
((TextBox)dgv.EditingControl).SelectAll();
return;
}
if (string.IsNullOrEmpty(twoProjectName))
{
MessageBox.Show("二级名称为空。", "错误", MessageBoxButtons.OK, MessageBoxIcon.Hand);
e.Cancel = true;
this.dgv.CurrentCell = dataGVrow.Cells["二级名称"];
this.dgv.BeginEdit(true);
if (this.dgv.EditingControl is TextBox)
((TextBox)dgv.EditingControl).SelectAll();
return;
}
}
}
使用RowValidating 事件,切换行的时候再次验证,像单行多个单元格验证,要控制好 this.dgv.BeginEdit(true)控制, BeginEdit控制不好 会触发CellValidating事件的死循环。