简体   繁体   中英

C# ASP.NET GridView UpdateRow

I have the problem with editing rows in GridView web control. I'm writing an application with ASP.NET and using NHibernate to get data from MySQL database. I have *.aspx page with GridView control. Goods.aspx code:

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

And I'm trying to edit data in next code:

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

    }

When I click "Save" button, it save into database old values from textboxes. How can I fix this problem? I know, that there are many posts with this problem. I've watched 5 posts, and did not find a solution.

Code on 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();


    }

Before calling Databind , you should assign DataSource to Gridview . tg.DataSource=dt;
tgv.DataBind();

Also , in your page load check and bind the gridview only for the first time

if (!Page.IsPostBack)
//bind Gridview()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM