RowDataBound event in a GridView to highlight the rows or cells

RowDataBound event in Asp.net GridView to highlight the rows or cells:



In this Post i want describe about few topic  in rowdatabound event which is available in asp.net gridview control and these tips will provide the solution for all possible requirements when working with rowdatabound event in gridview control.

How to use a RowDataBound event in a GridView:

how to use a RowDataBound event in a GridView to highlight the rows or cells in ASP.NET. This example is helpful in situations where you need to highlight the GridView rows or cells based on a specific condition.

First we take an ASP.NET GRID Control:

<asp:GridView ID="GRDSample" runat="server" AutoGenerateColumns="false" OnRowDataBound="GRDSample_RowDataBound">
            <Columns>
                <asp:TemplateField HeaderText="Userprofile">
                    <ItemTemplate>
                     
                        <asp:Label ID="lblText" runat="server" Text='<%# Eval("Price") %>'
></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>


In this we take  lable control in grid control.

Know we want to create event by C# code on .aspx.cs page

protected void GRDSample_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            DataRowView drview = e.Row.DataItem as DataRowView;
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                Label lblText       = e.Row.FindControl("lblText") as Label;
                if (Convert.ToInt32(lblText.Text) > 100)
                {
                    e.Row.ForeColor = System.Drawing.Color.Black;
                    e.Row.Font.Bold = true;
                    e.Row.BackColor = System.Drawing.Color.Brown; 
                }
           
            }
      }


If you want to change the gridview row color change on mouse over and mouse out then write the below code in rowdatabound event.


Asp.net Related Other Post :




Comments

Popular posts from this blog