0

我遇到的问题是让我的 DataTable 显示在 Gridview 中。我在网上搜索了答案,但我发现的所有内容都与执行查询后的绑定数据库信息有关。我必须操纵从查询中获得的数据以显示平均值,因此我的数据表中的所有内容都存储在局部变量和容器中。这是aspx页面上gridview的代码以及将我的行添加到表中并将表绑定到girdview的块。任何关于为什么它没有显示在网络上的帮助将不胜感激。我正在使用 VS2012 和 .Net v4

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>
4

1 回答 1

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();
于 2013-06-04T18:15:24.617 回答