DataGridVIew control after the data binding, add, insert, delete operation

The beginning is not bound data quickly realized gain, insert, delete operation, but after data binding, these operations will throw an error.

Online information in this area is relatively small, their own way to find a solution, that is, data binding directly operate here to DataTable example.

Click on an event in which the control is bound to DataGridView ContextMenuStrip controls. datatable as a global variable.

1. Add the line

1         private void toolStripMenuItem_AddRow_Click(object sender, EventArgs e)
2         {
3             datatable.Rows.Add();
4 
5             //dataGridView_Barcode.Rows.Add();
6             //datatable.Rows.Add();
7         }

2. Insert a row

 1         private void ToolStripMenuItem_InsertRow_Click(object sender, EventArgs e)
 2         {
 3             int index = dataGridView_Barcode.CurrentCell.RowIndex;
 4 
 5             DataRow dr = datatable.NewRow();
 6             datatable.Rows.InsertAt(dr, index + 1);
 7 
 8             //int index = dataGridView_Barcode.CurrentCell.RowIndex;
 9             //dataGridView_Barcode.Rows.Insert(index + 1, 1);
10         }

new a new datarow actually thought for a long time to find, ha ha.

3. Delete the selected rows

 1         private void ToolStripMenuItem_DeleteRow_Click(object sender, EventArgs e)
 2         {
 3 
 4             int nCounts = dataGridView_Barcode.SelectedRows.Count;
 5             for (int i = nCounts - 1; i >= 0; i--)
 6             {
 7                 datatable.Rows.RemoveAt(dataGridView_Barcode.SelectedRows[i].Index);
 8             }
 9             
10             //int nCounts = dataGridView_Barcode.SelectedRows.Count;
11             //for (int i = nCounts - 1; i >= 0; i--)
12             //{
13             //    dataGridView_Barcode.Rows.Remove(dataGridView_Barcode.SelectedRows[i]);                
14             //}
15             
16         }

I hope to help you! Operational data time comments are not binding.

 

Guess you like

Origin www.cnblogs.com/ligiggy/p/11207478.html