View Single Post
  #2 (permalink)  
Old March 6th, 2009, 09:41 PM
Lee Dumond's Avatar
Lee Dumond Lee Dumond is offline
Wrox Author
Points: 4,942, Level: 29
Points: 4,942, Level: 29 Points: 4,942, Level: 29 Points: 4,942, Level: 29
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jan 2008
Location: Decatur, IL, USA.
Posts: 923
Thanks: 12
Thanked 166 Times in 162 Posts
Default

Here you go... blue for the boys, pink for the girls.

Code:
 
<%@ Import Namespace="System.Drawing" %>
<%@ Import Namespace="System.Data" %>
<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
   <title>Fun with Databinding</title>
   <script runat="server">    
      protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
      {
         if (e.Row.RowType == DataControlRowType.DataRow)
         {
            DataRowView rowView = (DataRowView)e.Row.DataItem;
            string title = (string)rowView["TitleOfCourtesy"];
            if (title == "Ms." || title == "Mrs.")
            {
               e.Row.BackColor = Color.LightPink;
               e.Row.ForeColor = Color.Maroon;
            }
            else if (title == "Mr.")
            {
               e.Row.BackColor = Color.LightCyan;
               e.Row.ForeColor = Color.DarkBlue;
            }
         }
      }
   </script>
</head>
<body>
   <form id="form1" runat="server">
      <div>
         <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
            DataSourceID="SqlDataSource1" OnRowDataBound="GridView1_RowDataBound">
            <Columns>
               <asp:BoundField DataField="FirstName" HeaderText="FirstName"
                  SortExpression="FirstName" />
               <asp:BoundField DataField="LastName" HeaderText="LastName" SortExpression="LastName" />
               <asp:BoundField DataField="TitleOfCourtesy" HeaderText="TitleOfCourtesy"
                  SortExpression="TitleOfCourtesy" />
            </Columns>
         </asp:GridView>
         <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"
            SelectCommand="SELECT [FirstName], [LastName], [TitleOfCourtesy] FROM [Employees]">
         </asp:SqlDataSource>
      </div>
   </form>
</body>
</html>
__________________
Visit my blog at http://leedumond.com
Follow me on Twitter: http://twitter.com/LeeDumond

Code:
if (this.PostHelpedYou)
{
   ClickThanksButton(); 
}
The Following User Says Thank You to Lee Dumond For This Useful Post:
chobo2 (March 8th, 2009)