Solved

asp.net gridview with checkbox

Posted on 2012-03-20
1
745 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 38

Accepted Solution

by:
Tom Beck earned 500 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Come and listen to Percona CEO Peter Zaitsev discuss what’s new in Percona open source software, including Percona Server for MySQL (https://www.percona.com/software/mysql-database/percona-server) and MongoDB (https://www.percona.com/software/mongo-…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

690 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