ASP.net - 强类型数据集用法

时间:2014-03-05 13:25:52

标签: asp.net sql gridview ado.net dataset

我有一个非常简单的SQL产品表,我想用asp.net GridView编辑。

1)我为products表创建了一个带有Insert / Select / Update命令的DataSet / Adapter。 2)在Default.aspx中,我有以下网格:

   <asp:GridView ID="GridView1" AutoGenerateColumns="false" runat="server" OnRowEditing="GridView1_RowEditing" OnRowUpdating="GridView1_RowUpdating">
    <Columns>
        <asp:CommandField ShowEditButton="True" />
        <asp:BoundField DataField="ProductName" HeaderText="Produkt" />
        <asp:BoundField DataField="ProductManufacturer" HeaderText="Hersteller" />
        <asp:BoundField DataField="ProductPrice" HeaderText="Preis" />
    </Columns>
</asp:GridView> <asp:GridView ID="GridView1" AutoGenerateColumns="false" runat="server" OnRowEditing="GridView1_RowEditing" OnRowUpdating="GridView1_RowUpdating">
    <Columns>
        <asp:CommandField ShowEditButton="True" />
        <asp:BoundField DataField="ProductName" HeaderText="Produkt" />
        <asp:BoundField DataField="ProductManufacturer" HeaderText="Hersteller" />
        <asp:BoundField DataField="ProductPrice" HeaderText="Preis" />
    </Columns>
</asp:GridView>

3)背后的代码:

public partial class Default : System.Web.UI.Page
{
    public static dsProducts ds = new dsProducts();

    protected void GridBind()
    {
        ProductTableAdapter tableAdapter = new ProductTableAdapter();
        tableAdapter.Fill(ds.Product);
        GridView1.DataSource = ds.Product;
        GridView1.DataBind();
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            GridBind();
        }
    }

    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        this.GridView1.EditIndex = e.NewEditIndex;
        GridBind();

    }

    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        if (ds.HasChanges()==true)
        {
            ProductTableAdapter adapter = new ProductTableAdapter();
            adapter.Update(ds.Product.Rows[e.RowIndex]);
        }

        // Exit edit mode
        this.GridView1.EditIndex = -1;
        // Update the grid
        GridBind();

    }

}

问题是,ds.hasChanges总是错误的。因此,在编辑并单击“更新”后,它只会加载原始数据。

问题是什么?

0 个答案:

没有答案