Bind dataset to Checkbox column in asp/net gridview

Posted on 2013-12-09
Last Modified: 2014-01-14
I am binding a gridview to a dataset in a webform using    I am able to populate the grid expect the first column in the grid is a checkbox that I have placed in a template field.   That value that I want to display in the check box column is a true/false value stored as a bit in the SQL Server database.

I have tried all of the examples found here and either none of the check boxes are checked when they should be and no error is returned.

If have tried the following: (isApproved is the column name in the dataset)

.......Checked='<%# DataBinder.Eval(Container.DataItem, "isApproved") %>'/> ... No errors returned but no check boxes checked either

.......Checked='<%#If(Eval("IsApproved").ToString(), True, False)%>' ... No errors returned but no check boxes checked either

in the GridView1_RowDataBound event I tried.... and got the error "Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index "

...If e.Row.RowType = DataControlRowType.DataRow Then
             Dim chk As Boolean = Convert.ToBoolean((Me.GridView1.DataKeys  e.Row.RowIndex).Value.ToString()))
            'Find Check Box Now
            Dim chkBox As CheckBox = DirectCast(e.Row.FindControl("AcctApproved"), CheckBox)
            chkBox.Checked = chk
        End If

Any help would be greatly appreciated
Question by:Mike_Stevens
LVL 11

Assisted Solution

SAMIR BHOGAYTA earned 250 total points
ID: 39707806
hi.. try this example


<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">
        <asp:TemplateField HeaderText="ID">
                <asp:Label ID="Label1" runat="server" Text='<%# Eval("id") %>'></asp:Label>
        <asp:TemplateField HeaderText="Staff">
                <asp:CheckBox ID="CheckBox1" runat="server" Checked='<%# Eval("staff") %>' />
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />

Code behind:

protected void Button1_Click(object sender, EventArgs e)
    string conStr = @"Data Source=.\SQLEXPRESS;AttachDbFilename=E:\website\w2\App_Data\Database.mdf;Integrated Security=True;User Instance=True";
    SqlConnection con1 = new SqlConnection(conStr);
    DataSet ds = new DataSet();
    SqlDataAdapter adapter = new SqlDataAdapter("SELECT id,staff FROM staff_details  ", con1);
    GridView1.DataSource = ds;
LVL 12

Accepted Solution

jitendra patil earned 250 total points
ID: 39711078
you can give a try to the below markup too.
<asp:TemplateField HeaderText="Serial Number" SortExpression="SERIALNUMBER" >
                    <asp:CheckBox ID="CheckBox1" runat="server" Checked='<%# iif(Bind("IsApproved")=0,"False","True") %>' />

hope this helps.

Featured Post

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

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

User art_snob ( encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA.…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

820 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