Deleting A Row from a DataTable based on ID column

rito1
rito1 used Ask the Experts™
on
Hi All,

I have a DataTable stored within a Session variable like so..

        DataTable myDt = new DataTable();
        myDt = createTempDatesTable();
        Session["TempDataTable"] = myDt;

The DataTable has a column called 'ID' which stored string values.

Please could anyone show me how to delete a row from my DataTable based on a value within the ID column.?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Top Expert 2010
Commented:

If ID column is the primary column, then you can use like:

DataRow foundRow = myDt.Rows.Find("idValue");
foundRow.Delete();
Top Expert 2010

Commented:
D columns in not primary key, then you can use like:

string id = "123";
DataRow[] foundRows = myDt.Select(string.Format("ID Like '{0}%'", id));
foreach(DataRow row in foundRows) // if there is only one for the id, then foundRows will have one row only
  row.Delete();

Author

Commented:
Excellent, thanks

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial