c#常用表格控件dataGridView的分页显示的案例分享

1.让垂直滚动条消失

1

dataGridView1.ScrollBars =ScrollBars.None;

2.确定每页显示多少行

如果表格是固定大小,此步骤可省略,直接用行数进行下面的操作即可,此方法针对表格大小可调整自适应的时候,通过计算获取能显示的最大行。

1

int rowCount;//每一页多少行int maxCount;//所有页数private void uc_table_Resize(object sender, EventArgs e){    this.Invalidate();    rowCount = (dataGridView1.Height - dataGridView1.ColumnHeadersHeight) / dataGridView1.RowTemplate.Height;    maxCount = (int)Math.Ceiling(dataGridView1.Rows.Count / rowCount * 1.0);}

以上方法是针对表格内容还没有添加,如果表格已经有内容,是无法通过模板(RowTemplate)修改高度的。

那可以使用:

(1)ColumnHeadersHeaderSize属性设为 EnableResizing

(2)ColumnHeadersHeader 的值改为 需要的高度

(3)RowTemplate属性下的Height,把值也设置为 需要的高度

3.控制表格定位

使用FirstDisplayedScrollingRowIndex属性调整显示内容。

1

dataGridView1.FirstDisplayedScrollingRowIndex = 0;//定位到第一页dataGridView1.FirstDisplayedScrollingRowIndex = dataGridView1.Rows.Count- rowCount;//定位到最后一页

例如:

以上就是简单利用dataGridView的分页显示,总结来说就是计算每一页显示多少行,计算dataGridView每次定位到哪里。

补充:datagridview分页读取,定时循环翻页

在VB中使用C# 语言,datagridview和计时器。

这里我假设datagridview一页显示五行,两秒变换一次。

拖一个时间控件到页面上,设置Interval属性为2000.

我写好了注释和流程,大家都能看懂的

using System.Data;using System.Data.SqlClient;//使用到这个类,先添加命名空间,访问数据库using System.Drawing;using System.Text;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Windows.Forms;using System.Xml;namespace WindowsFormsApp1{    public partial class Form1 : Form     {        private DataSet myDataSet = new DataSet();        private int DataRowsCountTotal = 0;//总行数        private int PageCount = 0;//满页页数        private int residualRowsCount = 0;//除去满页,最后一页的数据,余数        private int TakeCount = 0;//当前页页数        public Form1()        {            InitializeComponent();        }        //public string myConnectString = "server=192.168.50.50;uid=sa;pwd=123;database=111";  //使用IP地址连接数据库192.168.50.50        private void Form1_Load(object sender, EventArgs e)        {            string myConnectString = "Server=AUTOBVT-EMECSND;User Id=sa;Pwd=123;DataBase=111;Persist Security Info=True";            //    "Persist Security Info=True";// Data Source数据源    initial catalog数据库  Persist Security Info是否保存安全信息                        SqlConnection myConn = new SqlConnection(myConnectString);       //创建数据库连接对象            myConn.Open();                          //打开数据库            SqlCommand myComm = new SqlCommand("select * from Table_1 order by ID asc", myConn);            //用sql语句实现查询SELECT * FROM 表名 WHERE ID NOT IN (SELECT TOP(I) ID FROM 表名)            SqlDataAdapter myAdap = new SqlDataAdapter();            myAdap.SelectCommand = myComm;            myAdap.Fill(myDataSet, "Table_1");            DataRowsCountTotal = myDataSet.Tables[0].Rows.Count;//=MyDataSet中的数据行数            PageCount = DataRowsCountTotal / 5;            //求商  PageCount满页的页数            residualRowsCount = DataRowsCountTotal % 5;     //取余residualRowsCount最后一页的数据行数            DataGrieDataBind(0);            timer1.Enabled = true;//计时器是否正在运行              int a = (this.Size.Width - label1.Size.Width) / 2;          //控件的水平居中位置            //int b = (this.Size.Height - 2 * label1.Size.Height) / 2 ;//控件的垂直居中位置            label1.Location = new Point(a, 1);                      //位置坐标            label2.Text = DateTime.Now.ToLongTimeString();          //显示当前的时间            label2.Location = new Point(a, this.Size.Height-30);    //位置最下,居中            dataGridView1.AutoGenerateColumns = false; //不允许bai自动创建列,            dataGridView1.RowHeadersVisible = false;//删除最左边一列,把控件的 RowHeadVisible属性设置为false            this.FormBorderStyle = FormBorderStyle.None;    //无边框            this.WindowState = FormWindowState.Maximized;   //窗体最大化            //dataGridView1.Dock = DockStyle.Fill;            //控件最大化            dataGridView1.AllowUserToAddRows = false;       //不同意用户添加行,这样就不会出现最后一行空白行,大多数时候表格只是用来展示数据而非用户录入数据(录入数据神马的用EXCEL更方便吧)            /*    设置属性AutoSizeColumnsMode = Fill;列表头的宽度均匀分配,填满表格空间。                  设置属性BackgroundColor = White; 背景色设置为白色            timer1.Interval = 2000; 定时器间隔时间            */                      //toolStripStatusLabel1.Text = "登录用户:" + Form1.strName;//显示登录用户,显示登录时间        }        /*  DataSet.Tables[0].Rows[0][1]表示DataSet中第一张表(因为Tables[0]就是第一张表的意思)中第一行(Rows[0][]) 第二列(Rows[][1])的数据。                 DataSet.Tables["tableName"] 是指定获取特定的表名。如果DataSet只有一张表,则为DataSet.Tables[0].  */        private void DataGrieDataBind(int TakeCount)        {            DataTable myDt = new DataTable();       //创建一个DataTable的对象,虚拟表            myDt = myDataSet.Tables[0].Clone();     //克隆表                                                    //  myDt.Clear();//清除行信息为0            if (TakeCount + 1 > PageCount)//如果 当前页数 >满页页数,即最后一页            {                for (int i = 5 * TakeCount; i PageCount)//如果  当前页 >满页页数            {                TakeCount = 0;//显示第一页            }            DataGrieDataBind(TakeCount);        }        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)        {        } } } 逻辑结构大概是这个private int DataRowsCountTotal = 0;//总行数        private int PageCount = 0;//满页页数        private int residualRowsCount = 0;//除去满页,最后一页的数据,余数        private int TakeCount = 0;//当前页页数DataRowsCountTotal = myDataSet.Tables[0].Rows.Count;//=MyDataSet中的数据行数        PageCount = DataRowsCountTotal / 5;            //求商  PageCount满页的页数        residualRowsCount = DataRowsCountTotal % 5;     //取余residualRowsCount最后一页的数据行数``第几行  0   0-4       1页     1-5    1    5-9       2页     6-10    2    10-14    n页   5n/5n+4   n+1页  5n+1/5n+5    尾页   5n/N      尾页+1

未经允许不得转载:木盒主机 » c#常用表格控件dataGridView的分页显示的案例分享

赞 (0)

相关推荐

    暂无内容!