我有一个bit类型的数据表列“staff”。在我的网格视图中,我添加了一个复选框的项目模板。如果数据绑定上“staff”列的值= 1,我希望显示复选框。其他明智的未经检查..来自我写的像这样的搜索
<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:String未被识别为有效的布尔值。”请帮忙
答案 0 :(得分:4)
经过测试和工作:
<强>#UPDATE1 强>
Checked='<%#Convert.ToBoolean(Eval("staff")) %>'
<ItemTemplate>
<asp:CheckBox ID="chk1" runat="server" Checked='<%#Convert.ToBoolean(Eval("staff")) %>' />
</ItemTemplate>
答案 1 :(得分:2)
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" />
代码背后:
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();
}