Hellow frnz

i wanted to select row in gridview using checkbox, using c#.net
pleae help me

Hi
Please check out this code it will really help you.

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" BorderStyle="Solid" CellPadding="4" DataKeyNames="CategoryID" BorderColor="Silver" BorderWidth="1px" Width="300px"> 



<Columns> 



<asp:TemplateField HeaderText="Categories"> 



<HeaderTemplate> 



<asp:CheckBox ID="chkSelectAll" runat="server" Text="SelectAll" AutoPostBack="true" OnCheckedChanged="chkSelectAll_CheckedChanged" /> 



</HeaderTemplate> 



<ItemTemplate> 



<asp:CheckBox ID="chk1" runat="server" Text='<%# DataBinder.Eval( Container.DataItem,"categoryID" ) + " " + DataBinder.Eval( Container.DataItem,"categoryName" ) %>' /> 



</ItemTemplate> 



</asp:TemplateField> 



</Columns> 



<HeaderStyle HorizontalAlign="Left" /> 



</asp:GridView> 

In the above HTML code we have added two checkboxes inside the GridView control. 1st checkbox control has been added inside the HeaderTemplate of TemplateField column. This checkbox will perform the function to select all the checkboxes rendered in the row items of GridView. 2nd checkbox has been placed inside the ItemTemplate of same TemplateField column that will generate row items like a checkbox list. In the above HTML code you can see that two additional properties of checkbox have been used for the checkbox placed inside the HeaderTemplate. AutoPostback="true" property enables the checkbox control to execute the associated server end method code, for example chkSelectAll_CheckedChanged server code method in the above sample associated with onCheckedChanged event of checkbox. Now next step is to add C# server end method code for Select All checkbox control placed inside the header.

C# code for ASP.Net GridView Checkbox Select All Function

protected void chkSelectAll_CheckedChanged(object sender, EventArgs e)
{
    CheckBox chk; 

    foreach (GridViewRow rowItem in GridView1.Rows)
    { 


        chk = (CheckBox)(rowItem.Cells[0].FindControl("chk1")); 


        chk.Checked =((CheckBox)sender).Checked; 


    }
} 

Above C# code shows that how to find checkbox control placed inside the each row of GridView control and access its Checked property to set the checkbox state accordingly. ((Checkbox) sender).Checked code has been used to get the state of checkbox placed inside the HeaderTemplate of TemplateField column.

Edited 3 Years Ago by mike_2000_17: Fixed formatting

This article has been dead for over six months. Start a new discussion instead.