gpt4 book ai didi

c# - 将数据绑定(bind)到 gridview。如何使用分页?

转载 作者:行者123 更新时间:2023-11-30 17:03:08 24 4
gpt4 key购买 nike

它弹出一个异常,说我不能在服务器端使用分页。

conn.Open(); 
string querstring = "select * from gt_transaction_log where LogTimeStamp between '2013-09-19 07:06:00.077' and '2013-09-19 10:28:25.163' ";
SqlCommand cmd = new SqlCommand(querstring, conn);
GridView1.EmptyDataText = "no record found";
GridView1.DataSource = cmd.ExecuteReader();
GridView1.DataBind();

GridView1.AllowPaging = true;
GridView1.PageSize = 5;

最佳答案

您不能对 DataReader 使用分页。所以问题出在这一行:

GridView1.DataSource = cmd.ExecuteReader();

您应该使用 Dataset 或使用 DataAdapterDatatable 填充 GridView。

例子:

//使用DataTable

string querstring = "select * from gt_transaction_log where LogTimeStamp between
'2013-09-19 07:06:00.077' and '2013-09-19 10:28:25.163' ";
SqlDataAdapter adapter = new SqlDataAdapter(querstring , conn);
DataTable dt = new DataTable();
adapter.Fill(dt);
GridView1.DataSource=dt;
GridView1.DataBind();

//使用DataSet

string querstring = "select * from gt_transaction_log where LogTimeStamp between
'2013-09-19 07:06:00.077' and '2013-09-19 10:28:25.163' ";
SqlDataAdapter adapter = new SqlDataAdapter(querstring , conn);
DataSet ds = new DataSet();
adapter.Fill(ds, "Table_Name"); // you can supply a table name
GridView1.DataSource=ds;
GridView1.DataBind();

关于c# - 将数据绑定(bind)到 gridview。如何使用分页?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18947491/

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