Delete one row from Data Grid

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

On click of the cmdRemove button, I want the selected row in a data grid to
be removed. Here is my attempt, but code generates errors.

private void cmdRemove_Click(object sender, System.EventArgs
e)
{
DataTable dt = dgPrivileges.DataSource as DataTable;
dt.Rows.Remove(dgPrivileges.CurrentRowIndex);
}
 
Mike,

You might want to try the RemoveAt method on the Rows class, as you want
to pass an index. The Remove method takes a data row, not an integer (which
is what the CurrentRowIndex returns).

Hope this helps.
 
Nicholas said:
Mike,

You might want to try the RemoveAt method on the Rows class, as you want
to pass an index. The Remove method takes a data row, not an integer (which
is what the CurrentRowIndex returns).

Hope this helps.
Yes, but if the user has sorted by clicking a column header, or if the
DataView being used by the grid is ordered (or filtered) differently
from the underlying DataTable, then you'll have trouble.

The best thing to do in this case is to get a reference to the
CurrencyManager for the DataGrid. Try this article:

http://www.codeproject.com/cs/miscctrl/DBGridCurrentRow.asp

HTH,
M.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top