views:

82

answers:

1

I'm using a DataGridView with C#.NET. When a user is editing a column, I need another column in the same grid to change with every keystroke/change. How/where do I insert my own code for this type of event?

A: 

What you're trying to achieve is not possible without a bit of work. By default the DataGridView class does not provide a CellChanging style event. Instead it provides book ended events in the form of CellBeginEdit and CellEndEdit.

Part of the reason is likely due to the varying ways in which a cell can be editted. Having a CellChanging would make sense for a text style cell but wouldn't make as much sense for say a Button style cell.

You could easily implement a solution though which propagated the value once it was completely entered via the above said events.

The only way I can see to implement it for every keystroke would be to

  1. Handle the above events
  2. Figure out what the runtime type of DataGridView.EditingControl is and find a way to hook into every single change for every type of cell
  3. Propagate the changes on every edit

Even then I think you still may run into issues because I'm not sure if DataGridView is designed to have cell values changed while a different one is being actively edited.

JaredPar
Is there any reason that you can think of that nothing occurs when I do use the following code?private void dataGridView1_CellEndEdit(object sender, DataGridViewCellCancelEventArgs e) { dataGridView1[3, e.RowIndex].Value = "test"; dataGridView1[0, 0].Value = "another test"; }Essentially, neither 0,0 nor 0,X are affected when I click a cell, edit the text, and press enter.
Brandon