Question Event for column to change data in cell in datagridview

divjoy

Well-known member
Joined
Aug 25, 2013
Messages
159
Programming Experience
1-3
Hi, I'm using vb.net 2010 express with sql server 2008r

I have a datagridview with multiple columns and rows on a form.

I am trying to populate a particular cell depending on what is entered into another cell for each row.

In other words...
When I enter a date in column1, I want to then calculate another date based on that date (say add 12 months) and enter that new date into column6 in the same row, but only if the column 6 cell is already empty (or Null)

Does that make sense?

The calculation is easy enough but I am not sure which event I should use to do this.

I only want it to happen when the user either enters a new date in column1 or changes the date in column1.
 
Handle the CellValueChanged event of the grid. The event gives you the column and row index of the cell whose value has changed. You can first check that the changed occurred in the appropriate column. You can then get the row and check the value in the other column to see if you need to update it. If you do need to update that cell, perform your calculation and update the Value of that cell.
 
Hi,

That's great thanks...

But how do I stop the event happening when the from loads.

I only want the event to happen when the user adds a new row or changes an existing row manually.

Thanks...
 
You don't stop events being raised. You just decide at the time whether to act on it or not. If the form is loading then don't act and if it's already loaded then do.
 
You don't stop events being raised. You just decide at the time whether to act on it or not. If the form is loading then don't act and if it's already loaded then do.

Yes but how exatly, there quite a lot of different examples online but no definitive one.
 
Hi,

There are two ways that quickly come to mind:-

1) Create a Private Class Level Boolean variable and then Set this variable to True in the Form.Shown event. You then test this variable when executing your code in the CellValueChanged event of the DataGridView.

2) Remove the actual Event Handler for the CellValueChanged Event in your code and then add the Handler dynamically when the Form has completed loading in the Form.Shown event.

Hope that helps.

Cheers,

Ian
 
Hi Ian,
Thanks for your reply...

Thats what was quoted online alright,


In terms of code efficiency which one would be best....adding handlers or setting flags...

Thank you...
 
Hi,

There are two ways that quickly come to mind:-

1) Create a Private Class Level Boolean variable and then Set this variable to True in the Form.Shown event. You then test this variable when executing your code in the CellValueChanged event of the DataGridView.

2) Remove the actual Event Handler for the CellValueChanged Event in your code and then add the Handler dynamically when the Form has completed loading in the Form.Shown event.

Hope that helps.

Cheers,

Ian

Hi Ian,
Thanks for your reply...

Thats what was quoted online alright,


In terms of code efficiency which one would be best....adding handlers or setting flags...

Thank you...
I would tend to use the first option unless you have a large number of events that are affected. If you do have a lot of events affected then you would have to put an If statement in each handler, which might make the second option more attractive.
 
Back
Top