简体   繁体   中英

Add new row to gridview not working inside repeater control

I'm using Gridview inside a repeater control to select items for billing.

but when I try to add new row in the gridview it says:

"Object reference not set to an instance of an object."

I'm using gridview to show item details and have a button to add new row to the gridview.

Below is my c# code to add new row:

private void AddNewRowToGrid()
    {
        foreach (RepeaterItem rptrItems in rptrTables.Items)
        {
            GridView gvItems = (GridView)rptrTables.FindControl("grdOrder");
            int rowIndex = 0;
            if (ViewState["CurrentTable"] != null)
            {
                DataTable dtCurrentTable = (DataTable)ViewState["CurrentTable"];
                DataRow drCurrentRow = dtCurrentTable.NewRow();
                if (dtCurrentTable.Rows.Count > 0)
                {
                    for (int i = 1; i <= dtCurrentTable.Rows.Count; i++)
                    {
                        DropDownList box1 = (DropDownList)gvItems.Rows[rowIndex].Cells[1].FindControl("drpItemname");
                        TextBox box2 = (TextBox)gvItems.Rows[rowIndex].Cells[2].FindControl("lblUnitPrice");
                        TextBox box3 = (TextBox)gvItems.Rows[rowIndex].Cells[3].FindControl("lblQuantity");
                        TextBox box4 = (TextBox)gvItems.Rows[rowIndex].Cells[4].FindControl("lblLineTotal");
                        drCurrentRow = dtCurrentTable.NewRow();
                        drCurrentRow["RowNumber"] = i + 1;
                        dtCurrentTable.Rows[i - 1]["ItemName"] = box1.Text;
                        dtCurrentTable.Rows[i - 1]["UnitPrice"] = box2.Text;
                        dtCurrentTable.Rows[i - 1]["Quantity"] = box3.Text;
                        dtCurrentTable.Rows[i - 1]["LineTotal"] = box4.Text;
                        rowIndex++;
                    }
                    dtCurrentTable.Rows.Add(drCurrentRow);
                    ViewState["CurrentTable"] = dtCurrentTable;
                    gvItems.DataSource = dtCurrentTable;
                    gvItems.DataBind();
                }
            }
            else
            {
                Response.Write("ViewState is null");
            }
            SetPreviousData();
        }        
    }

And here is my aspx Markup:

<div class="card-body">
                                                    <div class="row">
                                                        <div class="col-md-12">
                                                            <div class="form-group">
                                                                <asp:GridView ID="grdOrder" runat="server" AutoGenerateColumns="false" AllowPaging="true" CssClass="table table-bordered table-hover table-responsive" GridLines="None" PageSize="10" OnRowDataBound="gvRowDataBound" ShowFooter="true">
                                            <Columns>
                                                 <asp:CommandField ShowDeleteButton="true" ControlStyle-CssClass="btn btn-danger fa fa-trash" DeleteText="" HeaderText="Remove" />
                                                <asp:BoundField DataField="RowNumber" HeaderText="Sl. No." />
                                                <asp:TemplateField HeaderText="Item Name">
                                                    <ItemTemplate>
                                                        <asp:DropDownList ID="drpItemname" runat="server" CssClass="form-control select2" OnSelectedIndexChanged="GetItemDetails" AutoPostBack="true"></asp:DropDownList>
                                                    </ItemTemplate>
                                                </asp:TemplateField>
                                                <asp:TemplateField HeaderText="Price">
                                                    <ItemTemplate>
                                                        <asp:TextBox ID="lblUnitPrice" runat="server" CssClass="form-control" Text='<%# Eval("UnitPrice")%>'></asp:TextBox>
                                                    </ItemTemplate>
                                                </asp:TemplateField>
                                                <asp:TemplateField HeaderText="Qty">
                                                    <ItemTemplate>
                                                        <asp:TextBox ID="lblQuantity" runat="server" TextMode="Number" CssClass="form-control" Text='<%# Eval("Quantity")%>'></asp:TextBox>
                                                    </ItemTemplate>
                                                </asp:TemplateField>                                               
                                                <asp:TemplateField HeaderText="Line Total">
                                                    <ItemTemplate>
                                                        <asp:TextBox ID="lblLineTotal" runat="server" CssClass="form-control" Text="0"></asp:TextBox>
                                                    </ItemTemplate>
                                                    <FooterStyle HorizontalAlign="Right" />
                                                <FooterTemplate>
                                                    <asp:Button ID="ButtonAdd" runat="server" CssClass="btn btn-primary" Text="Add" OnClick="AddItem" CausesValidation="False" />
                                                </FooterTemplate>                                                   
                                                </asp:TemplateField>                                        
                                            </Columns>
                                        </asp:GridView>
                                                            </div>
                                                        </div>
                                                    </div>
                                                </div>
                                                <div class="card-footer">
                                                    <div class="pull-right">
                                <asp:Button ID="btnSubmit" runat="server" CausesValidation="false" TabIndex="2" CssClass="btn btn-primary" Text="Print KOT" />
                                <asp:Button ID="Button1" runat="server" CausesValidation="false" TabIndex="2" CssClass="btn btn-primary" Text="Print Final Bill" />
                                <asp:Button ID="Button2" runat="server" CausesValidation="false" TabIndex="2" CssClass="btn btn-primary" Text="Complete Order" />
                            </div>
                                                </div>

I want to add new row to the gridview provided all the data remains intact in the previous row!

 DataRow drCurrentRow = null; 

at the place of this you must do

DataRow drCurrentRow=  dtCurrentTable.NewRow();

because if you use NeWRow inside loop then in case your code doesn't reach inside loop your datarow object will be left with null and in that case when

   dtCurrentTable.Rows.Add(drCurrentRow);

This code run it tries to add datarow instance but that is not created because that assigned to null in beginning. Or you can try

 dtCurrentTable.Rows.Add(drCurrentRow);

keeping this inside loop too

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