Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

asp.net gridview with checkbox

Posted on 2012-03-20
1
Medium Priority
?
749 Views
Last Modified: 2012-03-20
Hello experts,

I have a gridview with a checkbox column.  I am binding the gridview to a dataset. The first column in the dataset is bool (true\false).  When the page loads, how can I programmatically check the check boxes if the dataset column == true?

Thanks in advance.

            <asp:GridView ID="gvPayments" runat="server" AutoGenerateColumns="false"
                DataKeyNames="rowid" GridLines="None" AllowPaging="false" CssClass="gvGrid"
                PagerStyle-CssClass="pgr" AlternatingRowStyle-CssClass="alt"
                >
            <Columns>    
                <asp:TemplateField>
                    <HeaderTemplate>
                        <asp:CheckBox ID="ChkAll" runat="server" />
                    </HeaderTemplate>
                    <ItemTemplate>
                        <asp:CheckBox ID="chkSel" class="chkSel" runat="server" />
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:BoundField DataField="path" HeaderText="path"
                    SortExpression="path" />
                <asp:BoundField DataField="valuetext" HeaderText="value"
                    SortExpression="valuetext" />
            </Columns>
            </asp:GridView>
0
Comment
Question by:j420exe1
1 Comment
 
LVL 38

Accepted Solution

by:
Tom Beck earned 1500 total points
ID: 37745550
This could be done in the RowDataBound event.

You could first add the boolean column to the DataKeyNames.
DataKeyNames="rowid, boolCol"
 protected void gvPayments_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                CheckBox chkBox = (CheckBox)e.Row.FindControl("ChkAll");
                bool col1 = (bool)GridView1.DataKeys[1].Value;
                chkBox.Checked = col1;
            }
        }

Open in new window


Or you could bind it in the GridView markup.

<asp:CheckBox ID="ChkAll" runat="server" Checked='<%# Bind("boolColID") %>' />
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
High user turnover can cause old/redundant user data to consume valuable space. UserResourceCleanup was developed to address this by automatically deleting user folders when the user account is deleted.
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Screencast - Getting to Know the Pipeline

916 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question