2014-12-20 82 views
0

我創建一個空的數據行到我的網格視圖結合,但是,我一直都想與「指定的強制轉換無效」錯誤消息對我的複選框複選框指定的轉換無效

這是我的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

添加您列如下: -

tempFoodItems.Columns.Add("IncludeInFoodBag",typeof(bool)); 

您需要添加列的數據類型,同時將其添加到數據表。