Monday, February 29, 2016

How to change a value of a DataItem in a GridViews RowDataBound Event

if (e.Row.RowType == DataControlRowType.DataRow)
{
 DataRow row = ((DataRowView)e.Row.DataItem).Row;
 for (int i = 0; i < row.ItemArray.Length; i++)
 {
  var obj = row.ItemArray[i];
  if (obj.GetType().Name == "DateTime")
  {
   e.Row.Cells[i + 1].Text = ((DateTime)obj).ToShortDateString();
  }
  else if (obj.GetType().Name == "Boolean")
  {
   e.Row.Cells[i + 1].Text = ((Boolean)obj).ToYesNo();
  }
 }
}

No comments:

Post a Comment