Using the RowCreated Event to programmatically change the style : GridView « ADO.net Database « ASP.Net






Using the RowCreated Event to programmatically change the style

<%@ Page Language="C#" %>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <title>Using the RowCreated Event to programmatically change the style</title>
    <script runat="server">
        protected void gridProducts_RowCreated(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {            
                int daysToManufacture = (int)DataBinder.Eval(e.Row.DataItem, "DaysToManufacture");

                if (daysToManufacture == 0)
                {
                    e.Row.BackColor = System.Drawing.Color.LightPink;
                    e.Row.ForeColor = System.Drawing.Color.Maroon;
                }
                else if (daysToManufacture == 1)
                {
                    e.Row.BackColor = System.Drawing.Color.LightCyan;
                    e.Row.ForeColor = System.Drawing.Color.DarkBlue;
                }
                else
                {
                    e.Row.BackColor = System.Drawing.Color.LightGray;
                    e.Row.ForeColor = System.Drawing.Color.Red;
                }
            }
        }   

    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:SqlDataSource ID="productsSource" runat="server"
            ProviderName="System.Data.SqlClient" 
            ConnectionString="<%$ ConnectionStrings:AdventureWorks %>"
            SelectCommand="Select ProductID, Name, ProductNumber, DaysToManufacture from Production.Product">            
        </asp:SqlDataSource>
        <asp:GridView runat="server" ID="gridProducts" DataSourceID="productsSource"
            AutoGenerateColumns="false" OnRowCreated="gridProducts_RowCreated">        
            <Columns>                             
                <asp:BoundField DataField="ProductID" HeaderText="ID" />
                <asp:BoundField DataField="Name" HeaderText="Name" />
                <asp:BoundField DataField="ProductNumber" HeaderText="Number" />
                <asp:BoundField DataField="DaysToManufacture" HeaderText="Days To Manufacture" />                
            </Columns>
        </asp:GridView>
    </div>
    </form>
</body>
</html>

 








Related examples in the same category

1.Displaying data in database table with the GridView control.
2.The GridView control supports programmatic databinding
3.Selecting Data
4.Using Data Keys
5.Paging Through Data
6.Sortable GridView
7.Change row style for higher value
8.Highlight Rows
9.Numeric Format Strings
10.Time and Date Format Strings
11.GridView custom paging with ObjectDataSource
12.GridView format event
13.Turn off the GridLine
14.GridView selection style
15.GridView Summaries
16.Using the RowDeleted event to catch SQL errors (C#)
17.Using the RowDeleted event to catch SQL errors (VB)
18.Adding a delete link to the GridView
19.Checking for Update errors using the RowUpdated event (C#)
20.Checking for Update errors using the RowUpdated event (VB)
21.Turning off AutoGenerateColumns in the GridView control
22.Adding Indicators to display Sorting Direction