Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/312.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将数据绑定到gridview。如何使用分页?_C#_Asp.net_Gridview - Fatal编程技术网

C# 将数据绑定到gridview。如何使用分页?

C# 将数据绑定到gridview。如何使用分页?,c#,asp.net,gridview,C#,Asp.net,Gridview,它弹出了一个异常,说我不能在服务器端使用分页 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.EmptyDataTex

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

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; 

在设计视图上,单击Gridview>允许分页
或者改为使用SQLdatasource,然后允许分页

您不能将分页与
数据读取器
一起使用。所以这条线的问题是:

GridView1.DataSource=cmd.ExecuteReader()

您应该使用
数据集
数据表
使用
数据适配器
填充GridView

例如:

//使用数据表

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();
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();
//使用数据集

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();
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();

必须指定是否要在页面的UI中分页,即:

<asp:GridView ID="grid" runat="server" AllowPaging="true" PageSize="5" OnPageIndexChanging="grid_PageIndexChanging" />

其中BindGrid()方法是绑定网格的方法。

您可以尝试使用以下代码

String constring = "Data Source=dsdsdsds;Initial Catalog=table;User Id=uid;Password=pass";
SqlConnection conqav = new SqlConnection(constring);
String takeffty = "select top 10 * from table";
conqav.Open();
SqlCommand comqav = new SqlCommand(takeffty,conqav);
GridView1.DataSource = comqav.ExecuteReader();
GridView1.DataBind();
conqav.Close();

尝试将最后两个命令移到DataBind方法上方。查看以下示例: