Thursday, January 24, 2013

Accessing DataTable Cell Value in C#.NET


Following DataTable contains four columns.

DataTable dtRecordDetails = new DataTable();
dtRecordDetails.Columns.Add("hID", typeof(int));
dtRecordDetails.Columns.Add("Revision", typeof(string));
dtRecordDetails.Columns.Add("DocHeaderType", typeof(string));
dtRecordDetails.Columns.Add("ConnectionString", typeof(string));

Let's add some rows to the DataTable

dtRecordDetails.Rows.Add(1, A, Doc1, Conn1);
dtRecordDetails.Rows.Add(2, B, Doc2, Conn2);
dtRecordDetails.Rows.Add(3, C, Doc3, Conn3);
dtRecordDetails.Rows.Add(4, D, Doc4, Conn4);

Above code will add four rows. Let's say we need to get "Revision" column second rows cell value, which is "B".

Below code will return the value "B".

string CellValues = dtRecordDetails.Rows[1]["Revision"].ToString();

No comments:
Write comments
Recommended Posts × +