简体   繁体   中英

DataGridView and the CellEndEdit Event

I have a DataGridView , and would like to hook into the CellEndEdit event. I've been able to successfully hook into the CellContentClick event, but am having issues with CellEndEdit .

I added the following code to my Form1.cs file:

private void dataGridView1_CellEndEdit(object sender, DataGridViewCellCancelEventArgs e)
        {
            dataGridView1[0, 0].Value = "Changed";
        }

With that code, nothing happens when I am done editing a cell. Is there anything else that I need to do to successfully hook into this event? I see that CellContentClick has a

this.dataGridView1.CellContentClick += new System.Windows.Forms.DataGridViewCellEventHandler(this.dataGridView1_CellContentClick);

line of code in the Form1.Designer.cs file, but I tried to mimic this for CellEndEdit , and received a compile error

(No overload for 'dataGridView1_CellEndEdit' matches delegate 'System.Windows.Forms.DataGridViewCellEventHandler')

You could implement this yourself.

In your constructor you could have a HookEvents() method which wires up such events.

Or, within the form designer, click the gridview to select it, go to the properties window and click the yellow thunderbolt to find a list of events. Then, scroll down and find the CellEndEdit event and double click it - this will wire up the event for you.

To wire it up yourself, it may look like:

class A : Form
{
  public A()
  {
    Initialize();
    HookEvents();
  }

  private void HookEvents()
  {
    dataGridView1.CellEndEdit += dataGridView1_CellEndEdit;
  }
}

I doubt very much that your solution would work.

It's not a matter of where you place the subscription, is how you do it.

Brandon, you are declaring an EventHandler, that is the function responsible of doing what you want to do in case of that event "dataGridView1_CellEndEdit" but you are not subscribing to the event. Also in your function you are passing the wrong parameters.

The easy solution is either subscribe from the designer window or by code doing this:

write "dataGridView1.CellEndEdit +=" and then press the TAB buton twice. That shoud create the code for subscription to the event and the correct delegate to handle it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM