Reputation: 83
I know there are hundreds of questions about this and believe me, i've read them all but nothing seems to work for me.
I have a win form with a datagrid that i am populating like this:
private void button1_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection("user id=testuser12;" +
"password=Reporting11#;Data Source=SERVER;" +
"Initial Catalog=Partner_database; " +
"connection timeout=30");
con.Open();
SqlCommand command = new SqlCommand("select * from [dbo].[Test_table]", con);
sda = new SqlDataAdapter(command);
sda.AcceptChangesDuringFill = true;
sda.AcceptChangesDuringUpdate = true;
set.Clear();
cmdBuilder = new SqlCommandBuilder(sda); // using this to update the database
sda.Fill(set,"cucu");
dataGridView1.DataSource = set;
dataGridView1.DataMember = "cucu";
dataGridView1.CellValueChanged -= dataGridView1_CellValueChanged;//line added after solution given
dataGridView1.CellValueChanged += dataGridView1_CellValueChanged;//line added after solution given
}
Then i am trying to catch the CellValueChange event to run something like:
private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
//if (dataGridView1.Columns[e.ColumnIndex].Name == "Age") // deleted this line after solution was given
//if (e.ColumnIndex > -1) // i have also tried with this one but does not work
{
MessageBox.Show("It works!");
}
}
Cannot seem to make it work. I am modifying the cell and also change the focus to another cell to try and trigger the event. I'm going crazy. What am i missing?
Thank you. Danut
EDIT 1: based on replies so far.
I tried deleting the IF clause in the CellValueChanged event just to test if the event is working. It did not trigger.
I tried with CellEndEdit event, still did not trigger.
Is there anything i need to do when initializing the Grid? Or something to do to make the event trigger?
EDIT 2:
Now the event is triggered with this sollution: "You need to set up the event handler on the DataGridView. Go you the events panel in the properties tab and double click on the required event type, this will create and wire up you event handler... Alternatively you can do this manually by writing: dataGridView1.CellValueChanged += dataGridView1_CellValueChanged;"
But it gets triggered when the grid is populated with data and i get the box many times.
EDIT 3: SOLUTION GIVEN - solved
I have added 2 lines of code(see them in my code above). I have also reset the event in the datagrid properties and only used those 2 lines to condition the trigger.
Thank you all for your support.
Upvotes: 1
Views: 3091
Reputation: 23831
After the database is updated from SQL add the following
dataGridView1.CommitEdit(DataGridViewDataErrorContexts.Commit);
This should enable the DataGridView
to accept changes.
I hope this helps.
This did not work. Okay so, you need to set up the event handler on the DataGridView
. Go you the events panel in the properties tab and double click on the required event type, this will create and wire up you event handler... Alternatively you can do this manually by writing:
dataGridView1.CellValueChanged -= dataGridView1_CellValueChanged;
dataGridView1.CellValueChanged += dataGridView1_CellValueChanged;
The first ensures you do not 'stack' the handlers.
Upvotes: 2
Reputation: 2117
Try this one
if( dataGridView1.Columns[e.ColumnIndex].Name.ToLower().Trim().Equals("age") )
Upvotes: 0