0

How do I add the event? On default after adding the datagrid to the form, it didnt have the rowsadded event. Then I added this in

private void dataGridView1_RowsAdded(object sender, DataGridViewCellEventArgs e)
{
    MessageBox.Show("Row added", "Error!");
}

Which was able to run, and when I was adding data to the grid, this didn't trigger. Am I missing a step?

4

2 に答える 2

2

From Code Behind

after InitializeComponent(); add

dataGridView1.RowsAdded += dataGridView1_RowsAdded;

event should change as

private void dataGridView1_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e)
{
    MessageBox.Show("Row added", "Error!");
}

KB Shortcut

public MyForm()
{
    InitializeComponent()
    dataGridView1.RowsAdded += (press [tab][tab] now)
}

From designer

You can do this from designer view. go to properties of the grid view and select events tab. find RowsAdded event and double click on it. it will generate event handler and event for you.

Microsoft Visual Studio and C#: How to Visually Add Events to Controls?

于 2012-06-27T04:04:07.887 に答える
1

Did you add code to your *.designer.cs file?

this.dataGridView1.RowsAdded += new System.Windows.Forms.DataGridViewRowsAddedEventHandler(this.dataGridView1_RowsAdded);

The easiest way for you to add an event is to:

  1. Go to the properties window for your control.
  2. Then click on the little "Lightning" sign tab at the top of the properties window.
  3. Then scoll down to the event you want for that control and double click it.

This will create all the code for you in the designer and in your cs file.

于 2012-06-27T04:01:33.227 に答える