我正在尝试将一些行从一个数据表复制到另一个数据表,这里是我使用的代码: -
DataTable dt_query;
public void show_query(int page)
{
conn.Open();
SqlDataAdapter sda_query = new SqlDataAdapter("select * from query where resolved='NO'", conn);
conn.Close();
DataTable dt = new DataTable();
sda_query.Fill(dt);
dt_query.Clear();
for (int i = (page - 1) * 5; i < page * 5 && i<dt.Rows.Count; i++)
{
dt_query.ImportRow(dt.Rows[i]);
}
GridView2.DataSource = dt_query;
GridView2.DataBind();
}
但是gridview最终显示5个空白行,只有自动生成的选择按钮。这有什么不对?
gridview的HTML如下: -
<asp:GridView ID="GridView2"
runat="server" Width="820px" AutoGenerateSelectButton="True"
onselectedindexchanged="GridView2_SelectedIndexChanged" CellPadding="4"
ForeColor="#333333" GridLines="None">
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
<EditRowStyle BackColor="#999999" />
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#E9E7E2" />
<SortedAscendingHeaderStyle BackColor="#506C8C" />
<SortedDescendingCellStyle BackColor="#FFFDF8" />
<SortedDescendingHeaderStyle BackColor="#6F8DAE" />
</asp:GridView>
答案 0 :(得分:0)
尝试在填充方法后关闭连接。另外,尝试在没有dt_query表的情况下执行此操作。
public void show_query(int page)
{
conn.Open();
SqlDataAdapter sda_query = new SqlDataAdapter("select * from query where resolved='NO'", conn);
DataTable dt = new DataTable();
sda_query.Fill(dt);
conn.Close();
GridView2.DataSource = dt;
GridView2.DataBind();
}