Solved

asp.net gridview with checkbox

Posted on 2012-03-20
1
731 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 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Get id from json Data with NewtonSoft 3 32
Problem to error 4 43
Error in page 3 45
ASP.NET - Bind Gridview Datasource to a specific table in dataset 3 11
This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

919 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now