gpt4 book ai didi

asp.net - 数据源不支持服务器端数据分页

转载 作者:行者123 更新时间:2023-12-04 23:23:37 24 4
gpt4 key购买 nike

我目前正在尝试为我的 gridview 进行分页,但是一旦我允许在我的 gridview 中进行分页,它就会给我这个错误:数据源不支持服务器端数据分页。

这是我的 gridview 代码:

        SqlDataReader reader = cmd.ExecuteReader();
GridView1.DataSource = reader;
GridView1.DataSourceID = null;
GridView1.Visible = true;
GridView1.AllowPaging= true;
GridView1.DataBind();
conn.Close();

最佳答案

SqlDataReader 是前向的。服务器端分页需要能够向后和向前遍历数据源。使用不同的数据源,例如 SqlDataAdapter ,支持双向遍历。

示例(根据要求):

string query = string.Empty;
SqlConnection conn = null;
SqlCommand cmd = null;
SqlDataAdapter da = null;
DataSet ds = null;

try {
query = "SELECT * FROM table WHERE field = @value";
conn = new SqlConnection("your connection string");

cmd = new SqlCommand(query, conn);
cmd.Parameters.Add("value", SqlDbType.VarChar, 50).Value = "some value";

da = new SqlDataAdapter(cmd);
ds = new DataSet();
da.Fill(ds);

if (ds.Tables.Count > 0) {
GridView1.DataSource = ds.Tables(0);
GridView1.AllowPaging = true;
GridView1.DataBind();
}
} catch (SqlException ex) {
//handle exception
} catch (Exception ex) {
//handle exception
} finally {
if (da != null) {
da.Dispose();
}
if (cmd != null) {
cmd.Dispose();
}
if (conn != null) {
conn.Dispose();
}
}

SqlDataAdapter 也是来自 System.Data.SqlClient命名空间。

关于asp.net - 数据源不支持服务器端数据分页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17606868/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com