0

エラーが発生しています: オブジェクト参照がオブジェクトのインスタンスに設定されていません。赤いテキストは次のとおりです。

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

グリッドビューでデータを編集したい。ここに私のコードがあります:

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();
}

ここで何が欠けていますか?

別の質問です。更新リンクをクリックすると、名前と価格フィールドに編集テキストボックスが表示されます。しかし、名前の値はそこにありませんか? これがスクリーンショットです。

スクリーンショット

ここに私の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>
4

1 に答える 1

0

まず、データテーブルをビューステートに割り当てる必要があります。その後、フィールドの値を更新できます。

参考までに、グリッドビューを sqldatasource オブジェクトでバインドしました。

これでコードを確認してください

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();
}
于 2016-06-01T14:05:49.440 に答える