110,561
社区成员
发帖
与我相关
我的任务
分享
void dataGridView_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
//在执行下面的之前可以先在这屏蔽掉不想验证的单元格
TextBox TB = new TextBox();//把当前编辑的单元格转换为TextBox
TB.TextChanged += new EventHandler(TB_TextChanged);//用TextBox.TextChanged事件来判断
}
void TB_TextChanged(object sender, EventArgs e)
{
//这里验证
}
TextBox textBox = null;
void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
int colIndex = dataGridView1.CurrentCell.ColumnIndex;
if(colIndex == 0) //0可以替换成你需要验证的列的Index
{
textBox = e.Control as TextBox;
if(textBox != null)
{
//先注销,在注册,可以防止事件重复注册
textBox.Validating -= (textBox_Validating);
textBox.Validating += (textBox_Validating);
}
}
}
void textBox_Validating(object sender, CancelEventArgs e)
{
//to do
}