Hi i have a data table and is used like ths to add data todatagrid view (dgv) dt2 is the datatable.
DataGridViewRow row = dgv.SelectedRows[0];
string a = row.Cells[0].Value.ToString();
string b = row.Cells[1].Value.ToString();
dt2.Rows.Add(a, b);
dgvBill.DataSource = dt2;
Now when i am trying to delete from the datagridview the following error is occuring: Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index.
the thing is that on the datagrid i have values so i am not sure why this is happining :/
DataGridViewRow row = dgv.SelectedRows[0];
int a = row.Cells[0].RowIndex;
dgvBill.Rows.RemoveAt(a);
dgvBill.Refresh();
Loading
Amit ChoudharyPosted Mar 15, 2010, 1:28 AM
you should write the delete code inside Gridview delete event. in your code the selected row is not able to retrieved and so getting no index
try following code:
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
customer.Delete(GridView1.DataKeys[e.RowIndex].Values[0].ToString());
FillCustomerInGrid();
}
please mark as answer if it helps you.
Sam HobbsPosted Mar 14, 2010, 2:34 PM