Sunday, February 17, 2013

More Microsoft Enterprise Libraray Stuff...

Now, let's say you have a DataSet of data that you just queried, but you want to sort it two different ways, but you don't want to re-query the data.  This is a situation where the DataView object can come in handy.  First, let's query some data:


MyDatabase lMyDatabase = new MyDatabase();

string lsQuery = "SELECT * FROM sys.all_columns";
DataSet laData = lMyDatabase.SelectQuery(lsQuery);


Now, let's sort by column_id (this data set is a table of information that is built into MS SQL, so it should work as is, no need to create a database):


DataView laDataView = new DataView(laData.Tables[0]);
laDataView.Sort = "column_id";


Now you can spin through the sorted data:


foreach (DataRow lpRow in laDataView.Table.Rows)
{
    HttpContext.Current.Response.Write(lpRow["column_id"].ToString());
    HttpContext.Current.Response.Write(",");
    HttpContext.Current.Response.Write(lpRow["name"].ToString());
}