What I'm currently trying to do is allow a user to add a row to my datagridview. I have AllowUserToAddRows set to true. And the empty now does appear at the bottom of my data. The datagridview has a data source set to a datatable. The code currently allows a user to add the row properly and the data is making it into the datatable. So what I want to do know is auto fill in an ID column as soon as the user starts editing the datagridview. I'm just having trouble knowing which event to handle to do this. Optimally I want to handle dataTable.tableNewRow, but that event is never throw even though the user is adding a new row to the table. So I've been trying to use datagridview.CellValueChanged and then check if the cell being edited is part a new row, but haven't found much success. I also tried ddatagridview.NewRowsAdded, but that event is thrown when a user just clicks on the empty column on the bottom and hasn't even made an edit yet. The Id column that I'm auto filling is supposed to be one more than the larges existing ID in that column.
here is what I have so far:
Expand|Select|Wrap|Line Numbers
- private void EditMade(object sender, DataGridViewCellEventArgs e)
- {
- if (e.RowIndex == dataGridView1.Rows.Count-2)
- {
- double largestID=FindLargestID();
- dataGridView1.CellValueChanged -= new DataGridViewCellEventHandler(EditMade);
- dataGridView1.Rows[dataGridView1.Rows.Count - 2].Cells[sortedColumn].Value = largestID + 1;
- dataGridView1.CellValueChanged += new DataGridViewCellEventHandler(EditMade);
- }
- }
Thanks for the help!