我正在创建一个空数据行来绑定我的网格视图,但是,我总是得到“指定的强制转换无效”我的Checkbox错误消息
这是我的aspx:
<asp:TemplateField HeaderText="Include In Food Bag" ItemStyle-Width="20%" ItemStyle-HorizontalAlign="Center" FooterStyle-HorizontalAlign="Center">
<ItemTemplate>
<asp:CheckBox runat="server" ID="ChkBxIncludeInFoodBag" Checked='<%# Bind("IncludeInFoodBag") %>' ></asp:CheckBox>
</ItemTemplate>
<FooterTemplate>
<asp:CheckBox runat="server" ID="ChkBxIncludeInFoodBag"></asp:CheckBox>
</FooterTemplate>
</asp:TemplateField>
这是我背后的代码:
protected void BindFoodBagItemsGridView()
{
DataTable FoodBagItems = BLFoodBagItem.GetAllFoodBagItems();
if (FoodBagItems.Rows.Count > 0)
{
FoodBagItemsGridView.DataSource = BLFoodBagItem.GetAllFoodBagItems();
}
else
{
tempFoodItems = new DataTable();
tempFoodItems.Columns.Add("FoodBagItemId");
tempFoodItems.Columns.Add("ItemName");
tempFoodItems.Columns.Add("ItemQuantity");
tempFoodItems.Columns.Add("IncludeInFoodBag");
DataRow dr = tempFoodItems.NewRow();
dr["IncludeInFoodBag"] = bool.Parse("false");
-- I also tried dr["IncludeInFoodBag"] = false;
-- I also tried dr["IncludeInFoodBag"] = 0;
tempFoodItems.Rows.Add(dr);
FoodBagItemsGridView.DataSource = tempFoodItems;
}
FoodBagItemsGridView.DataBind();
}
答案 0 :(得分:0)
像这样添加你的专栏: -
tempFoodItems.Columns.Add("IncludeInFoodBag",typeof(bool));
您需要在将数据类型添加到DataTable时添加列的数据类型。