在Datagridview分页中没有获得显示的确切行数

时间:2013-08-02 15:07:41

标签: c# vb.net

我正在使用此代码进行分页,使用此代码我获取datagridview中的所有记录,我已设置_PateSize = 10;为什么我在datagridview中只获得10行,并且单击下一个按钮接下来的10行,我如何使这个代码工作,我出错了

public partial class ShowEngClgList : Form
    {
        private int _CurrentPage = 1;
        private int _PateSize = 10;
        public int PageSize
        {
            get
            {
                return _PateSize;
            }
            set
            {
                _PateSize = value;
            }
        }
        private DataTable _DataSource;
        public DataTable DataSource
        {
            get
            {
                return _DataSource;
            }
            set
            {
                _DataSource = value;
            }
        }
        private int _Width;
        public int ControlWidth
        {
            get
            {
                if (_Width == 0)
                    return dataGridView1.Width;
                else
                    return _Width;
            }
            set
            {
                _Width = value;
                dataGridView1.Width = _Width;
            }
        }

        private int _Height;
        public int ControlHeight
        {
            get
            {
                if (_Height == 0)
                    return dataGridView1.Height;
                else
                    return _Height;
            }
            set
            {
                _Height = value;
                dataGridView1.Height = _Height;
            }
        }
        private string _FirstButtonText = string.Empty;
        public string FirstButtonText
        {
            get
            {
                if (_FirstButtonText == string.Empty)
                    return btnFirst.Text;
                else
                    return _FirstButtonText;
            }
            set
            {
                _FirstButtonText = value;
                btnFirst.Text = _FirstButtonText;
            }
        }

        private string _LastButtonText = string.Empty;
        public string LastButtonText
        {
            get
            {
                if (_LastButtonText == string.Empty)
                    return btnLast.Text;
                else
                    return _LastButtonText;
            }
            set
            {
                _LastButtonText = value;
                btnLast.Text = _LastButtonText;
            }
        }

        private string _PreviousButtonText = string.Empty;
        public string PreviousButtonText
        {
            get
            {
                if (_PreviousButtonText == string.Empty)
                    return btnPrevious.Text;
                else
                    return _PreviousButtonText;
            }
            set
            {
                _PreviousButtonText = value;
                btnPrevious.Text = _PreviousButtonText;
            }
        }

        private string _NextButtonText = string.Empty;
        public string NextButtonText
        {
            get
            {
                if (_NextButtonText == string.Empty)
                    return btnNext.Text;
                else
                    return _NextButtonText;
            }
            set
            {
                _NextButtonText = value;
                btnNext.Text = _NextButtonText;
            }
        }
        public void DataBind(DataTable dataTable)
        {
            DataSource = dataTable;
            dataGridView1.DataSource = ShowData(1);
        }
        private DataTable ShowData(int pageNumber)
        {
            DataTable dt = new DataTable();
            int startIndex = PageSize * (pageNumber - 1);
            var result = DataSource.AsEnumerable().Where((s, k) => (k >= startIndex && k < (startIndex + PageSize)));

            foreach (DataColumn colunm in DataSource.Columns)
            {
                dt.Columns.Add(colunm.ColumnName);
            }

            foreach (var item in result)
            {
                dt.ImportRow(item);
            }

            txtPaging.Text = string.Format("Page {0} Of {1} Pages", pageNumber, (DataSource.Rows.Count / PageSize) + 1);
            return dt;
        }  

        public OleDbConnection acccon = null;
        public OleDbDataAdapter da = null;
        public DataTable dt = null;
        public ShowEngClgList()
        {
            InitializeComponent();
            try
            {
                acccon = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=db1.mdb");
                acccon.Open();
            }
            catch (Exception err)
            {

            }
            string sql = "Select EngClgName,EngClgAddress,EngClgEntranceType From EngColeges";
            da = new OleDbDataAdapter(sql, acccon);

            dt = new DataTable();
            da.Fill(dt);
            dataGridView1.DataSource = dt;
        }

        private void btnFirst_Click(object sender, EventArgs e)
        {
            if (_CurrentPage == 1)
            {
                MessageBox.Show("You are already on First Page.");
            }
            else
            {
                _CurrentPage = 1;
                dataGridView1.DataSource = ShowData(_CurrentPage);
            }
        }

        private void btnPrevious_Click(object sender, EventArgs e)
        {
            if (_CurrentPage == 1)
            {
                MessageBox.Show("You are already on First page, you can not go to previous of First page.");
            }
            else
            {
                _CurrentPage -= 1;
                dataGridView1.DataSource = ShowData(_CurrentPage);
            }
        }

        private void btnNext_Click(object sender, EventArgs e)
        {
            int lastPage = (DataSource.Rows.Count / PageSize) + 1;
            if (_CurrentPage == lastPage)
            {
                MessageBox.Show("You are already on Last page, you can not go to next page of Last page.");
            }
            else
            {
                _CurrentPage += 1;
                dataGridView1.DataSource = ShowData(_CurrentPage);
            }
        }

        private void btnLast_Click(object sender, EventArgs e)
        {
            int previousPage = _CurrentPage;
            _CurrentPage = (DataSource.Rows.Count / PageSize) + 1;

            if (previousPage == _CurrentPage)
            {
                MessageBox.Show("You are already on Last Page.");
            }
            else
            {
                dataGridView1.DataSource = ShowData(_CurrentPage);
            }
        }        

    }

提前感谢任何帮助

0 个答案:

没有答案