如何在GridView控件绑定复选框值与数据库表中的值? [英] how to bind check box values on gridview with the values of database table?
本文介绍了如何在GridView控件绑定复选框值与数据库表中的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有位类型的数据表列参谋。在我的网格视图我已经添加的复选框的项目模板。我希望O显示复选框如果选中数据绑定工作人员栏= 1的值。其他明智选中..从我写这样的搜索
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();
但它显示了一个错误System.FormatException:字符串未被识别为有效的布尔值。请大家帮忙
but it shows an error "System.FormatException: String was not recognized as a valid Boolean." please help
推荐答案
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:
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();
}
这篇关于如何在GridView控件绑定复选框值与数据库表中的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文