如何-的更新数据,在-ASP-的Net-GridView控件

时间:2015-09-16 10:01:00

标签: c# asp.net gridview

无法转换类型为#System; Web.UI.LiteralControl'的对象键入' 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>
&#13;
&#13;
&#13;

3 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

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

而不是假设索引只是使用您已经找到的GridViewRow找到控件。

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

答案 2 :(得分:0)

它将是

TextBox textprice = (TextBox)Gridview2.Rows[e.Rowindex].FindControl("TextBox1");