我非常确定这个问题之前曾被多次提出并回答过。但我再次要求其他答案。这是我的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
,最后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个参数”。我怎样才能做到这一点?提前谢谢。
答案 0 :(得分:1)
This链接可能会有所帮助。
但我建议通过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();
}
答案 1 :(得分:0)
这就是我现在使用的解决方案。 我的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 TO 在gridview中 DEFINED 和 VISIBLE 。但是,如果有人能够提供示例代码作为我的问题,我们将非常感激。