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