答案 0 :(得分:0)
我解决了这个问题,对于那些可能和我有同样问题的人,我是怎么做到的:
我使用的是扩展数据网格视图而不是普通数据视图。
public partial class DataGridViewEx : DataGridView
{
public DataGridViewEx()
{
InitializeComponent();
}
protected override void OnPaint(PaintEventArgs pe)
{
base.OnPaint(pe);
}
protected override void SetSelectedRowCore(int rowIndex, bool selected)
{
this.SuspendLayout();
base.SetSelectedRowCore(rowIndex, selected);
int index = rowIndex;
while (index < this.Rows.Count - 1 && this.Rows[index].Cells["BoxNo"].Value.ToString() == this.Rows[index + 1].Cells["BoxNo"].Value.ToString())
{
base.SetSelectedRowCore(index + 1, selected);
index = index + 1;
}
index = rowIndex;
while (index > 0 && this.Rows[index].Cells["BoxNo"].Value.ToString() == this.Rows[index - 1].Cells["BoxNo"].Value.ToString())
{
base.SetSelectedRowCore(index - 1, selected);
index = index - 1;
}
this.ResumeLayout();
}
}
然后我添加this project中的列。该项目有一个演示,所以你有一个如何使用它的例子。为了跨越一个单元格,我使用
var cell = (DataGridViewTextBoxCellEx)dataGridView1[0, index];
int nr = (int)dataGridView1.Rows[index].Cells["Nr"].Value;
cell.RowSpan = nr;
之后,从&#34; 3 spaned行&#34;按键向上/向下到另一行,我必须按下向上/向下按钮3次(从&#34; 3有效行和#34;逐一选择每一行。为了解决这个问题,我用它来键按上述活动:
private void dataGridView1_KeyDown(object sender, KeyEventArgs e)
{
if (dataGridView1.Rows.Count > 0)
{
int current = dataGridView1.CurrentRow.Index;
if (e.KeyData == Keys.Down)
{
e.Handled = true;
dataGridView1.SuspendLayout();
for (int i = current; i < dataGridView1.Rows.Count; i++)
{
if (dataGridView1.CurrentRow.Cells["BoxNo"].Value.ToString() != dataGridView1.Rows[i].Cells["BoxNo"].Value.ToString())
{
dataGridView1.CurrentCell = dataGridView1.Rows[i].Cells["BoxNo"];
break;
}
}
dataGridView1.ResumeLayout();
}
else if (e.KeyData == Keys.Up)
{
e.Handled = true;
dataGridView1.SuspendLayout();
for (int i = current; i >= 0; i--)
{
if (dataGridView1.CurrentRow.Cells["BoxNo"].Value.ToString() != dataGridView1.Rows[i].Cells["BoxNo"].Value.ToString())
{
dataGridView1.CurrentCell = dataGridView1.Rows[i].Cells["BoxNo"];
break;
}
}
dataGridView1.ResumeLayout();
}
}
}