GridView.RowDataBound Event

Occurs when a data row is bound to data in a GridView control.
Namespace:  System.Web.UI.WebControls
Assembly:  System.Web (in System.Web.dll)



public event GridViewRowEventHandler RowDataBound
<asp:GridView OnRowDataBound="GridViewRowEventHandler" />
In the previous article I explined about gridviewsorting and gridviewpaging
Before the GridView control can be rendered, each row in the control must be bound to a record in the data source. The RowDataBoundevent is raised when a data row (represented by a GridViewRow object) is bound to data in the GridView control. This enables you to provide an event-handling method that performs a custom routine, such as modifying the values of the data bound to the row, whenever this event occurs.
A GridViewRowEventArgs object is passed to the event-handling method, which enables you to access the properties of the row being bound. To access a specific cell in the row, use the Cells property of the GridViewRow object contained in the Row property of the GridViewRowEventArgs object. You can determine which row type (header row, data row, and so on) is being bound by using the RowType property.

<asp:GridView runat="server" ID="gvEmpdetails" DataKeyNames="EmpID" DataSourceID="EmpDetails"
AllowPaging="True" AllowSorting="True" AutoGenerateColumns="False" OnRowDataBound="gvEmpdetails_RowDataBound">
<RowStyle BackColor="#EFF3FB" />
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:TemplateField>
<HeaderTemplate>
<asp:CheckBox runat="server" ID="chkhead" />
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox ID="chk" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="EmpID" HeaderText="EmpID" SortExpression="EmpID" />
<asp:BoundField DataField="EmpName" HeaderText="EmpName" SortExpression="EmpName" />
<asp:BoundField DataField="EmpSal" HeaderText="EmpSal" SortExpression="EmpSal" />
<asp:BoundField DataField="EmpBranch" HeaderText="EmpBranch" SortExpression="EmpBranch" />
<asp:BoundField DataField="EmpCode" HeaderText="EmpCode" SortExpression="EmpCode" />  
<asp:TemplateField>
<ItemTemplate>
<asp:TextBox ID="txt1" runat="server" />
</ItemTemplate>
</asp:TemplateField>         
</Columns>
</asp:GridView>


In the below Code We can find the control in header and Datarow of the gridview as Follows:


protected void gvEmpdetails_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        //This condition is used to check RowType is Header
        if (e.Row.RowType == DataControlRowType.Header)
        {
            //Find the checkbox control in header and add an attribute
            CheckBox chk = (CheckBox)e.Row.FindControl("chkhead");

//I am calling Javascript Function SelectAll and passing chkhead Client ID
            chk.Attributes.Add("onclick", "javascript:SelectAll('" + ((CheckBox)e.Row.FindControl("chkhead")).ClientID + "')");
        }

        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            TextBox txt1 = (TextBox)e.Row.FindControl("txt1");
        }

    }


0 Responses to “GridView.RowDataBound Event”

Post a Comment

Labels

Topics