c# – 如何以编程方式从数据网格视图中的一个单元格移动到另一个单元格?
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了c# – 如何以编程方式从数据网格视图中的一个单元格移动到另一个单元格?,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3473字,纯文字阅读大概需要5分钟。
内容图文
![c# – 如何以编程方式从数据网格视图中的一个单元格移动到另一个单元格?](/upload/InfoBanner/zyjiaocheng/816/5c2d58ec1b464d2599bcf69c558cf097.jpg)
我只需要允许一个字符输入到可编辑的datagridview单元格中(每隔一列,奇数编号的单元格都是可编辑的);如果用户在其中一个单元格中添加第二个字符,则光标应该向下移动到下一个单元格并将第二个值放在那里(再次按下该键再次向下移动,依此类推).如果在网格的底部(第12行),它应该移动到第0行并且还向右移动两列.
我试过这样做:
private void dataGridViewPlatypus_KeyDown(object sender, KeyEventArgs e) {
var currentCell = dataGridViewPlatypus.CurrentCell;
int currentCol = currentCell.ColumnIndex;
int currentRow = currentCell.RowIndex;
if (currentCell.Value.ToString().Length > 0) {
if (currentRow < 11) {
dataGridViewPlatypus.CurrentCell.RowIndex = currentRow+1;
} else if (currentRow == 11) {
currentCell.RowIndex = 0;
currentCell.ColumnIndex = currentCell.ColumnIndex + 2;
dataGridViewPlatypus.CurrentCell = currentCell;
}
}
}
…但我得到的错误信息是RowIndex和ColumnIndex无法分配,因为它们是只读的.
那我该怎么做呢?
警告:我知道如果当前位于最后一个可编辑列的底部,我还必须添加逻辑以移动到第1列.
UPDATE
从tergiver的回答来看,这是我到目前为止所得到的,但我不知道如何进入下一个细胞.
protected override bool ProcessCmdKey(ref Message msg, Keys keyData) {
if (this.ActiveControl == dataGridViewPlatypus)
{
var currentCell = dataGridViewPlatypus.CurrentCell;
if (currentCell.Value.ToString().Length == 1)
{
;//Now what?
}
}
return base.ProcessCmdKey(ref msg, keyData);
}
更新2
谢谢大家;这就是我正在使用它来使它非常有效(我仍然希望能够让用户简单地按住键,并在后续单元格中连续输入该值):
private void dataGridViewPlatypus_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e) {
int columnIndex = (((DataGridView)(sender)).CurrentCell.ColumnIndex);
if (columnIndex % 2 == 1) {
e.Control.KeyDown -= TextboxNumeric_KeyDown;
e.Control.KeyDown += TextboxNumeric_KeyDown;
e.Control.KeyUp -= TextboxNumeric_KeyUp;
e.Control.KeyUp += TextboxNumeric_KeyUp;
}
}
private void TextboxNumeric_KeyDown(object sender, KeyEventArgs e) {
var tb = sender as TextBox;
if (tb != null) {
tb.MaxLength = 1;
}
}
// TODO: Now need to find a way to be able to just press down once
private void TextboxNumeric_KeyUp(object sender, KeyEventArgs e) {
var tb = sender as TextBox;
if (tb != null && tb.TextLength >= 1) {
if (dataGridViewPlatypus.CurrentCell.RowIndex != dataGridViewPlatypus.Rows.Count - 1) {
dataGridViewPlatypus.CurrentCell = dataGridViewPlatypus[
dataGridViewPlatypus.CurrentCell.ColumnIndex,
dataGridViewPlatypus.CurrentCell.RowIndex + 1];
} else { // on last row
this.dataGridViewPlatypus.CurrentCell = this.dataGridViewPlatypus.CurrentCell.ColumnIndex != dataGridViewPlatypus.Columns.Count - 1 ? this.dataGridViewPlatypus[this.dataGridViewPlatypus.CurrentCell.ColumnIndex + 2, 0] : this.dataGridViewPlatypus[1, 0];
}
}
}
解决方法:
DataGridView的CurrentCell属性有一个setter,允许您传入一个新单元格.
解决此问题的一种方法是处理网格的EditingControlShowing事件,并将KeyPress处理程序附加到编辑控件,如下所示:
private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
if ((int)(((System.Windows.Forms.DataGridView)(sender)).CurrentCell.ColumnIndex) == 1)
{
e.Control.KeyPress += TextboxNumeric_KeyPress;
}
}
然后在按键处理程序中,您有:
private void TextboxNumeric_KeyPress(object sender, KeyPressEventArgs e)
{
TextBox tb = sender as TextBox;
if (tb.TextLength >= 5)
{
dataGridView1.CurrentCell = dataGridView1[dataGridView1.CurrentCell.ColumnIndex + 1, dataGridView1.CurrentCell.RowIndex];
}
}
上面的逻辑当然不适合您的情况,但传递新的CurrentCell的原则(在从网格中检索所需的单元格之后)代表.
内容总结
以上是互联网集市为您收集整理的c# – 如何以编程方式从数据网格视图中的一个单元格移动到另一个单元格?全部内容,希望文章能够帮你解决c# – 如何以编程方式从数据网格视图中的一个单元格移动到另一个单元格?所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。