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
}
}
}
}
使用方法に関するヘルプはありますか?