C# 如何在栅格视图中编辑和更新行值?

C# 如何在栅格视图中编辑和更新行值?,c#,asp.net,C#,Asp.net,我有这样一个gridview: <asp:GridView ID="gvProducts" runat="server" AutoGenerateEditButton="True" AutoGenerateColumns="False" OnRowEditing="gvProducts_RowEditing" OnRowUpdating="gvProducts_RowUpdating" CellPadding="4" Fore

我有这样一个gridview:

<asp:GridView ID="gvProducts" runat="server" AutoGenerateEditButton="True" AutoGenerateColumns="False"
                OnRowEditing="gvProducts_RowEditing" OnRowUpdating="gvProducts_RowUpdating" CellPadding="4"
                ForeColor="#333333" GridLines="None">
                <RowStyle BackColor="#FFFBD6" ForeColor="#333333" />
                <Columns>
                    <asp:TemplateField>
                        <HeaderTemplate>
                            <asp:Label ID="lblPID" runat="server" Text="Product ID"></asp:Label>
                        </HeaderTemplate>
                        <ItemTemplate>
                            <asp:Label ID="lblProdID" runat="server" Text='<%#Eval("ProductID")%>'></asp:Label>
                        </ItemTemplate>
                        <EditItemTemplate>
                            <asp:TextBox ID="txtProdID" runat="server" Text='<%#Eval("ProductID")%>'></asp:TextBox>
                        </EditItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField>
                        <HeaderTemplate>
                            <asp:Label ID="lblPName" runat="server" Text="Product Name"></asp:Label>
                        </HeaderTemplate>
                        <ItemTemplate>
                            <asp:Label ID="lblProdName" runat="server" Text='<%#Eval("ProductName")%>'></asp:Label>
                        </ItemTemplate>
                        <EditItemTemplate>
                            <asp:TextBox ID="txtProdName" runat="server" Text='<%#Eval("ProductName")%>'></asp:TextBox>
                        </EditItemTemplate>
                    </asp:TemplateField>
                </Columns>
                <FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
                <PagerStyle BackColor="#FFCC66" ForeColor="#333333" HorizontalAlign="Center" />
                <SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy" />
                <HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
                <AlternatingRowStyle BackColor="White" />
            </asp:GridView>
试试这个

protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{ 
TextBox txtProdID = (TextBox)gvProducts.Rows[e.RowIndex].FindControl("txtProdID");
TextBox txtProdName = (TextBox)gvProducts.Rows[e.RowIndex].FindControl("txtProdName");


//Call update method
Product.Update(txtProdId,txtProdName);

gvProducts.EditIndex = -1;
//Refresh the gridviwe
BindGrid(); 
}

您需要检查
e.NewValues
字典中的更新数据

下面是一个示例,GridView模板绑定到CategoryName。 单击“编辑”按钮时触发OnRowUpdate。 在RowUpdate事件处理程序中,它获取textbox绑定到的CategoryName数据

注意:不要在正常模式下使用文本框,而是使用标签

<asp:GridView ID='GridView1' runat='server' DataKeyNames='CategoryID' OnRowUpdating='HandleOnGridViewRowUpdating'
        DataSourceID='ObjectDataSource1' AutoGenerateColumns='false'>
        <Columns>
            <asp:CommandField ShowEditButton="true" />
            <asp:TemplateField>
                <HeaderTemplate>
                    <asp:Label ID='CategoryName' Text='Category' runat='server'></asp:Label>
                </HeaderTemplate>
                <ItemTemplate>
                    <asp:TextBox ID='CategoryNameTextbox' Text='<%# Bind("CategoryName") %>' runat='server'></asp:TextBox>
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView>

你的帖子中没有设置标记的格式吗?@Vijay:下一次当然可以了,因为我不知道,谢谢!可汗;请现在检查,因为我已经更新了我的答案。我希望现在它能解决你的问题。我测试了你的代码,得到了修改过的txtProdID(ProductID,尽管让用户编辑ProductID是个坏主意)。您是如何绑定到数据源的?在找到控件后(如您所做的),我无法在控件中找到新值(在您的解决方案中为txtProdID)。您好,我在Page_Load事件中犯的错误。我每次都绑定gridview,现在我使用if(!isPostBack)条件将它限制为一次。代码现在运行良好。您的示例中的产品对象是什么?我在任何地方都没有看到它被声明……我在下面的语句字符串newCategoryName=e.NewValues[“CategoryName”].ToString()中得到错误:“对象引用未设置为对象的实例”;我将列设置为CategoryName您是否删除了if(..!=null)检查?如果数据绑定到gridview,则gridview标记不会说它绑定到CategoryName。请尝试gridview在您的ProductID中绑定的内容。@vijay:hi vijay,e.NewValues中的值为Null我测试了您的代码,我确实得到了修改后的txtProdID(ProductID,尽管让用户编辑ProductID是个坏主意)。如何绑定到数据源?@Vijay:我同意启用任何Id都是可编辑的坏主意,但在进入我的模块之前,我只创建了一个包含ProdID和prodName的表。我正在调用一个函数来绑定数据。连接ot sql db并获取数据集的。我把它和我的gridview绑定在一起
<asp:GridView ID='GridView1' runat='server' DataKeyNames='CategoryID' OnRowUpdating='HandleOnGridViewRowUpdating'
        DataSourceID='ObjectDataSource1' AutoGenerateColumns='false'>
        <Columns>
            <asp:CommandField ShowEditButton="true" />
            <asp:TemplateField>
                <HeaderTemplate>
                    <asp:Label ID='CategoryName' Text='Category' runat='server'></asp:Label>
                </HeaderTemplate>
                <ItemTemplate>
                    <asp:TextBox ID='CategoryNameTextbox' Text='<%# Bind("CategoryName") %>' runat='server'></asp:TextBox>
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView>
public void HandleOnGridViewRowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        if (e.NewValues["CategoryName"] != null)
        {
            String newCategoryName = e.NewValues["CategoryName"].ToString();
            // process the data; 
        }
    }