Menu ▼

Posted By :
Total Views : 24409 GridView RowDataBound example using C#. GridView RowDataBound event is raised when a data row is bound to data in the GridView control. This event can be used to modifying the values of the Grid Row/Cell. In this example we will modify the gridview row cell (Qty) background color based on the cell value. If the Qty is grater than 10 then we apply background color green else apply background color red.


Product CodeProduct CategoryProduct NameStock
P-101MobileSamsung S320
P-102MobileSamsung S46
P-102MobileSamsung S512
P-102MobileIPhone 59
P-102MobileIPhone 618

Design Page
<asp:GridView ID="gv" runat="server" CellPadding="6" BackColor="White" 
    BorderColor="#CCCCCC"  BorderWidth="1px" onrowdatabound="gv_RowDataBound" >
    <FooterStyle BackColor="White" ForeColor="#000066" />
    <HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" />
    <PagerStyle BackColor="White" ForeColor="#000066" HorizontalAlign="Left" />
    <RowStyle ForeColor="#000066" />
    <SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="White" />
    <SortedAscendingCellStyle BackColor="#F1F1F1" />
    <SortedAscendingHeaderStyle BackColor="#007DBB" />
    <SortedDescendingCellStyle BackColor="#CAC9C9" />
    <SortedDescendingHeaderStyle BackColor="#00547E" />

C# Code
protected void Page_Load(object sender, EventArgs e)
        DataTable dt = new DataTable();
        dt.Columns.Add("Product Code", typeof(string));
        dt.Columns.Add("Product Category", typeof(string));
        dt.Columns.Add("Product Name", typeof(string));
        dt.Columns.Add("Stock", typeof(int));
        dt.Rows.Add("P-101", "Mobile", "Samsung S3", 20);
        dt.Rows.Add("P-102", "Mobile", "Samsung S4", 6);
        dt.Rows.Add("P-102", "Mobile", "Samsung S5", 12);
        dt.Rows.Add("P-102", "Mobile", "IPhone 5", 9);
        dt.Rows.Add("P-102", "Mobile", "IPhone 6", 18);
        gv.DataSource = dt;



protected void gv_RowDataBound(object sender, GridViewRowEventArgs e)
        if (e.Row.RowType == DataControlRowType.DataRow)
            int Qty;
            if (int.TryParse(e.Row.Cells[3].Text, out Qty))
                if (Qty > 10)
                    e.Row.Cells[3].BackColor = System.Drawing.Color.Green;
                    e.Row.Cells[3].BackColor = System.Drawing.Color.Red;


you can download the source code form here... Download