[英]How to catch if cell Value got changed in DataGridView as sender
I haven't found something that matches my problem so I ask it here. 我没有找到适合我的问题的东西,所以我在这里问。 I have some code which belongs to a Textbox:
我有一些属于文本框的代码:
if ((sender as TextBox).Text == form1.filterType())
{
//Do something
}
This comes from the TextBox TextChanged event. 这来自TextBox TextChanged事件。 So when the TextChanged Event gets fired it calls a method which has the if-construct above and recognizes that the textchanged event came from the textbox.
因此,当触发TextChanged事件时,它将调用上面具有if-construct的方法,并识别出textchanged事件来自文本框。
Now I want exactly the same just when someone writes into a cell in a DataGridView (not when it's just clicked - when the content changes). 现在,我希望有人在DataGridView中的单元格中写入内容时完全相同(而不是在单击时-内容更改时)。
How to do this correctly and which event fires when the content changes in a cell without leaving the cell? 如何正确执行此操作,以及在单元格中的内容发生更改而没有离开单元格时会触发哪个事件?
I have found a solution for this: 我已经找到了解决方案:
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");
}
Now it fires everytime the value in the cell gets changed - it behaves like a textbox. 现在,每次更改单元格中的值时都会触发-它的行为就像一个文本框。
Now - I still need a solution for the "if-construct" above. 现在-我仍然需要上述“ if-construct”的解决方案。 How to do this exactly?
究竟该怎么做? I've casted the cell into a textbox but now?
我已经将单元格投射到文本框中,但是现在? Does this still comes from the dataGridview?
这是否仍然来自dataGridview?
Once I had a similar problem an solved it this way (think there are better ones but it worked) 一旦我遇到了类似的问题,就可以通过这种方式解决(认为有更好的问题,但它确实有效)
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
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.