//这是我的代码,具体的已经发到你的邮箱了,希望对你有点帮助~~
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.Sql;
using System.Data.SqlClient;
namespace GDV_Page_Row_Win
{
public partial class Form1 : Form
{
int pageSize = 0; //每页显示行数
int nMax = 0; //总记录数
int pageCount = 0; //页数=总记录数/每页显示行数
int pageCurrent = 0; //当前页号
int nCurrent = 0; //当前记录行
DataSet ds = new DataSet();
DataTable dtInfo = new DataTable();
string strConn = "Server=.;Trusted_Connection=SSPI;Database=DBTRUCK;Enlist=false;";
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection(strConn);
conn.Open();
string strSql = "SELECT * FROM CarFee";
SqlDataAdapter sda = new SqlDataAdapter(strSql, conn);
sda.Fill(ds, "ds");
conn.Close();
dtInfo = ds.Tables[0];
InitDataSet();
}
//用当前页面数据填充DataGridView
private void InitDataSet()
{
pageSize = 5; //设置页面行数
nMax = dtInfo.Rows.Count;
pageCount=(nMax/pageSize); //计算出总页数
if ((nMax % pageSize) > 0)
pageCount++;
pageCurrent = 1; //当前页数从1开始
nCurrent = 0; //当前记录数从0开始
LoadData();
}
private void LoadData()
{
int nStartPos = 0; //当前页面开始记录行
int nEndPos = 0; //当前页面结束记录行
DataTable dtTemp = dtInfo.Clone(); //克隆DataTable结构框架
if (pageCurrent == pageCount)
nEndPos = nMax;
else
nEndPos = pageSize * pageCurrent;
nStartPos = nCurrent;
lblPageCount.Text ="总页数:"+ pageCount.ToString();
txtCurrentPage.Text = Convert.ToString(pageCurrent);
//从元数据源复制记录行
for (int i = nStartPos; i < nEndPos; i++)
{
dtTemp.ImportRow(dtInfo.Rows[i]);
nCurrent++;
}
bdsInfo.DataSource = dtTemp;
dgvInfo.DataSource = bdsInfo;
}
//显示上一页
private void abovePageBtn_Click(object sender, EventArgs e)
{
pageCurrent--;
if (pageCurrent <= 0)
{
MessageBox.Show("已经是第一页,请点击“下一页”查看!");
return;
}
else
{
nCurrent = pageSize * (pageCurrent - 1);
}
LoadData();
}
//显示下一页
private void nextPageBtn_Click(object sender, EventArgs e)
{
pageCurrent++;
if (pageCurrent > pageCount)
{
MessageBox.Show("已经是最后一页,请点击“上一页”查看!");
return;
}
else
{
nCurrent=pageSize*(pageCurrent-1);
}
LoadData();
}
//选中上一行记录
private void aboveRowBtn_Click(object sender, EventArgs e)
{
try
{
dgvInfo.CurrentRow.Selected = true;
int i = dgvInfo.CurrentRow.Index ;
if (i > 0)
{
dgvInfo.CurrentCell=dgvInfo.Rows[i-1].Cells[0];
dgvInfo.CurrentRow.Selected = true;
}
else
{
MessageBox.Show("这已经是第一行!");
}
}
catch(Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
//选中下一行记录
private void nextRowBtn_Click(object sender, EventArgs e)
{
if (dgvInfo.CurrentRow.Cells[0].Value.ToString() != "")
{
try
{
dgvInfo.CurrentRow.Selected = true;
int i = dgvInfo.CurrentRow.Index;
if (i < pageSize - 1)
{
dgvInfo.CurrentCell = dgvInfo.Rows[i + 1].Cells[0];
dgvInfo.CurrentRow.Selected = true;
}
else
{
MessageBox.Show("这已经是最后一行!");
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
else
{
MessageBox.Show("这已经是最后一行!");
}
}
//显示整行数据
private void dgvInfo_SelectionChanged(object sender, EventArgs e)
{
string str = "";
for (int i = 0; i < dgvInfo.CurrentRow.Cells.Count; i++)
{
str = str + dgvInfo.CurrentRow.Cells[i].Value.ToString();
}
textBox1.Text = str;
}
}
}
全部输入完了之后,你一定需要做什么动作的吧?比如,输入完了之后,点击一个按钮保存,在这个按钮事件中加一句myGridView.EndEdit();,cellvaluechanged事件的代码不变
http://xinyuan178.blog.163.com/blog/static/49799652200942342122524/
亲 先把问题贴出来哟,这样才好为你具体问题具体作答唷
DatagridView.EndEdit();