Solved

Bind dataset to Checkbox column in asp/net gridview

Posted on 2013-12-09
2
5,401 Views
Last Modified: 2014-01-14
I am binding a asp.net gridview to a dataset in a webform using vb.net.    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
0
Comment
Question by:Mike_Stevens
2 Comments
 
LVL 11

Assisted Solution

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

ASPX:

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">
    <Columns>
        <asp:TemplateField HeaderText="ID">
            <ItemTemplate>
                <asp:Label ID="Label1" runat="server" Text='<%# Eval("id") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Staff">
            <ItemTemplate>
                <asp:CheckBox ID="CheckBox1" runat="server" Checked='<%# Eval("staff") %>' />
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>
<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);
    con1.Open();
    DataSet ds = new DataSet();
    SqlDataAdapter adapter = new SqlDataAdapter("SELECT id,staff FROM staff_details  ", con1);
    adapter.Fill(ds);
    GridView1.DataSource = ds;
    GridView1.DataBind();
    con1.Close();
}
0
 
LVL 12

Accepted Solution

by:
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" >
                <ItemTemplate>
                    <asp:CheckBox ID="CheckBox1" runat="server" Checked='<%# iif(Bind("IsApproved")=0,"False","True") %>' />
                </ItemTemplate>
            </asp:TemplateField>

hope this helps.
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

After several hours of googling I could not gather any information on this topic. There are several ways of controlling the USB port connected to any storage device. The best example of that is by changing the registry value of "HKEY_LOCAL_MACHINE\S…
For most people, the WrapPanel seems like a magic when they switch from WinForms to WPF. Most of us will think that the code that is used to write a control like that would be difficult. However, most of the work is done by the WPF engine, and the W…
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. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

744 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

16 Experts available now in Live!

Get 1:1 Help Now