How-Update-Data-In-Asp-Net-GridView

本文关键字:How-Update-Data-In-Asp-Net-GridView | 更新日期: 2023-09-27 18:13:02

无法强制转换'System.Web.UI '类型的对象。键入'System.Web.UI.WebControls.TextBox'。当我更新记录时显示此错误我想通过datagridview根据id更新记录。

 protected void GridView2_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
    SqlConnection conn = new SqlConnection(conctiioon);
    int userid = Convert.ToInt32(GridView2.DataKeys[e.RowIndex].Value.ToString());
    GridViewRow row = (GridViewRow)GridView2.Rows[e.RowIndex];
    //TextBox textName = (TextBox)row.Cells[0].Controls[0];
    TextBox textName = (TextBox)row.Cells[1].Controls[0];
    TextBox textprice = (TextBox)row.Cells[2].Controls[0];//here the error display 
    GridView2.EditIndex = -1;
    conn.Open();
    //SqlCommand cmd = new SqlCommand("SELECT * FROM detail", conn);
    SqlCommand cmd = new SqlCommand("update items set name='" + textName.Text + "',price='" + textprice.Text + "' where id='" + userid + "'", conn);
    cmd.ExecuteNonQuery();
    conn.Close();
    gvbind();
}

        <asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="False" DataKeyNames="Id"
            OnRowDeleting="GridView2_RowDeleting" OnRowEditing="GridView2_RowEditing" OnRowUpdating="GridView2_RowUpdating"
            CellPadding="4" ForeColor="#333333" GridLines="None">
            <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
            <Columns>
                <asp:BoundField DataField="id" HeaderText="S.No." />
                <asp:BoundField DataField="name" HeaderText="Name" />
                <asp:TemplateField HeaderText="Price">
                    <ItemTemplate>
                        <asp:TextBox ID="TextBox1" runat="server" Text='<%# Eval("Price") %>'
                            CssClass="stock">
                        </asp:TextBox>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField>
                    <ItemTemplate>
                        <a href="#" id='<%# Eval("id") %>' class="updatebutton">
                            <img border="0" src="Images/update.png" alt="Delete" />
                        </a>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:CommandField   ShowEditButton="true" />
                <asp:CommandField ShowDeleteButton="true" />
            </Columns>
            <EditRowStyle BackColor="#999999" />
            <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
            <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
            <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
            <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
            <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
            <SortedAscendingCellStyle BackColor="#E9E7E2" />
            <SortedAscendingHeaderStyle BackColor="#506C8C" />
            <SortedDescendingCellStyle BackColor="#FFFDF8" />
            <SortedDescendingHeaderStyle BackColor="#6F8DAE" />
        </asp:GridView>

How-Update-Data-In-Asp-Net-GridView

表示NameSapce(UperCase)中的System.Web.UI.WebControl

我模拟了你的gridview并实现了一个简单的数据源。当单元格中有3个控件时,假设价格文本框的索引为0。该单元格的第一个控件不是您的文本框,因此强制转换失败。

而不是假设索引是什么,只是找到使用GridViewRow你已经找到的控件。

TextBox textprice = (TextBox)(row.FindControl("TextBox1"));

TextBox textprice = (TextBox)Gridview2.Rows[e.Rowindex].FindControl("TextBox1");
相关文章: