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
minakshiPosted Dec 21, 2009, 6:51 AM
You can use following code
for (int i = 0; i < datagridview1.Rows.Count; i++)
{
datagridview1.Rows[i].Cells[0]=i+1;
}
regards,
Minakshi
theLizardPosted Dec 21, 2009, 8:07 PM
BailyPosted Dec 21, 2009, 6:00 PM
Regards,
Baily
theLizardPosted Dec 20, 2009, 2:37 AM
Sam HobbsPosted Dec 20, 2009, 2:13 AM
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.