datagridview的分页和datagridview的数据库更新
datagridview的分页
private int pagecount;
private int pagecurrent;
private int pagesize;
private DataTable dt;
private DataTable dt1;
private int ncurrent;//记录当前的行数
private int nmax;//记录多少行
public void Page_Load(object sender, EventArgs e)
{
DataTable datatable="数据库中查询出来的表";//有主键,方便下面的数据库的更新
dt=datatable;
InitBind();
datagridview1.DataSource=dt1;
}
public void InitBind()
{
pagesize=20;//一页包含20行数据
nmax=dt.Rows.Count;
pagecount=nmax/pagesize;
if((nmax%pagesize)>0)
pagecount++;
ncurrent = 0;
pagecurrent=1;
loadData();
}
public void loadData()
{
datagridview1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
int nStartPos = 0;//当页开始行数
int nEndPos = 0;//当页结束行数
DataTable dtt =dt.Clone();
if (pagecurrent == pagecount)
nEndPos = nmax;
else
nEndPos = pagesize * pagecurrent;
nStartPos = _nCurrent;
total.Text = _pageCount.ToString();//total是记录总页数的label
whichpage.Text = Convert.ToString(_pageCurrent);//whichpage是记录当前页的label
for (int i = nStartPos; i < nEndPos; i++)
{
dtt.ImportRow(_dt.Rows[i]);
_nCurrent++;
}
dt1= dtt;
}
//到首页的代码
publicvoid FirstPage_Click(object sender, EventArgs e)
{
if(whichpage .Text!=""&&total.Text!="")
{
pagecurrent = 1;
ncurrent = pagesize * (pagecurrent - 1);//页面开始的首记录行
loadData();
datagridview1.DataSource = dt1;
}
}
//到最后一页
public void lastpage_Click(object sender, EventArgs e)
{
if (whichpage.Text != "" && total.Text != "")
{
pagecurrent = Convert.ToInt32(total.Text); //当前页号设置为 总页数
ncurrent = pagesize*(pagecurrent - 1); //页面开始的首记录行 == 每页显示行数 * (当前页号减1 )
loadData();
datagridview1.DataSource = dt1;
}
}
//上一页的代码
public void xqpage_Click(object sender, EventArgs e)
{
if (whichpage.Text != "" && total.Text != "")
{
pagecurrent--;
if (pagecurrent <= 0)
{
MessageBox.Show("已经是第一页,请点击“下一页”查看");
return;
}
else
{
ncurrent = pagesize*(pagecurrent - 1);//页面开始的首记录行
}
loadData();
datagridview1.DataSource = dt1;
}
}
//下一页代码
public void nextpage_Click(object sender, EventArgs e)
{
pagecurrent++;
if (pagecurrent > pagecount)
{
MessageBox.Show("已经是最后一页了,请点击查看");
return;
}
else
{
ncurrent = pagesize * (pagecurrent - 1);
}
loadData();
datagridview1.DataSource = dt1; }
//跳转到多少页代码按钮
private void myButton27_Click(object sender, EventArgs e)
{
if (tzymtex.Text.Trim() == "")//tzymtex是记录跳转多少页的textbox
{
MessageBox.Show("所要跳转的页数不能为空!");
}
else
{
int i = 0;
if (int.TryParse(tzymtex.Text.Trim(), out i))
{
if (Convert.ToInt32(tzymtex.Text.Trim()) > pagecount)
{
MessageBox.Show("跳转页码超出!");
tzymtex.Text = "";
}
else
{
pagecurrent = Convert.ToInt32(tzymtex.Text.Trim());
nCurrent = pagesize * (pagecurrent- 1);
loadData();
//dataGridView4.ReadOnly = true;
datagridview1.DataSource = dt1;
tzymtex.Text = "";
}
}
else
{
MessageBox.Show("请输入数字!");
tzymtex.Text = "";
}
}
}
//更新数据库我用的是SQLiteDataAdapter
//这个是数据库基类中的方法 SQLiteDBHelper是基类的类名
public static SQLiteDataAdapter Adapte;
public DataTable ExecuteAdapte(string sql, SQLiteParameter[] parameters)
{
using (var connection = new SQLiteConnection(connectionString))
{
using (var command = new SQLiteCommand(sql, connection))
{
if (parameters != null)
{
command.Parameters.AddRange(parameters);
}
Adapte = new SQLiteDataAdapter(command);
var data = new DataTable();
Adapte.Fill(data);
SQLiteCommandBuilder sqLiteCommandBuilder = new SQLiteCommandBuilder(Adapte);
Adapte.InsertCommand = sqLiteCommandBuilder.GetInsertCommand();
Adapte.UpdateCommand = sqLiteCommandBuilder.GetUpdateCommand();
Adapte.DeleteCommand = sqLiteCommandBuilder.GetDeleteCommand();
//Adapte.Update(data);
return data;
}
}
}
//下面的这个"数据库中查询出来的表"调用的上面的方法返回的datatable
DataTable datatable="数据库中查询出来的表";//有主键,方便下面的数据库的更新
public void button1_Click(object sender, EventArgs e)
{
SQLiteDBHelper.Adapte.Update(_zhdt);//就这样就行了
}