2012-03-04 46 views
0

我很確定這個問題以前已經被問及過幾次了。但我再次要求提供一個替代答案。這裏是我的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參數」。我怎樣才能做到這一點?提前致謝。

回答

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必須是在GridView DEFINED可見。但如果任何人都可以提供示例代碼作爲我的問題,它將不勝感激。

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

謝謝@Vano,您的回覆 – 2012-03-05 07:28:02

相關問題