Merging Datasets
Posted on November 15, 2008
Filed Under Programming | Leave a Comment
| If you've enjoyed reading this post then please subscribe to my Full Text RSS Feed. |
This is an example that shows how to use the merge function in DataSets before binding the final DataSet to a GridView.
I have two database query. One to retrieve records of engineers who worked on Project A this week and the second query retrieves records of engineers who were on standby this week. Each query returns the name of the engineer and the no. of hours they worked on a task this week.
I create two instances of DataSets.
DataSet ed = new DataSet();
DataSet noed = new DataSet();
The query result is returned to DataSet ed and noed using the following two lines of code:
ed = cn.GetEngineers;
noed = cn.GetEngineersNoCalls;
Then noed is merged into ed DataSet:
ed.Merge(noed);
Since both DataSets are not sorted after merging and I want to display the list of engineers by name in ascending order, I can achieve the sort order using the following lines of code.
DataTable currentTable = new DataTable();
currentTable.Merge(ed.Table[0]);
currentTable.DefaultView.Sort = string.Format(Sort_Format, Sort_Column_Key, Sort_Direction);
merged.Tables.Add(currentTable.DefaultView.ToTable());
* Sort_Column_Key, Sort_Direction and Sort_Format are defined as const. See full code below.
Finally you bind the DataSet to a GridView.
Sphere: Related Content
Other Related Posts:
If you've enjoyed reading this post then why not subscribe to received updates by email.
Email This Post
Trackback This Post
Comments
Leave a Reply









