0

1 行から 30 行までのセルを含む dataGridView があります。ここで、入力データを入力しながらチェックし、検証が通れば次のセルにフォーカスを移す必要があります.

主なアイデアは、入力中に現在のセルをチェックすることです。

PS セルはプログラムで作成されており、それぞれに 1文字しか含めることができません。

さて、確認のために次のものを作成しましたが、動作します:

private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
        {
            if (dataGridView1.CurrentCell.RowIndex == 0)
            {
                TextBox tb = (TextBox)e.Control;
                tb.KeyPress += new KeyPressEventHandler(tb_KeyPress);
            }
        }

        void tb_KeyPress(object sender, KeyPressEventArgs e)
        {
            List<string> detect = new List<string> { "№", "1", "2", "3", "4", "5", "6", "7", "8", "9", "0", "`", "~", "!", "@", "#", "$", "%", "^", "&", "*", "(", ")", "_", "+", "=", "-", "/", "*", ".", "|", "]", "[", "}", "{", "'", ";", ":", "?", ">", "<", ",", "\"", "\\" };
            var character = e.KeyChar.ToString();
            if (dataGridView1.CurrentCell.RowIndex == 0)
            {
                foreach (string Item in detect)
                {
                    if (character == Item)
                    {
                        e.Handled = true;
                    }
                }
            }
        }

現在のセルに文字が 1 つある場合にのみ、次のセルにフォーカスを移動する必要があります。

このコードを見つけましたが、適切に見えますが、実際にはどうすればよいかわかりません:

public static class GridExtension
{
    public static void MoveNextCell(this DataGridView dgv)
    {
        DataGridViewCell currentCell = dgv.CurrentCell;
        if (currentCell != null)
        {
            int nextRow = currentCell.RowIndex;
            int nextCol = currentCell.ColumnIndex + 1;
            if (nextCol >= dgv.ColumnCount)
            {
                nextCol = 0;
                nextRow++;
            }
            if (nextRow >= dgv.RowCount)
            {
                nextRow = 0;
            }
            DataGridViewCell nextCell = dgv.Rows[nextRow].Cells[nextCol];
            if (nextCell != null && nextCell.Visible)
            {
                if ((currentCell != null) && (currentCell.IsInEditMode))
                    dgv.EndEdit();
                try
                {
                    dgv.CurrentCell = nextCell;
                }
                catch (InvalidOperationException) { } //Fails if you have cell validation
            }
        }
    }
}

使用方法に関するヘルプはありますか?

4

1 に答える 1

0

まず、実行時に同じ値をチェックできるように、定数値を割り当てる必要があります。

Private Const GV_Cell1 As Integer = 0
Private Const GV_Cell2 As Integer = 1

For Each ループ Ex を使用して、その定数によって同じセルを評価できます。

For Each gr As GridViewRow In Me.GV.Rows

gr.Cells(GV_cell1).Text
于 2013-04-15T09:25:12.140 に答える