Change Rows Background color in GridView with selected Criteria

In this article we change background color of the rows in a GridView with selected criteria on GridView1_RowDataBound event.

In aspx.cs page

In aspx.cs page write the code for the Gridview1_RowDataBound Event. Set the criteria you want for the Gridview Row Backcolor Change. The GridView offers a RowDataBound event that fires once for each row after the row has been created and bound to the corresponding record of data from the data source control. In this article I am changing the row background color when CategoryID == 2; you can use whatever criteria you need to.

protected void Gridview1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            // determine the value of the UnitsInStock field
            int CategoryID = Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "CategoryID"));
            if (CategoryID == 2)
                // color the background of the row yellow
                e.Row.BackColor = System.Drawing.Color.Brown;
        }

    }


In aspx page

In the aspx page on the GridView set the RowDataBound event in the GridView property. Then, in the RowDataBound line, type in the name of the event handler you want to create (I named my event handler productsGridView_RowDataBound). Finally, enter in the following code into the event handler on OnRowDataBound="Gridview1_RowDataBound" .



Conclusion

In this article we try to improve GridView design based on certain criteria. Hopefully it's useful for you.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值