.Net DataTable Exercise

Every now and then I need to retrieve some data and show it in a grid... uh who am I kidding, I have to do this ALL the time. Well, here is a simple way to filter your data quick and easy.
/* CODE */
GridFieldDAO dao = new GridFieldDAO();
DataTable dt = dao.getDT();
DataRow[] drs = dt.Select("(detailID = 1) AND (detailTypeID = 2)");
DataTable dt2 = dt.Clone();
foreach (DataRow d in drs)
{
dt2.ImportRow(d);
}
myGrid.DataSource = dt2;
myGrid.DataBind();
/* CODE */

The real magic here is how the DataTable Select() method allows you to get subsets of data for display. Most people filter based off changing paramaters and reloading values from the database. If you want to just display filtering for a whole set, this is easier and more efficent.

Enjoy!

Comments

Popular posts from this blog

It is on like Donkey Kong!

Open Letter to Microsoft

Writing a Video Game