Search a DataRow from DataTable1 into DataTable2

Tip

The situation is something like this. A GridView is getting populated at the PageLoad event. Now one of the Column of the Grid contains the CheckBox to represent the True/False state for that Row. There are 2 tables in the SQL Database. Now, if a DataRow is present in both the tables then, that Row’s checkbox should be marked in the GridView. The GridView Columns which contains the checkbox is being added at the runtime i.e, that column is not present in the SQL DataTable.
The question is how can I search a single DataRow of a DataTable against all the DataRows of another DataTable. Look at the code below


DataTable dt = null;
DataTable dtParent = null;

dt = GetList(); // Get the selected List
dtParent = GetNameList(); //Get the Entire List

// Mark a Column(s) as Primary, which will be searched for the given value.
dtParent.PrimaryKey = new DataColumn[] { dtParent.Columns["ID"] };
foreach (DataRow dr in dt.Rows)
{
	//If that Row is Present in dtParent then, mark the row's checkbox as checked.
	dtParent.Rows.Find(dr["ID"])["chkCheckBox"] = true; 
}
gvStudentDetails.DataSource = dtParent;
gvStudentDetails.DataBind();

Happy Coding …
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s