GridView.RowDataBound Event No Data

I was trying to use the GridView.RowDataBound event to change the background color of the row in a GridView. But the data was missing. Here is what I trying:

void gvReport_RowDataBound(Object sender, GridViewRowEventArgs e)
{
   if (e.Row.RowType == DataControlRowType.DataRow)
   {
      if (e.Row.Cells[1].Text.IndexOf(“Total:”) > 0)
         e.Row.BackColor = System.Drawing.Color.FromArgb(247, 249, 223);
   }
}

But my e.Row.Cells[1].Text had no data in it. I am binding the grid in code so maybe that is why the grid would not show any data. Here is what I did to get it to work:

protected void gvReport_RowDataBound(object sender, GridViewRowEventArgs e)
{
   if (e.Row.RowType == DataControlRowType.DataRow)
   {
      DataRowView rowdata = (DataRowView)e.Row.DataItem;
      if (rowdata[1].ToString().IndexOf(“Total:”) > 0)
         e.Row.BackColor = System.Drawing.
Color.FromArgb(247, 249, 223);
   }
}

Hope this helps someone else.

 

Code to create a DataSet

Use this code to create a table and place it in a DataSet.

// Create a new DataTable.
System.Data.DataTable table = new DataTable(“Details”);
// Declare variables for DataColumn and DataRow objects.
DataColumn column;
DataRow row;
// Create new DataColumn, set DataType,
// ColumnName and add to DataTable.
column = new DataColumn();
column.DataType = System.
Type.GetType(“System.Int32”);
column.ColumnName =
“fk_agent_id”;
column.ReadOnly =
false;
column.Unique =
true;
// Add the Column to the DataColumnCollection.
table.Columns.Add(column);

// Create second column.
column = new DataColumn();
column.DataType = System.
Type.GetType(“System.String”);
column.ColumnName =
“agent_name”;
column.AutoIncrement =
false;
column.Caption =
“agent_name”;
column.ReadOnly =
false;
column.Unique =
false;
// Add the column to the table.
table.Columns.Add(column);

// Make the ID column the primary key column.
DataColumn[] PrimaryKeyColumns = new DataColumn[1];
PrimaryKeyColumns[0] = table.Columns[
“fk_agent_id”];
table.PrimaryKey = PrimaryKeyColumns;
// Instantiate the DataSet variable.
dataSet = new DataSet();
// Add the new DataTable to the DataSet.
dataSet.Tables.Add(table);

Code to create a DataRow and add it to a DataSet

Use this code to create a DataRow from a table in a DataSet and place it in the table in a DataSet.

// Call a method to create the DataSet with a table in it.

DataSet
resultData = MakeDetailTable();
DataRow row = resultData.Tables[“Details”].NewRow();
row[
“fk_agent_id”] = userInfo.account_id;
row[
“agent_name”] = userInfo.lname + “, “ + userInfo.fname;
resultData.Tables[“Details”].Rows.Add(row);

// Use the data in the GridView.
gvReport.DataSource = resultData.Tables[
“Details”];
gvReport.DataBind();