views:

36

answers:

1

how to bind the value to gridview

i have a datatable

DataTable dtBindGrid = new DataTable();

    dtBindGrid = serviceobj.SelectExamTimeTable(txtSchoolName.Text, txtBranchName.Text, txtClass.Text, txtExamName.Text);


    foreach (DataRow row in dtBindGrid.Rows)
    {

       strgetday=  row["Day"].ToString();
       strgetdate = row["Date"].ToString();

       DatedTime = Convert.ToDateTime(row["Time"].ToString());
      strgettime = DatedTime.ToString("t");

       strgetsubject = row["Subject"].ToString();
      strgetduration = row["Duration"].ToString();
      strgetsession = row["Session"].ToString();
      strgetminmark = row["MinMarks"].ToString();
      strgetmaxmark = row["MaxMarks"].ToString();

   //  dtBindGrid.Rows.Add(row);
    }



    GrdExamTimeTable.DataSource = dtBindGrid.DefaultView;
    GrdExamTimeTable.DataBind();

this datatable will return me some values like day,date,time,duration,subject,..

here im getting each value in string bec to convert the Time as 9.00am or 9.00pm

DatedTime = Convert.ToDateTime(row["Time"].ToString()); strgettime = DatedTime.ToString("t");

.... how to bind this converted value to my gridview.

A: 

You could add an extra column to your DataTable once you've got it back from the service, then as you iterate through the rows, calculate the DatedTime and update the row. As it is then in your GridView's data source you should be able to bind it as normal through a BoundColumn.

DataTable dtBindGrid = new DataTable();

dtBindGrid = serviceobj.SelectExamTimeTable(...);

dtBindGrid.Columns.Add(new DataColumn("DatedTime"));

foreach (DataRow row in dtBindGrid.Rows)
{
    DatedTime = Convert.ToDateTime(row["Time"].ToString());
    strgettime = DatedTime.ToString("t");

    row.BeginEdit();
    row.SetField("DatedTime", strgettime);
    row.EndEdit();

    row.AcceptChanges();
}

GrdExamTimeTable.DataSource = dtBindGrid.DefaultView;
GrdExamTimeTable.DataBind();
PhilPursglove