View Single Post
  #3 (permalink)  
Old March 8th, 2009, 02:16 AM
chobo2 chobo2 is offline
Friend of Wrox
 
Join Date: Sep 2007
Location: , , .
Posts: 169
Thanks: 7
Thanked 2 Times in 2 Posts
Default

Ah thanks. So what is better the databind way or this way?

Quote:
Originally Posted by Lee Dumond View Post
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>