如何以编程从一个细胞移动在一个DataGridView到另一个? [英] How can I programmatically move from one cell in a datagridview to another?

查看:148
本文介绍了如何以编程从一个细胞移动在一个DataGridView到另一个?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要仅允许输入到可编辑的datagridview细胞一个字符(每隔一列,奇数的人,是可编辑的);如果用户添加的第二字符,而在这些细胞中的一个,光标应移动到下一个小区向下,并把该第二值有(再次向下在该键移动再次按下,等等)。如果在网格(第12行)的底部,它应该移动到第0行,也移动两列向右



我试图这样做:

 私人无效dataGridViewPlatypus_KeyDown(对象发件人,发送KeyEventArgs E){
VAR currentCell = dataGridViewPlatypus.CurrentCell;
INT currentCol = currentCell.ColumnIndex;
INT currentRow = currentCell.RowIndex;
如果(currentCell.Value.ToString()长度方式> 0){
如果(currentRow&下; 11){
dataGridViewPlatypus.CurrentCell.RowIndex = currentRow + 1;
}否则如果(currentRow == 11){
currentCell.RowIndex = 0;
currentCell.ColumnIndex = currentCell.ColumnIndex + 2;
dataGridViewPlatypus.CurrentCell = currentCell;
}
}
}



...但我得到迷途的rowIndex和参数:columnIndex无法被分配到短信息,因为它们是只读的。



那么,如何才能做到这一点?



警告:我知道,我还必须添加逻辑,如果当前移动到第1列在最后一个可编辑栏的底部



更新



从tergiver的回答,这是我这么远,但我不知道如何前进到下一个单元格。

 保护覆盖布尔ProcessCmdKey(参考消息味精,钥匙KEYDATA){
如果(this.ActiveControl == dataGridViewPlatypus)
{
变种currentCell = dataGridViewPlatypus.CurrentCell;
如果
{
(currentCell.Value.ToString()长度== 1); //现在怎么办?
}
}
返回base.ProcessCmdKey(REF味精,KEYDATA);
}



更新2



感谢所有;这是我用得到它几乎什么工作(我仍然希望能够允许用户只需按住按键,并在随后的细胞不断输入的值):

 私人无效dataGridViewPlatypus_EditingControlShowing(对象发件人,DataGridViewEditingControlShowingEventArgs E){
INT参数:columnIndex =(((DataGridView中)(发件人))CurrentCell.ColumnIndex);
如果(参数:columnIndex%2 == 1){
e.Control.KeyDown - = TextboxNumeric_KeyDown;
e.Control.KeyDown + = TextboxNumeric_KeyDown;
e.Control.KeyUp - = TextboxNumeric_KeyUp;
e.Control.KeyUp + = TextboxNumeric_KeyUp;
}
}

私人无效TextboxNumeric_KeyDown(对象发件人,发送KeyEventArgs E){
VAR TB =发件人的文本框;
如果(TB!= NULL){
tb.MaxLength = 1;
}
}

// TODO:现在需要找到一种方法,能够只按下一次
私人无效TextboxNumeric_KeyUp(对象发件人,发送KeyEventArgs E) {
VAR TB =发件人的文本框;
如果(TB = NULL&放大器;&安培; tb.TextLength> = 1!){
如果(dataGridViewPlatypus.CurrentCell.RowIndex = dataGridViewPlatypus.Rows.Count - 1!){
dataGridViewPlatypus .CurrentCell = dataGridViewPlatypus [
dataGridViewPlatypus.CurrentCell.ColumnIndex,
dataGridViewPlatypus.CurrentCell.RowIndex + 1];
}其他{//在最后一行
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 酒店有一个二传手,让您在新的细胞通过。



一个办法解决这个问题是处理网格的 EditingControlShowing 事件并附加的KeyPress 处理程序,如编辑控制这样:

 私人无效dataGridView1_EditingControlShowing(对象发件人,DataGridViewEditingControlShowingEventArgs E) 
{
如果((INT)(((System.Windows.Forms.DataGridView)(发件人))。CurrentCell.ColumnIndex)== 1)
{
e.Control .KeyPress + = TextboxNumeric_KeyPress;
}
}



然后在按键处理您有:

 私人无效TextboxNumeric_KeyPress(对象发件人,KeyPressEventArgs E)
{
文本框TB =发件人的文本框;
如果(tb.TextLength> = 5)
{
dataGridView1.CurrentCell = dataGridView1 [dataGridView1.CurrentCell.ColumnIndex + 1,dataGridView1.CurrentCell.RowIndex]
}
}

上面的逻辑当然是对你的情况不正确但是在一个新CurrentCell传递(从电网检索所需的细胞后)的原理表示。


I need to only allow one character to be entered into the editable datagridview cells (every other column, the odd-numbered ones, are editable); if the user adds a second character while in one of these cells, the cursor should move to the next cell down and put that second value there (pressing again on that key moves down again, and so forth). If at the bottom of the grid (the 12th row), it should move to row 0 and also move two columns to the right.

I tried doing this:

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;
        }
    }
}

...but I get err msgs that RowIndex and ColumnIndex cannot be assigned to, as they are readonly.

So how can I accomplish this?

Caveat: I know that I will also have to add logic to move to column 1 if currently at the bottom of the last editable column.

UPDATE

From tergiver's answer, this is what I've got so far, but I don't know how to advance to the next cell.

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);
}

UPDATE 2

Thanks all; this is what I'm using to get it pretty much working (I still want to be able to allow the user to simply hold the key down, and have that value continuously entered in subsequent cells):

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];
        }
    }
}

解决方案

The CurrentCell property of the DataGridView has a setter, allowing you to pass in a new cell.

One approach to this problem is to handle the EditingControlShowing event of the grid and attach a KeyPress handler to the editing control like so:

private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{                
    if ((int)(((System.Windows.Forms.DataGridView)(sender)).CurrentCell.ColumnIndex) == 1)
    {
        e.Control.KeyPress += TextboxNumeric_KeyPress;
    }
}

Then in the key press handler you have:

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];
     }
}

The logic above is of course not correct for your case but the principle of passing in a new CurrentCell (after retrieving the desired cell from the grid) stands.

这篇关于如何以编程从一个细胞移动在一个DataGridView到另一个?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆