datagridview如何设置分页
时间: 2023-05-31 15:03:15 浏览: 121
1. 首先,在项目中添加一个DataGridView控件,并设置其数据源,例如:
```
dataGridView1.DataSource = myDataTable;
```
2. 接着,在窗体上添加一个分页控件,例如:
```
private void Form1_Load(object sender, EventArgs e)
{
BindingSource bs = new BindingSource();
bs.DataSource = myDataTable;
dataGridView1.DataSource = bs;
BindingNavigator bn = new BindingNavigator(true);
bn.BindingSource = bs;
this.Controls.Add(bn);
}
```
3. 在分页控件中设置每页显示的记录数和总记录数,例如:
```
bn.CountItem.Text = "总记录数:" + myDataTable.Rows.Count.ToString() + "条";
bn.PositionItem.Visible = false;
bn.Items.Add("每页显示:");
ToolStripComboBox tscb = new ToolStripComboBox();
tscb.Items.Add("10");
tscb.Items.Add("20");
tscb.Items.Add("50");
tscb.SelectedIndex = 0;
tscb.SelectedIndexChanged += new EventHandler(tscb_SelectedIndexChanged);
bn.Items.Add(tscb);
```
4. 在分页控件中添加翻页按钮,并设置其事件处理程序,例如:
```
bn.Items.Add("第");
ToolStripTextBox tstb = new ToolStripTextBox();
tstb.Text = "1";
tstb.Width = 50;
bn.Items.Add(tstb);
bn.Items.Add("页");
bn.Items.Add("共" + (myDataTable.Rows.Count / 10 + 1).ToString() + "页");
bn.Items.Add(new ToolStripButton("首页"));
bn.Items.Add(new ToolStripButton("上一页"));
bn.Items.Add(new ToolStripButton("下一页"));
bn.Items.Add(new ToolStripButton("末页"));
bn.Items.Add(new ToolStripLabel("跳转到"));
ToolStripTextBox tstb2 = new ToolStripTextBox();
tstb2.Text = "1";
tstb2.Width = 50;
bn.Items.Add(tstb2);
bn.Items.Add(new ToolStripButton("确定"));
bn.Items.Add(new ToolStripLabel("页"));
bn.ItemClicked += new ToolStripItemClickedEventHandler(bn_ItemClicked);
```
5. 在事件处理程序中,根据用户的操作计算出当前页码和每页显示的记录数,并重新绑定数据源,例如:
```
private void tscb_SelectedIndexChanged(object sender, EventArgs e)
{
ToolStripComboBox tscb = (ToolStripComboBox)sender;
int pageSize = int.Parse(tscb.SelectedItem.ToString());
BindingSource bs = (BindingSource)dataGridView1.DataSource;
bs.DataSource = myDataTable;
bs.Filter = "";
bs.Sort = "";
dataGridView1.DataSource = bs;
BindingNavigator bn = (BindingNavigator)dataGridView1.BindingNavigator;
bn.CountItem.Text = "总记录数:" + myDataTable.Rows.Count.ToString() + "条";
bn.PositionItem.Visible = false;
bn.Items[7].Text = "共" + (myDataTable.Rows.Count / pageSize + 1).ToString() + "页";
bn.Items[3].Text = "第1页";
if (myDataTable.Rows.Count > pageSize)
{
bs.DataSource = myDataTable.AsEnumerable().Take(pageSize).CopyToDataTable();
}
}
private void bn_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
{
BindingSource bs = (BindingSource)dataGridView1.DataSource;
int pageSize = int.Parse(((ToolStripComboBox)bs.Position).SelectedItem.ToString());
int pageCount = myDataTable.Rows.Count / pageSize + 1;
int currentPage = int.Parse(bs.PositionItem.Text.Substring(2, bs.PositionItem.Text.Length - 3));
switch (e.ClickedItem.Text)
{
case "首页":
currentPage = 1;
break;
case "上一页":
currentPage--;
break;
case "下一页":
currentPage++;
break;
case "末页":
currentPage = pageCount;
break;
case "确定":
currentPage = int.Parse(bs.PositionItem.Text.Substring(2, bs.PositionItem.Text.Length - 3));
int gotoPage = int.Parse(((ToolStripTextBox)bs.PositionItem).Text);
if (gotoPage >= 1 && gotoPage <= pageCount)
{
currentPage = gotoPage;
}
break;
}
bs.DataSource = myDataTable.AsEnumerable().Skip((currentPage - 1) * pageSize).Take(pageSize).CopyToDataTable();
bs.PositionItem.Text = "第" + currentPage.ToString() + "页";
}
```
阅读全文