使用编辑模板仅编辑gridview中的特定字段

时间:2016-06-01 12:55:15

标签: c# asp.net gridview roweditor

我遇到错误:对象引用未设置为对象的实例。红色文字是:

dt.Rows[row.RowIndex]["Name"] = Name;

我想在gridview中编辑数据。这是我的代码:

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 OnRowEditing(object sender, GridViewEditEventArgs e)
{
    gdview.EditIndex = e.NewEditIndex;
    this.GetProducts(0);
}

这是getproducts()

private void GetProducts(int CategoryID)
{
    ShoppingCart k = new ShoppingCart()
    {
        CategoryID = CategoryID
    };
    gdview.DataSource = null;
    gdview.DataSource = k.GetAllProducts();
    gdview.DataBind();
}

我在这里缺少什么?

另一个问题。当我点击更新链接时,它会在名称和价格字段上显示编辑文本框。但是这个名字的价值不存在?这是截图。

screenshot

这是我的HTML代码:

<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>

        <asp:TemplateField>
    <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>

1 个答案:

答案 0 :(得分:0)

首先,您必须将数据表分配给ur viewstate。那么你就可以更新该字段的值。

仅供参考,我已经通过sqldatasource对象绑定了gridview。

请使用此

检查代码
if (!Page.IsPostBack)
{
    try
    {
        gdview.DataSource = SqlDataSource1;
        gdview.DataBind();
        DataView dv = (DataView)SqlDataSource1.Select(DataSourceSelectArguments.Empty);
        DataTable dt = new DataTable();
        dt = dv.ToTable();
        ViewState["dt"] = dt;
    }
    catch(Exception ex)
    {
    }
}

-----------------你的另一种方法

private void GetProducts(int CategoryID)
{
    ShoppingCart k = new ShoppingCart()
    {
        CategoryID = CategoryID
    };
    gdview.DataSource = null;
    gdview.DataSource = ViewState["dt"];
    gdview.DataBind();
}