Work with Dataset Prior to Displaying

  • Thread starter Thread starter Scott D
  • Start date Start date
S

Scott D

I am querying a SQL database and then creating a dataset and binding
that to the datagrid. Is their any way prior to binding I can work
with the data in the dataset? What I am trying to do is run some
logic on the data prior to binding it to the datagrid. For example:

I am checking for a certain value in two fields if I find the value in
one field I would like to display that date in a column if not I would
like to display the other date in the same column. Is there an easier
way to accomplish this?
 
Yes, you can do whatever you want to. That is what DataSet is meant: a whole
set of orgnized, disconnected data to be manipulated by your app. It is not
only for binding to DataGrid.

In your case, you can:

foreach (DataRow r in myDS.Tables["TheTable"}.Rows)
{
if (r["field1"].ToString()=="A" && r["field"].ToString()=="B")
{
r["MyDateField"]=DateTime.Today; //if conditions are met, set to
Today
}
else
{
r["MyDateField"]=DateTime.ToDay.AddDays(7); //otherwise, set to
one week later
}
}
 
Oh, by the way,

What you want is very easy to be accomplished when the database is queried,
so you do not even have to manipulate DataSet:

SELECT
Field1,
Field2,
CASE
WHEN Field1=1 THEN '2004-01-06'
ELSE '2004-01-13'
END AS MyDate
FROM TheTable
WHERE ...
ORDER BY...

So, it is up to you to decide where you want the data is processed before
binding to DataGrid, on server side, or on client side.
 
Back
Top