DataGrid web control (original) (raw)
I so dislike how the datagrid works. I must be missing something, but I can't figure out what.
Basically all I want to do is take two fields from my table dtTimeIn and dtTimeOut, and take the hour difference between the two, then add a new column on the datagrid.
`
public void Submit_Click(object sender, System.EventArgs e)
{
SqlConnection c = new SqlConnection(ConfigurationSettings.AppSettings["maindb"]);
DataSet ds = new DataSet();
SqlDataAdapter cmd = new SqlDataAdapter("select * from timecard", c);
cmd.Fill(ds, "timecard");
DataColumn dc = new DataColumn("ttlhours", Type.GetType("System.String"));
ds.Tables["timecard"].Columns.Add(dc);
DataView dv = ds.Tables["timecard"].DefaultView;
DateTime dt1;
DateTime dt2;
for (int x = 0; x < dv.Count; x++)
{
dt1 = DateTime.Parse(dv[x]["dttimein"].ToString());
dt2 = DateTime.Parse(dv[x]["dttimeout"].ToString());
dv[x]["ttlhours"] = dt2.Subtract(dt1);
}
results.DataSource = ds;
results.DataBind();
}
`
There is the code that I am using for it so far, but that isn't working either... Any help you can provide would be appreciated (this is for a timecard control that I will redistribute to anyone once it is finished.)
Edit (this works!):
`
public void Submit_Click(object sender, System.EventArgs e)
{
SqlConnection c = new SqlConnection(ConfigurationSettings.AppSettings["maindb"]);
DataSet ds = new DataSet();
SqlDataAdapter cmd = new SqlDataAdapter("select * from timecard", c);
cmd.Fill(ds, "timecard");
results.DataSource = ds;
results.DataBind();
}
public void results_bind(object sender, DataGridItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
DateTime dt1 = DateTime.Parse(Convert.ToString(DataBinder.Eval(e.Item.DataItem, "dtTimeIn")));
DateTime dt2 = DateTime.Parse(Convert.ToString(DataBinder.Eval(e.Item.DataItem, "dtTimeOut")));
string dt = String.Format("{0:D}", dt2.Subtract(dt1));
e.Item.Cells[3].Text = dt;
}
}
`