Reputation: 150
I am working on editing gridview. I have added checkbox as well. I need to check when user clicks check button, it should give true value. if not false. Here is my Grid View.
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="ProductID" OnRowDeleting="DeleteRecord"
EmptyDataText="There are no data records to display." onrowediting="GridView1_RowEditing" onrowupdating="GridView1_RowUpdating"
AutoGenerateDeleteButton="True" AutoGenerateEditButton="True"
CellPadding="4" ForeColor="#333333" GridLines="None">
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:TemplateField HeaderText="Product Name" SortExpression="ProductName">
<EditItemTemplate>
<asp:TextBox ID="ProductTextBox" runat="server" Text='<%# Bind("ProductName") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("ProductName") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Description" SortExpression="Description">
<EditItemTemplate>
<asp:TextBox ID="DescriptionTextBox" runat="server" Text='<%# Bind("Description") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("Description") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Items In Set" SortExpression="ItemsInSet">
<EditItemTemplate>
<asp:TextBox ID="ItemsTextBox" runat="server" Text='<%# Bind("ItemsInSet") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("ItemsInSet") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Unit Price Owner" SortExpression="UnitPriceOwner">
<EditItemTemplate>
<asp:TextBox ID="PriceOwnerTextBox" runat="server" Text='<%# Bind("UnitPriceOwner") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("UnitPriceOwner") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Unit Price Reseller" SortExpression="UnitPriceReseller">
<EditItemTemplate>
<asp:TextBox ID="PriceResellerTextBox" runat="server" Text='<%# Bind("UnitPriceReseller") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("UnitPriceReseller") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Shipping Cost" SortExpression="ShippingCost">
<EditItemTemplate>
<asp:TextBox ID="CostTextBox" runat="server" Text='<%# Bind("ShippingCost") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("ShippingCost") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="In Offer" SortExpression="InOffer">
<EditItemTemplate>
<asp:TextBox ID="InOfferTextBox" runat="server" Text='<%# Bind("InOffer") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("InOffer") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Visible" SortExpression="Visible">
<EditItemTemplate>
<asp:CheckBox ID="VisibleCheckBox" runat="server" />
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("Visible") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#FFCC66" ForeColor="#333333" HorizontalAlign="Center" />
<RowStyle BackColor="#FFFBD6" ForeColor="#333333" />
<SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy" />
<SortedAscendingCellStyle BackColor="#FDF5AC" />
<SortedAscendingHeaderStyle BackColor="#4D0000" />
<SortedDescendingCellStyle BackColor="#FCF6C0" />
<SortedDescendingHeaderStyle BackColor="#820000" />
</asp:GridView>
I am getting error while executing the page. The error is String was not recognized as a valid Boolean.
Code:
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
int productId = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value);
TextBox productName = GridView1.Rows[e.RowIndex].FindControl("ProductTextBox") as TextBox;
TextBox description = GridView1.Rows[e.RowIndex].FindControl("DescriptionTextBox") as TextBox;
TextBox itemsInSet = GridView1.Rows[e.RowIndex].FindControl("ItemsTextBox") as TextBox;
TextBox unitPriceOwner = GridView1.Rows[e.RowIndex].FindControl("PriceOwnerTextBox") as TextBox;
TextBox unitPriceReseller = GridView1.Rows[e.RowIndex].FindControl("PriceResellerTextBox") as TextBox;
TextBox shippingCost = GridView1.Rows[e.RowIndex].FindControl("CostTextBox") as TextBox;
TextBox inOffer = GridView1.Rows[e.RowIndex].FindControl("InOfferTextBox") as TextBox;
CheckBox visible = GridView1.Rows[e.RowIndex].FindControl("VisibleCheckBox") as CheckBox;
Product product = new Product();
product.ProductID = productId;
product.ProductName = productName.Text;
product.Description = description.Text;
product.ItemsInSet = Convert.ToInt32(itemsInSet.Text);
product.UnitPriceOwner = Convert.ToInt32(unitPriceOwner.Text);
product.UnitPriceReseller = Convert.ToInt32(unitPriceReseller.Text);
product.ShippingCost = Convert.ToInt32(shippingCost.Text);
product.InOffer = Convert.ToBoolean(inOffer.Text);
product.Visible = Convert.ToBoolean((visible.Text).ToString());
ProductBL.UpdateProduct(product);
GridView1.EditIndex = -1;
GridView1.DataSource = ProductBL.GetProducts();
GridView1.DataBind();
}
Upvotes: 1
Views: 720
Reputation: 216243
The CheckBox has a boolean property named IsChecked
, you don't need a conversion to a string and then back to boolan at all, just use that property
product.Visible = visible.IsChecked;
By the way, your error is caused by the fact the you convert the Text property of the checkbox and this probably is just an empty string. Something that could not be converted to a boolean
Upvotes: 2