Wednesday, December 26, 2012

Select Single Row from a DataTable

In C#.NET DataTable you can perform many operations. If you want to select the top most record from a DataTable with collection of DataRows, you can use following methods.

DataTable dtRecords = new DataTable();
DataRow DR= dtRecords.Columns["YourColumn"][0];

Above method outputs the top most record of the DataTable where the column name of your DataTable equals to "YourColumn"

DataRow DR = dtRecords.AsEnumerable().Where(r => r.Field<string>("YourColumn") == "Test").AsDataView().ToTable().Rows[0];

In above method, it will select only the records where "YourColumn" records with "Test" and then select the top most record from that updated DataTable.

Once you have retrieved the DataRow now you are eligible to get the values from a specified cell.

string name= DR["YourColumn"].ToString();

No comments:
Write comments
Recommended Posts × +