C# Gridview中的分页-数据源不支持服务器端数据分页
我正在尝试为我的GridView使用分页,但我得到一个错误:“数据源不支持服务器端数据分页” 我不明白我做错了什么 HTML(ASPX标记): 代码隐藏(GridView):C# Gridview中的分页-数据源不支持服务器端数据分页,c#,asp.net,gridview,webforms,paging,C#,Asp.net,Gridview,Webforms,Paging,我正在尝试为我的GridView使用分页,但我得到一个错误:“数据源不支持服务器端数据分页” 我不明白我做错了什么 HTML(ASPX标记): 代码隐藏(GridView): 例外情况很可能来自这一行: gwActivity.DataSource = cmd.ExecuteReader(); 当然,ExecuteReader()方法使用的SqlDataReader不能用于绑定为GridView数据源,因为它是仅转发的读卡器,不支持分页功能。使用另一个数据源,如SqlDataAdapter,该数
例外情况很可能来自这一行:
gwActivity.DataSource = cmd.ExecuteReader();
当然,ExecuteReader()
方法使用的SqlDataReader
不能用于绑定为GridView
数据源,因为它是仅转发的读卡器,不支持分页功能。使用另一个数据源,如SqlDataAdapter
,该数据源支持双向数据读取,如下所示:
public void BindGridviewActivity()
{
/*************Connectionstring is located in Web.config ******************/
string CS = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;
using (SqlConnection con = new SqlConnection(CS))
{
SqlCommand cmd = new SqlCommand("SELECT T1.[ActivityID] FROM [BI_Planning].[dbo].[tlbActivity]", con);
con.Open();
var da = new SqlDataAdapter(cmd);
var ds = new DataSet();
da.Fill(ds);
if (ds.Tables.Count > 0)
{
gwActivity.DataSource = ds.Tables[0];
gwActivity.DataBind();
}
}
}
你好,可能是重复的,谢谢。然而,我在@ds.Tables(0)中遇到了这个错误:“非Invocable成员DataSet.Table不能像方法一样使用”哦,这只是一个输入错误。它应该是
ds.Tables[0]
。
protected void gwActivity_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
gwActivity.PageIndex = e.NewPageIndex;
BindGridviewActivity();
}
gwActivity.DataSource = cmd.ExecuteReader();
public void BindGridviewActivity()
{
/*************Connectionstring is located in Web.config ******************/
string CS = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;
using (SqlConnection con = new SqlConnection(CS))
{
SqlCommand cmd = new SqlCommand("SELECT T1.[ActivityID] FROM [BI_Planning].[dbo].[tlbActivity]", con);
con.Open();
var da = new SqlDataAdapter(cmd);
var ds = new DataSet();
da.Fill(ds);
if (ds.Tables.Count > 0)
{
gwActivity.DataSource = ds.Tables[0];
gwActivity.DataBind();
}
}
}