现在用C#做的WINFORM平台。。涉及到了datagridview分页问题。
如果数据量小的话还看不出什么问题,当数据量大的时候就会出现如下问题。比如 每页显示10条数据,如果我跳转到第98页,有时候就会出现98页是空白的情况,或者连续两页数据相同的情况,比如98 99两页的内容是相同的。不知道为什么。求大神

解决方案 »

  1.   

    private void CrossThreadFlush() //线程newthread3 启动事件,实现短信记录刷新  
            {
                while (true)
                {
                    out_pagecount();      //计算需要多少页显示
                    t_interfaceupdate();  //界面刷新
                    t_sqltodgv();        //绑定数据
                    Thread.Sleep(MessageModem.Properties.Settings.Default.pageUDTime);
                }
            }        private int out_historycount( ) //计算历史记录中记录的条数 ,返回结果 historycount
            {
                SqlConnection sqlConnection = new SqlConnection(MessageModem.Properties.Settings.Default.ConnString);
                sqlConnection.Open();
                SqlCommand mysqlcommand2 = new SqlCommand("select count(*)  from t_history", sqlConnection);
                int historycount = (int)mysqlcommand2.ExecuteScalar();
                sqlConnection.Close();            return historycount;
            }        private void out_pagecount( )   //计算历史记录中数据需要多少页显示,赋值给pageCount
            {
                try
                {
                    int recordcount = out_historycount( );
                    pageCount = (pageSize + recordcount - 1) / pageSize;
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.TargetSite.ToString()+" :"+ex.Message, "出错啦!");
                }
            }        private void t_interfaceupdate()  //标签界面刷新
            {
                if (this.panel1.InvokeRequired)
                {
                    FlushClient fc = new FlushClient(t_interfaceupdate);
                    this.Invoke(fc);
                }
                else
                {
                    this.label5.Text = string.Format("{0}/{1}页", currentPage + 1, pageCount);
                    if (currentPage <= 0)
                        this.button_front.Enabled = false;
                    else
                        this.button_front.Enabled = true;
                    if (currentPage >= pageCount - 1)
                        this.button_behind.Enabled = false;
                    else
                        this.button_behind.Enabled = true;
                }
            }        private void t_sqltodgv() // 将DataGridView与数据表绑定。
            {
                if (this.panel1.InvokeRequired)//等待异步
                {
                    FlushClient fc = new FlushClient(t_sqltodgv);
                    this.Invoke(fc);//通过代理调用刷新方法
                }
                else
                {
                    SqlConnection sqlConnection = new SqlConnection(MessageModem.Properties.Settings.Default.ConnString); 
                    sqlConnection.Open();
                    string  mysql = string.Format("select top {0} * from t_history  where 时间 not in "
                                               +"(select top {1} 时间 from t_history order by 时间 desc) order by 时间 desc",pageSize,pageSize * currentPage);
                    SqlCommand mysqlcommand = new SqlCommand( mysql, sqlConnection );
                    SqlDataAdapter data = new SqlDataAdapter();
                    data.SelectCommand = mysqlcommand;
                    DataSet data2 = new DataSet();
                    data.Fill(data2);
                    dataGridView1.DataSource = data2.Tables[0];
                    this.dataGridView1.Columns[0].FillWeight = 27; //设置每行相对宽度,百分比
                    this.dataGridView1.Columns[1].FillWeight = 12;
                    this.dataGridView1.Columns[2].FillWeight = 12;
                    this.dataGridView1.Columns[3].FillWeight = 17;
                    this.dataGridView1.Columns[4].FillWeight = 32;                sqlConnection.Close();
                }
            }