View Single Post
  #1 (permalink)  
Old March 6th, 2009, 07:40 PM
chobo2 chobo2 is offline
Friend of Wrox
Join Date: Sep 2007
Location: , , .
Posts: 169
Thanks: 7
Thanked 2 Times in 2 Posts
Default Databind a gridview question.


In a book I have it has this

protected void gridEmployees_RowDataBound(object sender, GridViewRowEventArgs e)
if (e.Row.RowType == DataControlRowType.DataRow)
// Get the title of courtesy for the item that's being created.
string title = (string)
DataBinder.Eval(e.Row.DataItem, "TitleOfCourtesy");
// If the title of courtesy is "Ms.", "Mrs.", or "Mr.",
// change the item's colors.
if (title == "Ms." || title == "Mrs.")
e.Row.BackColor = System.Drawing.Color.LightPink;
e.Row.ForeColor = System.Drawing.Color.Maroon;
else if (title == "Mr.")
e.Row.BackColor = System.Drawing.Color.LightCyan;
e.Row.ForeColor = System.Drawing.Color.DarkBlue;
Then after it has this

■Tip This example uses the DataBinder.Eval() method to retrieve a piece of information from the data item using
reflection. Alternatively, you could cast the e.Row.DataItem to the correct type (such as EmployeeDetails for the
ObjectDataSource), DataRowView (for the SqlDataSource in DataSet mode), or DbDataRecord (for the SqlDataSource in
DataReader mode). However, the DataBinder.Eval() approach works in all these scenarios (at the cost of being slightly
It never tells you how to actually to write the line of code of any of these. Like say for the SqlDataSource in a dataset.

I am not sure how the line would be I

tried to do this:

string title = (RowDataView)e.Row.DataItem.

I got stuck there since I did not know how to choose the header it should be looking for.