更新boundfield中的数据(输入字符串的格式不正确。)(updating data in boundfield (Input string was not in a correct format

编程入门 行业动态 更新时间:2024-10-26 21:19:56
更新boundfield中的数据(输入字符串的格式不正确。)(updating data in boundfield (Input string was not in a correct format.))

我有一个viewproducts页面,我想要的是点击编辑链接时,将编辑两个字段(名称和价格)。 这是我的HTML代码:

<ItemTemplate> <asp:LinkButton ID="LinkButton1" Text="Edit" runat="server" CommandName="Edit" /> </ItemTemplate> <EditItemTemplate> <asp:LinkButton ID="LinkButton2" Text="Update" runat="server" OnClick="OnUpdate" /> <asp:LinkButton ID="LinkButton3" Text="Cancel" runat="server" OnClick="OnCancel" /> </EditItemTemplate> </asp:TemplateField>

和代码背后:

private void GetProducts(int CategoryID) { ShoppingCart k = new ShoppingCart() { CategoryID = CategoryID }; gdview.DataSource = null; gdview.DataSource = k.GetAllProducts(); gdview.DataBind(); } protected void OnRowEditing(object sender, GridViewEditEventArgs e) { gdview.EditIndex = e.NewEditIndex; this.GetProducts(0); } protected void OnUpdate(object sender, EventArgs e) { GridViewRow row = (sender as LinkButton).NamingContainer as GridViewRow; string Name = (row.Cells[0].Controls[0] as TextBox).Text; string Price = (row.Cells[2].Controls[0] as TextBox).Text; DataTable dt = ViewState["dt"] as DataTable; dt.Rows[row.RowIndex]["Name"] = Name; dt.Rows[row.RowIndex]["Price"] = Price; ViewState["dt"] = dt; gdview.EditIndex = -1; this.GetProducts(0); } protected void OnCancel(object sender, EventArgs e) { gdview.EditIndex = -1; this.GetProducts(0); } protected void gdview_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { if (Convert.ToInt32(e.Row.Cells[6].Text) < 50) { e.Row.Cells[0].BackColor = System.Drawing.Color.Red; e.Row.Cells[0].ForeColor = System.Drawing.Color.White; e.Row.Cells[6].BackColor = System.Drawing.Color.Red; e.Row.Cells[6].ForeColor = System.Drawing.Color.White; } } }

但它给我一个错误说:输入字符串格式不正确。 红色文字是:

if (Convert.ToInt32(e.Row.Cells[6].Text) < 50)

我在这里错过了什么?

这是gridview代码:

<Columns> <asp:BoundField HeaderText="Name" DataField="Name" SortExpression="Name" > <ItemStyle Height="20px" Width="150px" /> </asp:BoundField> <asp:BoundField HeaderText="ProductCategory " ReadOnly="true" DataField="CategoryName" SortExpression="CategoryNaame" > <ItemStyle Height="20px" Width="150px" /> </asp:BoundField> <asp:BoundField HeaderText="Price" DataField="Price" SortExpression="Price" > <ItemStyle Height="20px" Width="150px" /> </asp:BoundField> <asp:ImageField HeaderText ="ImageUrl" DataImageUrlField="ImageUrl" SortExpression="ImageUrl" ReadOnly="true" ControlStyle-Width ="10"> <ControlStyle Width="50px"></ControlStyle> </asp:ImageField> <asp:BoundField HeaderText="ProductQuantity" DataField="ProductQuantity" ReadOnly="true" SortExpression="ProductQuantity" > <ItemStyle Height="20px" Width="150px" /> </asp:BoundField> <asp:BoundField HeaderText="ProductSold" DataField="ProductSold" SortExpression="ProductSold" ReadOnly="true" > <ItemStyle Height="20px" Width="150px" /> </asp:BoundField> <asp:BoundField HeaderText="AvailableStock" DataField="AvailableStock" SortExpression="AvailableStock " ReadOnly="true" > <ItemStyle Height="20px" Width="150px" /> </asp:BoundField>

当编辑文本框可见时,名称字段会消失吗?

I have a viewproducts page and what I want is when clicking on the edit link, two fields will be edited (Name and Price). here is my html code:

<ItemTemplate> <asp:LinkButton ID="LinkButton1" Text="Edit" runat="server" CommandName="Edit" /> </ItemTemplate> <EditItemTemplate> <asp:LinkButton ID="LinkButton2" Text="Update" runat="server" OnClick="OnUpdate" /> <asp:LinkButton ID="LinkButton3" Text="Cancel" runat="server" OnClick="OnCancel" /> </EditItemTemplate> </asp:TemplateField>

and code behind:

private void GetProducts(int CategoryID) { ShoppingCart k = new ShoppingCart() { CategoryID = CategoryID }; gdview.DataSource = null; gdview.DataSource = k.GetAllProducts(); gdview.DataBind(); } protected void OnRowEditing(object sender, GridViewEditEventArgs e) { gdview.EditIndex = e.NewEditIndex; this.GetProducts(0); } protected void OnUpdate(object sender, EventArgs e) { GridViewRow row = (sender as LinkButton).NamingContainer as GridViewRow; string Name = (row.Cells[0].Controls[0] as TextBox).Text; string Price = (row.Cells[2].Controls[0] as TextBox).Text; DataTable dt = ViewState["dt"] as DataTable; dt.Rows[row.RowIndex]["Name"] = Name; dt.Rows[row.RowIndex]["Price"] = Price; ViewState["dt"] = dt; gdview.EditIndex = -1; this.GetProducts(0); } protected void OnCancel(object sender, EventArgs e) { gdview.EditIndex = -1; this.GetProducts(0); } protected void gdview_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { if (Convert.ToInt32(e.Row.Cells[6].Text) < 50) { e.Row.Cells[0].BackColor = System.Drawing.Color.Red; e.Row.Cells[0].ForeColor = System.Drawing.Color.White; e.Row.Cells[6].BackColor = System.Drawing.Color.Red; e.Row.Cells[6].ForeColor = System.Drawing.Color.White; } } }

However it gives me an error saying: Input string was not in a correct format. and the red text is with:

if (Convert.ToInt32(e.Row.Cells[6].Text) < 50)

what am I missing here?

here is the gridview code:

<Columns> <asp:BoundField HeaderText="Name" DataField="Name" SortExpression="Name" > <ItemStyle Height="20px" Width="150px" /> </asp:BoundField> <asp:BoundField HeaderText="ProductCategory " ReadOnly="true" DataField="CategoryName" SortExpression="CategoryNaame" > <ItemStyle Height="20px" Width="150px" /> </asp:BoundField> <asp:BoundField HeaderText="Price" DataField="Price" SortExpression="Price" > <ItemStyle Height="20px" Width="150px" /> </asp:BoundField> <asp:ImageField HeaderText ="ImageUrl" DataImageUrlField="ImageUrl" SortExpression="ImageUrl" ReadOnly="true" ControlStyle-Width ="10"> <ControlStyle Width="50px"></ControlStyle> </asp:ImageField> <asp:BoundField HeaderText="ProductQuantity" DataField="ProductQuantity" ReadOnly="true" SortExpression="ProductQuantity" > <ItemStyle Height="20px" Width="150px" /> </asp:BoundField> <asp:BoundField HeaderText="ProductSold" DataField="ProductSold" SortExpression="ProductSold" ReadOnly="true" > <ItemStyle Height="20px" Width="150px" /> </asp:BoundField> <asp:BoundField HeaderText="AvailableStock" DataField="AvailableStock" SortExpression="AvailableStock " ReadOnly="true" > <ItemStyle Height="20px" Width="150px" /> </asp:BoundField>

The name field disappears when the textbox for edit is visible?

最满意答案

单元格中的文本为空,无法转换为int因此请检查文本是否为空而不是将其转换为int

if(!string.IsNullOrEmpty(e.Row.Cells[6].Text))

如果您不想使列可编辑,请设置属性ReadOnly="true"

ReadOnly="true"

例如,如果你想设置这个coulmn ReadOnly="true"

<asp:BoundField HeaderText="Name" DataField="Name" SortExpression="Name" ReadOnly="true"> <ItemStyle Height="20px" Width="150px" /> </asp:BoundField>

The text in cell is empty which can't be converted to int so put a check if text in not empty than convert it to int

if(!string.IsNullOrEmpty(e.Row.Cells[6].Text))

If you don't want to make a column editable set the property ReadOnly="true"

ReadOnly="true"

For example if you want to set this coulmn ReadOnly="true"

<asp:BoundField HeaderText="Name" DataField="Name" SortExpression="Name" ReadOnly="true"> <ItemStyle Height="20px" Width="150px" /> </asp:BoundField>

更多推荐

本文发布于:2023-07-18 11:40:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1159808.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字符串   不正确   格式   数据   boundfield

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!