Click here to Skip to main content
15,885,985 members
Please Sign up or sign in to vote.
4.50/5 (2 votes)
See more:
Hello Al1,

Below is my html code for Gridview

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
           DataKeyNames="userId" DataSourceID="SqlDataSource1"
           EmptyDataText="There are no data records to display.">
           <Columns>
               <asp:BoundField DataField="userId" HeaderText="userId" ReadOnly="True"
                   SortExpression="userId" />
               <asp:BoundField DataField="userName" HeaderText="userName"
                   SortExpression="userName" />
               <asp:CheckBoxField DataField="isChecked" HeaderText="isChecked"
                   SortExpression="isChecked" />
           </Columns>
       </asp:GridView>


How to get check box value its checked or not
I know how to do that using Item template but from CheckBoxField how can i find that control .

Thanks in Advance.
Posted
Updated 5-May-11 9:03am
v2
Comments
Raj Bose 5-May-11 10:12am    
in cs file button click i wrote
CheckBox chk = (CheckBox)grv.FindControl("isChecked");
if (chk.Checked)
{
string a = "test";
}

but it returns null
Monjurul Habib 5-May-11 15:04pm    
edited: removed duplicate pre tags.

1 solution

The reason your are getting a null is that (as per your comment), you need to access a row in the grid view and then use find control. But you are probably doing it using the grid view (or the button). I guess so at least. You haven't posted all your code.

Check out the following tutorial -

http://www.asp.net/data-access/tutorials/adding-a-gridview-column-of-checkboxes-cs[^]

Especially this section -

Step 4: Adding Check All and Uncheck All Buttons

private void ToggleCheckState(bool checkState)
{
    // Iterate through the Products.Rows property
    foreach (GridViewRow row in Products.Rows)
    {
        // Access the CheckBox
        CheckBox cb = (CheckBox)row.FindControl("ProductSelector");
        if (cb != null)
            cb.Checked = checkState;
    }
}


This tutorial might be the key for all your questions!
 
Share this answer
 
Comments
Raj Bose 5-May-11 11:22am    
Thanks :) ,:) ,:),:)
TweakBird 5-May-11 11:32am    
Good call. Proposed as answer with 5.
Monjurul Habib 5-May-11 15:05pm    
nice answer.my 5.
Raj Bose 5-May-11 16:08pm    
hmm :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900