#endregion //获取记录总数 #region protected void gvBind(int pageSize, int currentPage) { SqlConnection myConn = new SqlConnection(); myConn.ConnectionString = "server=**;uid=sa;pwd=**;database=**"; myConn.Open(); string strSql = "select top "+pageSize +" * from student where 学生编号 not in(select top "+(currentPage - 1)* pageSize+ " 学生编号 from student order by 学生编号 ASC) order by 学生编号 ASC"; SqlDataAdapter adapter = new SqlDataAdapter(strSql, myConn); DataSet ds = new DataSet(); adapter.Fill(ds); this.GridView1.DataSource = ds.Tables[0].DefaultView; this.GridView1.DataBind(); myConn.Dispose(); adapter.Dispose(); ds.Dispose();
private DataSet ds;
private void FrmWorks_Load(object sender, EventArgs e)
{
//ds = MT.GetTotal();
//grid.DataSource = ds.Tables[0];
BM = grid.BindingContext[grid.DataSource, grid.DataMember];
BM.PositionChanged += new EventHandler(BM_PositionChanged);
BM.CurrentItemChanged += new EventHandler(BM_CurrentItemChanged);
DisplayCustomsPosition();
} private void BM_CurrentItemChanged(object sender, EventArgs e)
{
DisplayCustomsPosition();
}
private void BM_PositionChanged(object sender, EventArgs e)
{
DisplayCustomsPosition();
}
private void DisplayCustomsPosition()
{
iText.Caption = String.Format("{0} / {1}", BM.Position + 1, BM.Count);
}
private void iFirst_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
{
BM.Position = 0;
}
private void iPrev_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
{
BM.Position--;
}
private void iNext_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
{
BM.Position++;
}
private void iEnd_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
{
BM.Position = BM.Count - 1;
}
CurrencyManager cmOrders = (CurrencyManager) this.BindingContext[dataset, "Orders"];都差不多吧.
private void toolStripButton3_Click(object sender, EventArgs e)
{
int i = dataGridView1.SelectedRows[0].Index;
if (i != 0)
{
dataGridView1.Rows[i].Selected = false;
dataGridView1.Rows[i - 1].Selected = true;
}
else
{
MessageBox.Show("已经是第一项了!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
} private void toolStripButton4_Click(object sender, EventArgs e)
{
int i = dataGridView1.SelectedRows[0].Index;
if (i<dataGridView1 .Rows .Count -1)
{
dataGridView1.Rows[i].Selected = false;
dataGridView1.Rows[i + 1].Selected = true;
}
else
{
MessageBox.Show("已经是最后一项了!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
我是这样做的
BindingSource bs = new BindingSource(); string strsql = "select * from 表";
DataTable table = da.GetTable(strsql);//根据sql 获得表
bs.DataSource = table;
dataGridView1.DataSource = bs;
bindingNavigator1.BindingSource = bs;
用bindingNavigator1 导航控件 只要把它和 dataGridView1 绑定到一起就可以了
而且点击dataGridView1的时候 bindingNavigator1也连动再在dataGridView1 的事件中
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
textBox1.Text = dataGridView1["序号", dataGridView1.CurrentRow.Index].Value.ToString();
}
//获取记录总数
#region
protected void gvBind(int pageSize, int currentPage)
{
SqlConnection myConn = new SqlConnection();
myConn.ConnectionString = "server=**;uid=sa;pwd=**;database=**";
myConn.Open();
string strSql = "select top "+pageSize +" * from student where 学生编号 not in(select top "+(currentPage - 1)*
pageSize+ " 学生编号 from student order by 学生编号 ASC) order by 学生编号 ASC";
SqlDataAdapter adapter = new SqlDataAdapter(strSql, myConn);
DataSet ds = new DataSet();
adapter.Fill(ds);
this.GridView1.DataSource = ds.Tables[0].DefaultView;
this.GridView1.DataBind();
myConn.Dispose();
adapter.Dispose();
ds.Dispose();
}
#endregion
//分页显示数据 protected void lnkbtnOne_Click(object sender, EventArgs e) //第一页
{
ViewState["currentPage"] = "1";
this.LabPage.Text = "[1]";
gvBind(pageSize, int.Parse(ViewState["currentPage"].ToString()));
pageAll = GetRecordCount();
pageCount = pageAll / pageSize;
this.LabBackPage.Text = pageCount.ToString(); ;
}
protected void lnkbtnUp_Click(object sender, EventArgs e) //上一页
{
currenPage = int.Parse(ViewState["currentPage"].ToString());
if (currenPage - 1 > 0) currenPage--;
this.LabPage.Text = currenPage.ToString();
ViewState["currentPage"] = currenPage;
gvBind(pageSize, int.Parse(ViewState["currentPage"].ToString()));
}
protected void lnkbtnNext_Click(object sender, EventArgs e) // 下一页
{
currenPage = int.Parse(ViewState["currentPage"].ToString());
pageAll = GetRecordCount();
pageCount = pageAll / pageSize;
if (currenPage < pageCount) currenPage++;
this.LabPage.Text = currenPage.ToString();
ViewState["currentPage"] = currenPage;
gvBind(pageSize, int.Parse(ViewState["currentPage"].ToString()));
}
protected void lnkbtnBack_Click(object sender, EventArgs e) //最后一页
{
pageAll = GetRecordCount();
pageCount = pageAll / pageSize;
if (pageCount > 0)
ViewState["currentPage"] = pageCount;
this.LabPage.Text = pageCount.ToString();
gvBind(pageSize, int.Parse(ViewState["currentPage"].ToString()));
}这是我以前自己写的,其实分页有很多种方式,但效率最高的还是调用存储过程。
{
if (dataGridView1.CurrentCell.RowIndex != dataGridView1.RowCount - 1)
{
this.dataGridView1.CurrentCell = this.dataGridView1[this.dataGridView1.CurrentCell.ColumnIndex, this.dataGridView1.CurrentCell.RowIndex + 1];
}
else
{
MessageBox.Show( "已到达末记录 ", "警告 ",MessageBoxButtons.OK,MessageBoxIcon.Information);
}
} private void button10_Click(object sender, EventArgs e)//上一条记录按钮
{
if (dataGridView1.CurrentCell.RowIndex != 0)
{
this.dataGridView1.CurrentCell = this.dataGridView1[this.dataGridView1.CurrentCell.ColumnIndex, this.dataGridView1.CurrentCell.RowIndex - 1];
}
else
{
MessageBox.Show( "已到达首记录 ", "警告 ", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}