private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
//dataGridView1.CurrentCell = dataGridView1[4, 0]; if (dataGridView1.CurrentCell.ColumnIndex == 1)
{
dataGridView1.CurrentCell = dataGridView1.Rows[dataGridView1.CurrentCell.RowIndex].Cells[4];
}
else if (dataGridView1.CurrentCell.ColumnIndex==4)
{
dataGridView1.CurrentCell = dataGridView1.Rows[dataGridView1.CurrentCell.RowIndex].Cells[1];
}
}
为什么dataGridView1列切换焦点时,不能切换到同一行?
为什么我这样写,每次按enter键的时候都是切换到下一行指定的列,而不是同一行的那列?
请大虾们赐教、帮忙。
{
//dataGridView1.CurrentCell = dataGridView1[4, 0]; if (dataGridView1.CurrentCell.ColumnIndex == 1)
{
dataGridView1.CurrentCell = dataGridView1.Rows[dataGridView1.CurrentCell.RowIndex].Cells[4];
}
else if (dataGridView1.CurrentCell.ColumnIndex==4)
{
dataGridView1.CurrentCell = dataGridView1.Rows[dataGridView1.CurrentCell.RowIndex].Cells[1];
}
}
为什么dataGridView1列切换焦点时,不能切换到同一行?
为什么我这样写,每次按enter键的时候都是切换到下一行指定的列,而不是同一行的那列?
请大虾们赐教、帮忙。
DataGridView按回车换列-同行下一单元格
private class DataGridViewEx : DataGridView
{
protected override bool ProcessDialogKey(Keys keyData)
{
Keys key = (keyData & Keys.KeyCode);
if (key == Keys.Enter)
{
return this.ProcessRightKey(keyData);
}
return base.ProcessDialogKey(keyData);
} public new bool ProcessRightKey(Keys keyData)
{
Keys key = (keyData & Keys.KeyCode);
if (key == Keys.Enter)
{
//第一种情况:只有一行,且当光标移到最后一列时
if ((base.CurrentCell.ColumnIndex == (base.ColumnCount - 1)) && (base.RowCount == 1))
{
base.CurrentCell = base.Rows[base.RowCount - 1].Cells[0];
return true;
}
//第二种情况:有多行,且当光标移到最后一列时,移到下一行第一个单元
if ((base.CurrentCell.ColumnIndex == (base.ColumnCount - 1)) && (base.CurrentCell.RowIndex < (base.RowCount - 1)))
{
base.CurrentCell = base.Rows[base.CurrentCell.RowIndex + 1].Cells[0];
return true;
} return base.ProcessRightKey(keyData);
}
return base.ProcessRightKey(keyData);
} protected override bool ProcessDataGridViewKey(KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
return this.ProcessRightKey(e.KeyData);
}
return base.ProcessDataGridViewKey(e);
}
}
protected override void OnKeyUp(System.Windows.Forms.KeyEventArgs e)
{ if (e.KeyCode == System.Windows.Forms.Keys.Enter)
{ e.Handled = true; }
} protected override bool ProcessCmdKey(ref System.Windows.Forms.Message msg, System.Windows.Forms.Keys keyData)
{
switch (keyData)
{
case System.Windows.Forms.Keys.Enter: if (dataGridView1.CurrentCell == dataGridView1.Rows[dataGridView1.CurrentCell.RowIndex].Cells[1])
{ dataGridView1.CurrentCell = dataGridView1.Rows[dataGridView1.CurrentCell.RowIndex].Cells[4];
}
else if (dataGridView1.CurrentCell.ColumnIndex == 4)
{
dataGridView1.CurrentCell = dataGridView1.Rows[dataGridView1 .CurrentCell .RowIndex+1 ].Cells[1];
}
return true;
}
return base.ProcessCmdKey(ref msg, keyData);
}