繁体   English   中英

如何在C#2.0中向具有列的网格视图添加行?

[英]How to add rows to a Grid View having columns in c# 2.0?

我非常确定这个问题已经被问过并回答过几次了。 但是我再次要求它替代答案。 这是我的GridView

<asp:GridView ID="dgvGeneralBillList" runat="server" style="font-size:11px;margin:10px auto auto 30px;width:500px;" AutoGenerateColumns="False" CellPadding="4" ForeColor="#333333" OnSelectedIndexChanged="dgvGeneralBillList_SelectedIndexChanged">
            <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
            <Columns>
                <asp:BoundField DataField="BillID" HeaderText="Bill ID" Visible="False" />
                <asp:BoundField DataField="SerialNo" HeaderText="Bill No" />
                <asp:BoundField DataField="BilledWeekNo" HeaderText="Billed Week" />
                <asp:BoundField DataField="BilledWeekDate" HeaderText="Billed Date" />
                <asp:BoundField DataField="Amount" HeaderText="Amount" />
                <asp:BoundField DataField="BillStatus" HeaderText="Bill Status" />
                <asp:CommandField SelectText="Print" ShowSelectButton="True" />
            </Columns>
            <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
            <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
            <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
            <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
            <EditRowStyle BackColor="#999999" />
            <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
        </asp:GridView> 

现在,我想在运行时向此gridview添加行。 在其他文章中,他们建议定义一个DataTable ,然后将ADD my desired COLUMNS ADD rows in that TABLE到该表,然后ADD rows in that TABLE ,最后BIND the table to the GridView 是的,这可以解决问题,我也可以做到。 但是我想做的是,因为我已经在gridview中添加了列,所以我只想在其中添加行。 而不是定义一个DataTable,然后将其绑定到GridView的东西 我在下面尝试过

oOutputBill = (clsBill[])oOutput;
            if (oOutputBill.Length > 0)
            {
                for (int i = 0; i < oOutputBill.Length; i++)
                {
                    GridViewRow oRow = new GridViewRow();
                    oRow.Cells[0].Text = Convert.ToString(oOutputBill[i].BillID);
                    oRow.Cells[1].Text = Convert.ToString(oOutputBill[i].SerialNo);
                    oRow.Cells[2].Text = Convert.ToString(oOutputBill[i].BilledWeekNo);
                    oRow.Cells[3].Text = Convert.ToString(oOutputBill[i].BilledWeekDate);
                    oRow.Cells[4].Text = Convert.ToString(oOutputBill[i].Amount);
                    oRow.Cells[5].Text = Convert.ToString(oOutputBill[i].BillStatus);
                }
            }

并出现预期的错误“方法GridViewRow的任何重载都不接受0个参数”。 我该如何实现? 提前致谢。

链接可能会有所帮助。

但是我建议按DataSource绑定GridView。 只需将数据保存到会话中,而不是手动将行添加到GridView中,而是将新行添加到已保存的数据中并重新绑定网格。

例:

// If you have List of clsBill.
List<clsBill> oOutputBill = 'Filled from DB...';

// Save Data into session
Session["oOutputBill"] = oOutputBill;

// Bind your GridView
dgvGeneralBillList.DataSource = Session["oOutputBill"];
dgvGeneralBillList.DataBind();

...

// Get saved data and insert new row
List<clsBill> oOutputBill = Session["oOutputBill"] as List<clsBill>;

if(oOutputBill != null)
{
    oOutputBill.Add(new clsBill() { /* Fill class properties */ } );

    // Rebind grid
    dgvGeneralBillList.DataSource = Session["oOutputBill"];
    dgvGeneralBillList.DataBind();
}

这就是我现在使用的解决方案。 我的GridView是-

<asp:GridView ID="dgvGeneralBillList" runat="server" style="font-size:11px;margin:10px auto auto 30px;width:auto;" AutoGenerateColumns="False" CellPadding="4" ForeColor="#333333" OnSelectedIndexChanged="dgvGeneralBillList_SelectedIndexChanged">
            <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
            <Columns>
                <asp:BoundField DataField="BillID" HeaderText="Bill ID" />
                <asp:BoundField DataField="SerialNo" HeaderText="Bill No" />
                <asp:BoundField DataField="BilledWeekNo" HeaderText="Billed Week" />
                <asp:BoundField DataField="BilledWeekDate" HeaderText="Billed Date" />
                <asp:BoundField DataField="Amount" HeaderText="Amount" />
                <asp:BoundField DataField="BillStatus" HeaderText="Bill Status" />
                <asp:CommandField SelectText="Print" ShowSelectButton="True" />
            </Columns>
            <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
            <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
            <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
            <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
            <EditRowStyle BackColor="#999999" />
            <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
        </asp:GridView>

并且添加行的方式是-

oOutputBill = (clsBill[])oOutput;
            if (oOutputBill.Length > 0)
            {
                dgvGeneralBillList.DataSource = oOutputBill;
                dgvGeneralBillList.DataBind();
                dgvGeneralBillList.Visible = true;
            }

这个答案类似于定义一个表,向表中添加列,向表中添加行,最后将表绑定到gridview,但是代码更少。 这样,只有在gridview中定义的列才与数据源绑定。 请注意,如果你想要做类似的东西OnSelectedIndexChanged并尝试从GridView控件获得的数据,那么DataColumn的 HAVE在GridView中定义可见 但是,如果有人可以向我提供示例代码作为我的问题,将不胜感激。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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