Как выполнить некоторую кодировку при нажатии клавиши ввода в datagridview? - PullRequest
2 голосов
/ 28 июля 2011

Я использую datagridview в winform.

Если я нажму клавишу ввода, выбор будет перемещен в ряд. Но я хочу выполнить код, который я напишу ниже:

try
{
    int dispin = dataGridView1.CurrentCell.OwningColumn.DisplayIndex;
    if (dispin == 0)
    {
        string cellval = dataGridView1.CurrentCell.Value.ToString();
        returnParam = cellval;
        this.Close();
    }
    else
    {
        int rowIndex = dataGridView1.CurrentCell.RowIndex;
        int colIndex = dataGridView1.CurrentCell.ColumnIndex;
        colIndex = colIndex - 1;
        string cellval = dataGridView1[colIndex, rowIndex].Value.ToString();

        // MessageBox.Show(cellval1+cellval2+cellval3);
        returnParam = cellval;
        this.Close();
        //textBox1.Text = cellval;
    }
}
catch
{
    MessageBox.Show("Select a Record", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
    //this.Close();
}

Как это сделать?

Я пытаюсь включить ключ, но он влияет на все ключи, пожалуйста, помогите мне.

Ответы [ 2 ]

5 голосов
/ 28 июля 2011

Использование datagridview.KeyPress Как:

//at form load:
dataGirdView1.KeyPress += OnDataGirdView1_KeyPress;

private void OnDataGirdView1_KeyPress(object sender, KeyPressEventArgs e)
{
     if (e.KeyChar == (char)Keys.Enter)
     { 
         RunMyCustomCode(); 
         //e.Handled = true; if you don't want the datagrid from hearing the enter pressed
     }
}
0 голосов
/ 10 июня 2014
void TextBox1_KeyDown(object sender, KeyEventArgs e) 
{ 
    if (e.KeyCode == Keys.Enter) 
    { 
        e.Handled = true; 
    } 
}

void TextBox1_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e) 
{ 
    if (e.KeyCode == Keys.Return)
    {
        /* Your code here! */ 
    }
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...