C#ASP.NET GridView UpdateRow

时间:2014-04-14 19:50:16

标签: c# asp.net gridview

我在GridView Web控件中编辑行时遇到问题。我正在用ASP.NET编写应用程序并使用NHibernate从MySQL数据库中获取数据。我有带有GridView控件的* .aspx页面。 Goods.aspx代码:

        <asp:GridView ID="tgv" runat="server" Width="915px" AutoGenerateColumns="False" 
            onrowcancelingedit="tgv_RowCancelingEdit" onrowediting="tgv_RowEditing" 
            onrowupdating="TaskGridView_RowUpdating">
            <RowStyle HorizontalAlign="Center" />
             <columns>
              <asp:boundfield datafield="Tovar_ID" headertext="ID" ReadOnly="True"/>

                    <asp:TemplateField HeaderText="Name">
                        <EditItemTemplate>
                            <asp:TextBox ID="editName" runat="server" Text='<%#Eval("Tovar_name") %>'></asp:TextBox>
                        </EditItemTemplate>
                        <ItemTemplate>
                            <asp:Label ID="Label1" runat="server" Text='<%#Eval("Tovar_name") %>'></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>

              <asp:TemplateField HeaderText="Measure">
                        <EditItemTemplate>
                            <asp:TextBox ID="editMeasure" runat="server" Text='<%#Eval("Measure") %>'></asp:TextBox>
                        </EditItemTemplate>
                        <ItemTemplate>
                            <asp:Label ID="Label2" runat="server" Text='<%#Eval("Measure") %>'></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>
              <asp:TemplateField HeaderText="Buying price">
                        <EditItemTemplate>
                            <asp:TextBox ID="editBPrice" runat="server" Text='<%#Eval("Buying_Price") %>'></asp:TextBox>
                        </EditItemTemplate>
                        <ItemTemplate>
                            <asp:Label ID="Label3" runat="server" Text='<%#Eval("Buying_Price") %>'></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>
              <asp:TemplateField HeaderText="Selling price">
                        <EditItemTemplate>
                            <asp:TextBox ID="editSPrice" runat="server" Text='<%#Eval("Selling_Price") %>'></asp:TextBox>
                        </EditItemTemplate>
                        <ItemTemplate>
                            <asp:Label ID="Label4" runat="server" Text='<%# Eval("Selling_Price") %>'></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>
                 <asp:CommandField ShowDeleteButton="True" />
                 <asp:CommandField ShowEditButton="True" />
            </columns>
        </asp:GridView>

我正在尝试编辑下一段代码中的数据:

 protected void tgv_RowEditing(object sender, GridViewEditEventArgs e)
        {
            //Set the edit index.
            tgv.EditIndex = e.NewEditIndex;
            //Bind data to the GridView control.
            tgv.DataBind();
        }

        protected void tgv_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            //Reset the edit index.
            tgv.EditIndex = -1;
            //Bind data to the GridView control.
            tgv.DataBind();
        }

        protected void tgv_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {

            GridViewRow row = tgv.Rows[e.RowIndex];
            ISession session = Transport.GetCurrentSession();
            ITransaction tx = session.BeginTransaction();
            Tovar t = session.Get<Tovar>(Convert.ToInt32(row.Cells[0].Text));
            string st = ((TextBox)tgv.Rows[e.RowIndex].FindControl("editName")).Text;
            t.tname = st;
            Response.Write(st);     
        session.Update(t);
        tx.Commit();
        session.Flush();
        Transport.CloseSession();
        tgv.EditIndex = -1;
        tgv.DataBind();

    }

当我单击“保存”按钮时,它会从文本框中保存到数据库旧值中。我该如何解决这个问题? 我知道,有很多帖子都有这个问题。我看过5个帖子,但没有找到解决方案。

PageLoad上的代码:

    protected void Page_Load(object sender, EventArgs e)
    {
        ISession session = Transport.GetCurrentSession();
        ITransaction tx = session.BeginTransaction();

        DataTable dt = new DataTable();
        DataColumn dc = new DataColumn();
        dc.ColumnName = "Tovar_ID";
        dc.DataType = System.Type.GetType("System.Int32");
        dt.Columns.Add(dc);

        dc = new DataColumn();
        dc.ColumnName = "Tovar_name";
        dc.DataType = System.Type.GetType("System.String");
        dt.Columns.Add(dc);

        dc = new DataColumn();
        dc.ColumnName = "Measure";
        dc.DataType = System.Type.GetType("System.String");
        dt.Columns.Add(dc);

        dc = new DataColumn();
        dc.ColumnName = "Buying_Price";
        dc.DataType = System.Type.GetType("System.String");
        dt.Columns.Add(dc);

        dc = new DataColumn();
        dc.ColumnName = "Selling_Price";
        dc.DataType = System.Type.GetType("System.String");
        dt.Columns.Add(dc);


        IQuery goodsQuery = session.CreateQuery
          ("select a from Tovar as a order by a.tovarid");

        foreach (Tovar a in goodsQuery.Enumerable<Tovar>())
        {
            dt.Rows.Add(a.tovarid, a.tname, a.measure, a.bprice, a.sprice);
        }

        tgv.DataSource = dt;
        tgv.DataBind();
        tx.Commit();
        session.Flush(); 
        Transport.CloseSession();


    }

1 个答案:

答案 0 :(得分:0)

在调用Databind之前,您应该将DataSource分配给Gridview。 tg.DataSource = DT;
tgv.DataBind();

此外,在您的页面加载检查并仅第一次绑定gridview

if(!Page.IsPostBack)
  //绑定Gridview()