将新行添加到gridview在转发器控件中不起作用

时间:2019-05-08 10:28:54

标签: c# asp.net gridview row repeater

我在中继器控件内部使用Gridview选择要计费的项目。

但是当我尝试在gridview中添加新行时,它说:

“对象引用未设置为对象的实例。”

我正在使用gridview显示项目详细信息,并有一个按钮可以向gridview添加新行。

下面是我的C#代码,用于添加新行:

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

这是我的aspx标记:

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

如果所有数据在前一行中保持不变,我想在gridview中添加新行!

2 个答案:

答案 0 :(得分:0)

 DataRow drCurrentRow = null; 

必须这样做

DataRow drCurrentRow=  dtCurrentTable.NewRow();

因为如果在内部循环中使用NeWRow,那么如果您的代码未到达内部循环,则datarow对象将保留为null,在这种情况下,将保留

   dtCurrentTable.Rows.Add(drCurrentRow);

运行此代码会尝试添加datarow实例,但未创建该实例,因为该实例开始时分配为null。 或者您可以尝试

 dtCurrentTable.Rows.Add(drCurrentRow);

也保持此内部循环

答案 1 :(得分:0)

我从这里得到解决: AspForums