我很確定這個問題以前已經被問及過幾次了。但我再次要求提供一個替代答案。這裏是我的GridView如何將行添加到在C#2.0中具有列的網格視圖?
<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
,最後是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參數」。我怎樣才能做到這一點?提前致謝。
謝謝@Vano,您的回覆 – 2012-03-05 07:28:02