/*你真让我无语,虽然我加了两个BUTTON,但改动光标的还是靠BUTTON事件里面的代码啊。你可以把那段代码拿出来放到你要使用的任何地方用的-。-!。还有啊。光标的位置我是那样设置的,就是一次上下移动一行,但是只要把那个dataGridView1.currentCell[colIndex,rolIndex]中的两个Index设置你为要改变那个CELL的Index就可以了。可以在任何地方,不一定非得按我写的用。我只是告诉你,此方法可行。而且BUTTON事件里面的代码行总共也就5,6行吧,再怎么修改也没多少工作量的你只先计算出你的CELL的Index就行了,实现起来哪有多难啊,我可以很负责任地告诉你,这真的很简单。非得吃现成的!*/
//你试一下下面的代码是不是你要的那种
public partial class Form1 : Form
{
int colIndex = -1;
int rowIndex = -1;
DataGridView dataGridView1;
Button button1,button2;
bool isButtonClick = false;
public Form1()
{
InitializeComponent();
dataGridView1 = new DataGridView();
button1 = new Button();
button2 = new Button();
this.Controls.AddRange(new Control[] { dataGridView1, button1, button2 });
this.MaximizeBox = false;
this.FormBorderStyle = FormBorderStyle.FixedDialog;
dataGridView1.Left = 30;
dataGridView1.Top = 30;
dataGridView1.Width = this.Width - 70;
dataGridView1.Height = this.Height - 160;
button1.Left = this.Left + this.Width -250;
button1.Top = this.Bottom - button1.Height - 50;
button1.Text = "上一行";
button2.Left = button1.Right + 10;
button2.Top = button1.Top;
button2.Text = "下一行";
button1.Click += new EventHandler(button1_Click);
button2.Click += new EventHandler(button2_Click);
dataGridView1.SelectionChanged += new EventHandler(dataGridView1_SelectionChanged);
}
void dataGridView1_SelectionChanged(object sender, EventArgs e)
{
if (colIndex != dataGridView1.CurrentCell.ColumnIndex)
{
colIndex = dataGridView1.CurrentCell.ColumnIndex;
rowIndex = dataGridView1.CurrentCell.RowIndex;
}
else if (!isButtonClick)
{
rowIndex = dataGridView1.CurrentCell.RowIndex;
}
else
{
isButtonClick = false;
}
}
private void Form1_Load(object sender, EventArgs e)
{
// Create new DataTable.
DataTable table = new DataTable("table");
// Declare DataColumn and DataRow variables.
DataColumn column;
DataRow row;
// Create new DataColumn, set DataType,
// ColumnName and add to DataTable.
column = new DataColumn();
column.DataType = System.Type.GetType("System.Int32");
column.ColumnName = "id";
table.Columns.Add(column);
// Create second column.
column = new DataColumn();
column.DataType = Type.GetType("System.String");
column.ColumnName = "item";
table.Columns.Add(column);
// Create new DataRow objects and add to DataTable.
for (int i = 0; i < 10; i++)
{
row = table.NewRow();
row["id"] = i;
row["item"] = "item " + i;
table.Rows.Add(row);
}
// Set to DataGrid.DataSource property to the table.
dataGridView1.DataSource = table;
dataGridView1.Focus();
dataGridView1.EditMode = DataGridViewEditMode.EditProgrammatically;
if (dataGridView1.Columns.Count > 0 && dataGridView1.Rows.Count > 0)
{
dataGridView1.CurrentCell = dataGridView1[0, 0];
colIndex = dataGridView1.CurrentCell.ColumnIndex;
rowIndex = dataGridView1.CurrentCell.RowIndex;
}
}
private void button1_Click(object sender, EventArgs e)
{
isButtonClick = true;
dataGridView1.CancelEdit();
if (rowIndex > 0)
{
dataGridView1.CurrentCell = dataGridView1[colIndex, --rowIndex];
}
else
{
rowIndex = dataGridView1.Rows.Count - 1;
dataGridView1.CurrentCell = dataGridView1[colIndex, rowIndex];
}
dataGridView1.BeginEdit(false);
}
private void button2_Click(object sender, EventArgs e)
{
isButtonClick = true;
dataGridView1.CancelEdit();
if (rowIndex < dataGridView1.Rows.Count - 1)
{
dataGridView1.CurrentCell = dataGridView1[colIndex, ++rowIndex];
}
else
{
rowIndex = 0;
dataGridView1.CurrentCell = dataGridView1[colIndex, rowIndex];
}
dataGridView1.BeginEdit(false);
}
}
如果是绑定的datagridview话,会产生一个datasource(名称会根据实际情况而变)这是类有很多方法,里面有movenext等,就是向上向下,第一条,最后一条,这样移动记录