Search code examples
c#asp.netgridviewupdatingnested-gridview

Asp GridView not updating


I am using a nested GridView. When I click edit, the items appear for editing in a texbox but the new values are not reflecting in the rowupdating. I tried inspecting it through browser also and even then it is fetching old values.

 <asp:GridView ID="GVInvoiceDet" runat="server" Width="100%" AutoGenerateColumns="False" EnableViewState="False"
            HorizontalAlign="Left" BorderStyle="Outset" OnRowCancelingEdit="GVInvoiceDet_RowCancelingEdit"
            OnRowEditing="GVInvoiceDet_RowEditing" OnRowDataBound="GVInvoiceDet_RowDataBound" ShowFooter="True"
            OnRowCommand="GVInvoiceDet_RowCommand" OnRowUpdating="GVInvoiceDet_RowUpdating"
            OnRowDeleting="GVInvoiceDet_RowDeleting" OnRowDeleted="GVInvoiceDet_RowDeleted"
            OnRowUpdated="GVInvoiceDet_RowUpdated" AllowSorting="True" DataSourceID="DSItemGrid" DataKeyNames="invoiceno,itemcd">
            <HeaderStyle BackColor="#0000ff" ForeColor="Yellow" />
            <FooterStyle BackColor="#FFFFCC" ForeColor="#330099" />
            <EditRowStyle BackColor="#FFFFCC" ForeColor="#330099" />
            <AlternatingRowStyle BackColor="#FE9CD5" />

            <Columns>
                <asp:TemplateField>
                    <ItemTemplate>
                        <a href="javascript:expandcollapse('div<%# Eval("itemcd") %>', 'one');">
                            <img id="imgdiv<%# Eval("itemcd") %>" alt="Click to show/hide Tax Details <%# Eval("itemcd") %>" border="0" src="Images/plus.png" />
                        </a>
                    </ItemTemplate>
                    <ItemStyle HorizontalAlign="Left" Width="15px" />
                </asp:TemplateField>
                <asp:TemplateField ItemStyle-Width="10%" HeaderText="Item Code" SortExpression="itemcd">
                    <ItemTemplate>
                        <asp:Label ID="lblItemCode" runat="server" Text='<%# Eval("itemcd")%>'></asp:Label>
                    </ItemTemplate>
                    <EditItemTemplate>
                        <asp:TextBox ID="txtItemCode" Width="90%" runat="server" Text='<%# Eval("itemcd")%>' Enabled="false"></asp:TextBox>
                    </EditItemTemplate>
                    <FooterTemplate>
                        <asp:TextBox ID="txtItemCode" Width="90%" runat="server" Text='' Enabled="false"></asp:TextBox>
                    </FooterTemplate>
                </asp:TemplateField>
      <asp:TemplateField ItemStyle-Width="5%" HeaderText="Quantity">
                    <ItemTemplate>
                        <asp:Label ID="lblItemQty" runat="server" Text='<%# Eval("quantity")%>'></asp:Label>
                    </ItemTemplate>
                    <EditItemTemplate>
                        <asp:TextBox ID="txtItemQty" Width="90%" runat="server" Text='<%# Bind("quantity") %>'></asp:TextBox>
                    </EditItemTemplate>
                    <FooterTemplate>
                        <asp:TextBox ID="txtItemQty" Width="90%" MaxLength="100" runat="server"></asp:TextBox>
                    </FooterTemplate>
                </asp:TemplateField>
        <asp:CommandField ShowEditButton="True" HeaderText="Edit" />
                <asp:TemplateField HeaderText="Delete" ItemStyle-Width="5%">
                    <ItemTemplate>
                        <asp:LinkButton ID="linkDeleteInvoiceItem" CommandName="DeleteItem" runat="server">Delete</asp:LinkButton>
                    </ItemTemplate>
                    <FooterTemplate>
                        <asp:LinkButton ID="linkAddInvoiceItem" CommandName="AddItem" runat="server">Add</asp:LinkButton>
                    </FooterTemplate>
                </asp:TemplateField>
      </Columns>
        </asp:GridView>
       <asp:SqlDataSource ID="DSItemGrid" runat="server" ConnectionString="<%$ ConnectionStrings:GSTCS %>" ProviderName="<%$ ConnectionStrings:GSTCS.ProviderName %>"
            SelectCommand="select invoiceno, dt.itemcd itemcd, mast.itemdesc itemdesc, mast.hsncd hsncd, dt.quantity quantity, 
            mast.unitrate unitrate, dt.unitvalue unitvalue, mast.itempercentage itempercentage   
            from GSTINVOICEDT dt join GSTITEMMASTER mast on (dt.itemcd = mast.itemcd and dt.unitcode = mast.unitcode and dt.projectcode = mast.projectcode) 
            where dt.unitcode = :unitCode and dt.projectCode = :projCode and dt.invoiceno = :invoiceNo order by invoiceno, itemcd"
            UpdateCommand="UPDATE GSTINVOICEDT set  quantity = :quantity ,unitRate =:unitRate, unitValue=:unitValue 
                           WHERE invoiceno = :invoiceno and unitcode= :unitcode and projectcode = :projectcode 
                           and itemcd = :itemcd"
            OnUpdating="DSItemGrid_Updating">
            <SelectParameters>
                <asp:SessionParameter DefaultValue="05" Name="unitCode" SessionField="unit" />
                <asp:SessionParameter DefaultValue="0501" Name="projCode" SessionField="project" />
                <asp:SessionParameter DefaultValue="INVOICENO" Name="invoiceNo" SessionField="invoiceNo" Type="String" />
            </SelectParameters>
            <UpdateParameters>
                <asp:Parameter Name="itemcd" Type="String" />
                <asp:Parameter Name="quantity" Type="Int32" />
                <asp:Parameter Name="unitRate" Type="Decimal" />
                <asp:Parameter Name="unitValue" Type="Decimal" />
                <asp:Parameter Name="invoiceno" Type="String" />
                <asp:SessionParameter Name="unitcode" SessionField="unit" />
                <asp:SessionParameter Name="projectcode" SessionField="project" />                
            </UpdateParameters>
        </asp:SqlDataSource>

    </div>

snapshot of the frontend grid showing new Quantity value 500 Gridview

inspect Snapshot of the inspect element of browser showing old Quantity value 50.

Partial Code for row updating is also getting old value for quantity

 protected void GVInvoiceDet_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            GridView temp = (GridView)sender;
            //Get the values stored in the text boxes
            string itemCode = ((TextBox)temp.Rows[e.RowIndex].FindControl("txtItemCode")).Text;
            string quantity = ((TextBox)temp.Rows[e.RowIndex].FindControl("txtItemQty")).Text;
            string unitRate = ((TextBox)temp.Rows[e.RowIndex].FindControl("txtItemUnitRate")).Text;
            string unitValue = ((TextBox)temp.Rows[e.RowIndex].FindControl("txtItemUnitValue")).Text;    
            string unit = Session["unit"].ToString();
            string proj = Session["project"].ToString();

Solution

  • Here you can try this code to update row

     protected void GVInvoiceDet_RowUpdating(object sender, GridViewRowEventArgs e)
     {
        string itemCode= (TextBox)GVInvoiceDet.Rows[e.RowIndex].FindControl("txtItemCode")).Text;
        string quantity = (TextBox)GVInvoiceDet.Rows[e.RowIndex].FindControl("txtItemQty")).Text;
        string unitRate = ((TextBox)GVInvoiceDet.Rows[e.RowIndex].FindControl("txtItemUnitRate")).Text;
        string unitValue = ((TextBox)GVInvoiceDet.Rows[e.RowIndex].FindControl("txtItemUnitValue")).Text;    
        string unit = Session["unit"].ToString();
        string proj = Session["project"].ToString();
        GVInvoiceDet.EditIndex = -1;
        // call bind grid method here
     }