Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

asp.net gridview with checkbox

Posted on 2012-03-20
1
Medium Priority
?
748 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 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

Technology Partners: 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

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…

688 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