且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

编辑使用编辑模板,在GridView中只有特定的领域

更新时间:2023-10-04 19:05:22

首先,你必须将数据表分配给UR视图状态。那么你就可以能够更新字段的值。

仅供参考,我已经在GridView SqlDataSource的通过对象绑定。

请检查code本

 如果(!Page.IsPostBack)
{
    尝试
    {
        gdview.DataSource = SqlDataSource1;
        gdview.DataBind();
        DataView的DV =(数据视图)SqlDataSource1.Select(DataSourceSelectArguments.Empty);
        DataTable的DT =新的DataTable();
        DT = dv.ToTable();
        的ViewState [DT] = DT;
    }
    赶上(异常前)
    {
    }
}

-----------------您的另一种方法。

 私人无效的GetProducts(INT类别ID)
{
    我的购物K =新的购物车()
    {
        类别id =类别ID
    };
    gdview.DataSource = NULL;
    gdview.DataSource =的ViewState [DT];
    gdview.DataBind();
}

i am having an error : Object reference not set to an instance of an object. and the red text is:

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

i want to edit data in my gridview. here is my code:

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

here is the getproducts()

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

what am i missing here?

Another question. When i click on the update link, it shows the edit textbox on the Name, and Price fields. But the value on the name is not there? here is a screenshot.

here is my html code:

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

First you have to assign the datatable to ur viewstate. then you can able to update the value of the field.

FYI, I have bind the gridview by sqldatasource object.

Please check the code with this

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)
    {
    }
}

-----------------your other method

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