Hallo,
In a DataGridView I would like to increase the value in one of my datafields if a new row is addd.
Index | Description | ItemCode | etc.
233 | First record | 1234545 | etc.
234 | Second record | 1234568 | etc.
| Third record | |
If I add one row to the DataGridView I would like to automatically give the value of 235 to the Index variable of the third record in the new row. ( i.e previous Index value + 1)
Is this possible?
Kind Regards,
Baily
Loading

minakshi24 reputationPosted Dec 21, 2009, 1:51 AM EST
You can use following code
for (int i = 0; i < datagridview1.Rows.Count; i++)
{
datagridview1.Rows[i].Cells[0]=i+1;
}
regards,
Minakshi
Sam HobbsRank #107 · 20.5K reputationPosted Dec 19, 2009, 9:13 PM EST
If the DataGridView is bound to a database, then you can set the identity property of the column in the database table, or an equivalent property.
Otherwise you can calculate the new Index value and supply it in an event handler; either the RowsAdded or the DefaultValuesNeeded event. I am sorry that I am not sure which event would be best; my guess is that the DefaultValuesNeeded event would be the most appropriate.
For determining the new Index value, if the DataGridView is sorted by the Index value, then you simply need to access the last row to get the greatest Index value currently. Or you could process the DataGridView when it is first filled with data and store the maximum Index value as a form member and then increment that value each time another record is added.
theLizardRank #1,079 · 1.8K reputationPosted Dec 19, 2009, 9:37 PM EST
Baily22 reputationPosted Dec 21, 2009, 1:00 PM EST
Regards,
Baily
theLizardRank #1,079 · 1.8K reputationPosted Dec 21, 2009, 3:07 PM EST