RowDataBound Event of GridView

The RowDataBound event occurs when a data row is bound to data in a GridView control. This event is raised a data row means GridViewRow object is bound to data in GridView control.

By help of this event we can modify row appearance or can modify data in data row at time of GridViewRow object bounds.

A RowDataBound event of gridview has two arguments one is object and another is GridViewRowEventArgs. GridViewRowEvnetArgs object enables us to access the properties of row that is bounding because  GridViewRowEventArgs object has Row property.

 

Design Of Gridview and DataSource

<asp:GridView ID="gvCustomres" runat="server"

            DataSourceID="EmployeeDataSource"

            AutoGenerateColumns="False"

            GridLines="Both"

            AllowPaging="true"           

            PagerStyle-CssClass="pgr"          

            Width=40% onrowdatabound="gvCustomres_RowDataBound">

            <Columns>

                <asp:TemplateField HeaderText="S.No." ItemStyle-HorizontalAlign="Center">

                    <ItemTemplate>

                        <%#Container.DataItemIndex+1%>

                    </ItemTemplate>

                </asp:TemplateField>

                <asp:BoundField DataField="Name" HeaderText="Employee Name" />

                <asp:BoundField DataField="Emp_Code" HeaderText="Employee Code" />

                <asp:BoundField DataField="Emp_Age" HeaderText="Employee Age" />               

            </Columns>

        </asp:GridView>

 

       <asp:SqlDataSource ID="EmployeeDataSource" runat="server"

        ConnectionString="Data Source=sandeepss-PC; database=development; user=sa;password=knowdev"

         SelectCommand="select Id, Name, Emp_Code,Emp_Age from EMPLOYEE">

     </asp:SqlDataSource>

 

RowDataBound Event

Here we are showing row background color blue these have age value greater than or equal 35.

protected void gvCustomres_RowDataBound(object sender, GridViewRowEventArgs e)

        {

            if (e.Row.RowType == DataControlRowType.DataRow)

            {

                int age =Convert.ToInt32( e.Row.Cells[3].Text);

                if (age >= 35)

                {

                    e.Row.BackColor =System.Drawing.ColorTranslator.FromHtml("#4F81BD");

                    e.Row.ForeColor =System.Drawing.ColorTranslator.FromHtml("#FFFFFF");

                }

            }

        }

 

1.PNG

Ebook Download
View all
Learn
View all