c#如何实现DataGridView翻页显示

冰雨在线 2012-04-07 05:23:40
c#如何实现DataGridView翻页显示。比如想在DataGridView当前界面只显示10条记录,其他记录通过按钮点击翻页来显示?
...全文
1011 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
许愿的泪水 2012-04-08
  • 打赏
  • 举报
回复
同意楼上
Csharp_琪 2012-04-08
  • 打赏
  • 举报
回复
6楼的很详细了,主要还是sql语句,剩下的就是逻辑的问题了,加油喽!
mingcsharp 2012-04-08
  • 打赏
  • 举报
回复
和六楼差不多,主要是在sql语句下下功夫
  • 打赏
  • 举报
回复
点击上一页或下一页,从数据库读取第i条到第i+10条数据就行
冰雨在线 2012-04-08
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 的回复:]
可以进行sql语句进行设置:

1.先新建一个窗体,一个DataGridView控件、两个label控件、两个Button控件
2.代码如下:
[/Quote]

谢谢 睡醒了试试
拓-------轩 2012-04-07
  • 打赏
  • 举报
回复 1
可以进行sql语句进行设置:

1.先新建一个窗体,一个DataGridView控件、两个label控件、两个Button控件
2.代码如下:



using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace _2012_4_7
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
static string str = "server=.;database=shuDB;uid=sa;pwd=accp";
SqlConnection conn = new SqlConnection(str);
DataSet set = new DataSet();
SqlDataAdapter adapter;

int index = 0; //sql语句中的索引
int yeshu = 1; //下一页显示的页数
int sum = 0; //总页码

//加载前6行数据
private void Form1_Load(object sender, EventArgs e)
{
this.label1.Text = "第" + yeshu.ToString() + "页";
string sql = @"select top 6 * from shu s";
GetDataSet(sql);

string sql2 = @"select MAX(shuid)/6 from shu";
conn.Open();
SqlCommand comm = new SqlCommand(sql2,conn);
sum =(int)comm.ExecuteScalar();
if (sum == 0) { return; }
this.label2.Text = "总页数" + sum.ToString();
conn.Close();
}

//上一页
private void button1_Click(object sender, EventArgs e)
{
if (index == 0 || yeshu == 0) { return; } //页数是0则返回
index--;
yeshu--;
this.label1.Text = "第" + yeshu.ToString() + "页";
string sql = @"select top 6 * from shu where shuid not in
(select top (6*" + index + ") shuid from shu)";
GetDataSet(sql);

}

//下一页
private void button2_Click(object sender, EventArgs e)
{
if (yeshu==sum) //如果翻的页数等于总页数
{
MessageBox.Show("已经是最后一页!");
return;
}
index++;
yeshu++;
this.label1.Text = "第" + yeshu.ToString()+"页";
string sql = @"select top 6 * from shu where shuid not in
(select top (6*"+index+") shuid from shu)";
GetDataSet(sql);
}

//绑定
public void GetDataSet(string sql)
{
adapter = new SqlDataAdapter(sql, conn);
if (set.Tables["stu"] != null)
{
set.Tables["stu"].Clear();
}
adapter.Fill(set, "stu");

this.dataGridView1.DataSource = set.Tables["stu"];
}
}
}

冰雨在线 2012-04-07
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 的回复:]
这个问题的核心是sql语句,LZ先试着在sqlserver里面写出查询第11到20的记录,要是能查出来的话,下面就好说了。
[/Quote]
试试看
Csharp_琪 2012-04-07
  • 打赏
  • 举报
回复
这个问题的核心是sql语句,LZ先试着在sqlserver里面写出查询第11到20的记录,要是能查出来的话,下面就好说了。
orochiheart 2012-04-07
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 的回复:]
引用 1 楼 的回复:
同问 帮顶!

不i会吧
[/Quote]
我主要是学习。。
冰雨在线 2012-04-07
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 的回复:]
同问 帮顶!
[/Quote]
不i会吧
orochiheart 2012-04-07
  • 打赏
  • 举报
回复
同问 帮顶!

111,126

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧