2013-06-04 86 views
0

我遇到的問題是讓我的DataTable顯示在Gridview中。我已經在Web上搜索了答案,但是我發現的所有內容都與在執行查詢後綁定數據庫信息有關。我必須操縱從查詢中獲得的數據以顯示平均值,因此我的數據表中的所有內容都存儲在局部變量和容器中。以下是aspx頁面上gridview的代碼,以及將我的行添加到表格並將表格綁定到girdview的塊。將不勝感激爲什麼它不顯示在網絡上的任何幫助。我正在使用VS2012和.Net v4將數據表綁定到Gridview - 不顯示在網頁上

DataRow newRow; 
       for (int i = 0; i < portcount; i++) 
       { 
        newRow = averageTable.NewRow(); 
        newRow["port_num"] = i+1; 
        newRow["port_status"] = currentstat[i]; //this is a list<Int32> 
        newRow["average_uptime"] = (statusCalc[i]/counter) * 100; 
        //statusCalc is an int array 
        averageTable.Rows.Add(newRow); 
       } 
       statusRdr.Close(); 

       GridView completeView = new GridView(); 
       this.completeView.Visible = true; 
       completeView.DataSource = averageTable; 
       completeView.DataBind(); 

<asp:Content ID="bodyContnet" ContentPlaceHolderID="cphContent" runat="server"> 
     <asp:GridView ID="completeView" runat="server" AutoGenerateColumns="False" ViewStateMode="Enabled" ForeColor="WhiteSmoke" Width="51%" Height="204px"> 
      <Columns> 
       <asp:BoundField DataField="port_num" HeaderText="Port" /> 
       <asp:BoundField DataField="port_status" HeaderText="Status" /> 
       <asp:BoundField DataField="average_uptime" HeaderText="Average Uptime" /> 
      </Columns> 
     </asp:GridView> 
     <br /> 
     <br /> 
     <asp:Label ID="message" runat="server" ForeColor="WhiteSmoke" Text="Port Status Table"></asp:Label> 
    </asp:Content> 

回答

1

您不需要重新創建GridView,它已經在頁面上並且默認情況下是可見的。刪除這樣做的行,以便您的代碼如下所示:

DataRow newRow; 
for (int i = 0; i < portcount; i++) 
{ 
    newRow = averageTable.NewRow(); 
    newRow["port_num"] = i+1; 
    newRow["port_status"] = currentstat[i]; //this is a list<Int32> 
    newRow["average_uptime"] = (statusCalc[i]/counter) * 100; 
    //statusCalc is an int array 
    averageTable.Rows.Add(newRow); 
} 
statusRdr.Close(); 

completeView.DataSource = averageTable; 
completeView.DataBind(); 
+0

謝謝!完美地工作。 – pham307

+0

完全沒問題=] – Sean