如何捕捉单元格值是否在DataGridView中作为发件人更改
本文关键字:单元格 何捕捉 是否 DataGridView | 更新日期: 2023-09-27 17:59:57
我没有找到与我的问题相匹配的东西,所以我在这里问它。我有一些属于文本框的代码:
if ((sender as TextBox).Text == form1.filterType())
{
//Do something
}
这来自TextBox TextChanged事件。因此,当TextChanged事件被触发时,它会调用一个方法,该方法具有上面的if构造,并识别出TextChanged事件来自文本框。
现在,当有人在DataGridView中的单元格中写入时(而不是当它刚刚被点击时——当内容发生变化时),我希望完全相同。
如何正确执行此操作?当内容在不离开单元格的情况下在单元格中发生更改时,会触发哪个事件?
我找到了一个解决方案:
private void Form1_Load(object sender, EventArgs e)
{
this.dataGridView1.EditingControlShowing += new DataGridViewEditingControlShowingEventHandler(dataGridView1_EditingControlShowing);
}
void dataGridView1_EditingControlShowing(object sender,
DataGridViewEditingControlShowingEventArgs e)
{
if (dataGridView1.CurrentCell.ColumnIndex == 0)
{
TextBox tb = (TextBox)e.Control;
//"unwire" the event before hooking it up ensures the event handler gets called only once
tb.TextChanged -= new EventHandler(tb_TextChanged);
tb.TextChanged += new EventHandler(tb_TextChanged);
}
}
void tb_TextChanged(object sender, EventArgs e)
{
MessageBox.Show("changed");
}
现在,每当单元格中的值发生更改时,它就会触发——它的行为就像一个文本框。
现在,我仍然需要上面的"if构造"的解决方案。具体如何做到这一点?我已经把手机投进了一个文本框,但现在呢?这仍然来自dataGridview吗?
有一次我遇到了类似的问题,就用这种方式解决了(认为有更好的,但它有效)
private void DataGridView1_onFocus ( Object sender, EventArgs e)
{
DataGridView1.onKeyPress+=DataGridView1_onKeyStroke;
}
private void DataGridView1_onFocusLost( Object sender, EventArgs e)
{
DataGridView1.onKeyPress-=DataGridView1_onKeyStroke;
}
private void DataGridView1_onKeyStroke( Object sender , EventArgs e)
{
//Do your thing
}