Hi @ankit goel , Welcome to Microsoft Q&A,
Yes, just extract the same code.
For example:
private void customevent(DataGridView dgv, KeyEventArgs e)
{
//code for results up and down in popup datagridview
if (dgv.CurrentCell == null)
{
return;
}
else
{
int rpos = dgv.CurrentCell.RowIndex;
int cpos = dgv.CurrentCell.ColumnIndex;
switch (e.KeyCode)
{
case Keys.Up:
rpos--;
if (rpos >= 0) dgv.CurrentCell = dgv.Rows[rpos].Cells[cpos];
e.Handled = true;
break;
case Keys.Down:
rpos++;
if (rpos < dgv.Rows.Count) dgv.CurrentCell = dgv.Rows[rpos].Cells[cpos];
e.Handled = true;
break;
}
}
}
Then you can use it in a similar way.
private void textBox2_KeyDown(object sender, KeyEventArgs e)
{
if (dgv.CurrentCell.ColumnIndex == 0) // Assuming the first column has an index of 0
{
TextBox textBox = (TextBox)sender;
customevent(dgv, e);
}
}
private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
customevent(dataGridView1, e);
}
Best Regards,
Jiale
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.