Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to bind check box values on gridview with the values of database table?

I have a data table column "staff" of type bit. In my grid view i have added an item template of check boxes. I wants o display the check boxes checked if the value of "staff" column =1 on data bind. other wise unchecked.. from searches i have written like this

<ItemTemplate>
   <asp:CheckBox ID="chk1" runat="server" Checked='<%# bool.Parse(Eval("staff").ToString()) %>'/>      
</ItemTemplate>

 DataSet ds = new DataSet();
 SqlDataAdapter adapter = new SqlDataAdapter("SELECT id,staff FROM staff_details  ", con1);
 adapter.Fill(ds);
 GridView1.DataSource = ds;
 GridView1.DataBind();

but it shows an error "System.FormatException: String was not recognized as a valid Boolean." please help

like image 353
ARATHY Avatar asked Aug 28 '13 07:08

ARATHY


People also ask

How to bind CheckBox in GridView checked?

The Checked property of the CheckBox is bound to the IsSelected column which is of BIT data type and hence when the value saved is TRUE the CheckBox will be selected and vice versa. Below the GridView, there is a Button for saving checked (selected) CheckBox values to database.

How to use CheckBox in GridView?

From the GridView s smart tag, click on the Edit Templates link and then drag a CheckBox Web control from the Toolbox into the ItemTemplate . Set this CheckBox s ID property to ProductSelector . With the TemplateField and CheckBox Web control added, each row now includes a checkbox.

How can add CheckBox in GridView in MVC?

There is no direct way to add a CheckBox to the Header Row of WebGrid and hence, using jQuery a dynamic CheckBox is created and it is appended to the first cell of the Header row of the WebGrid. The Header Row CheckBox is assigned a jQuery Click event handler.


2 Answers

Tested and works :

#UPDATE1

Checked='<%#Convert.ToBoolean(Eval("staff")) %>'

<ItemTemplate>
   <asp:CheckBox ID="chk1" runat="server" Checked='<%#Convert.ToBoolean(Eval("staff")) %>' />      
</ItemTemplate>
like image 59
Ahmed Alaa El-Din Avatar answered Nov 04 '22 03:11

Ahmed Alaa El-Din


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();
}
like image 34
Samiey Mehdi Avatar answered Nov 04 '22 04:11

Samiey Mehdi