Search code examples
c#.netdatagridviewkeypress

Move DataGridView Selected Cell/Row Up/Down On Keys Press Using C#


I have a datagridview1 in my form with one column and unlimited rows hiding its headers using the below codes...

dataGridView1.RowHeadersVisible = false;   // Hide Row Header
dataGridView1.ColumnHeadersVisible = false; // Hide Column Header

So now when we focus on it then first row is already selected and highlighted. Now here I want to use my keyboard UP and DOWN keys to move my selection up and down using the below KeyDown function and when I press ENTER then the select cell value should go to class level variable and by default first focus row/cell value should be set to class level variable also...

private void datagridview1_KeyDown(object sender, KeyEventArgs e)
{
   if (dataGridView1.Visible == true)
   {
       if (e.KeyCode.Equals(Keys.Up))
       {
           MessageBox.Show("Move UP Row/Cell And Select It");
       }
       if (e.KeyCode.Equals(Keys.Down))
       {
           MessageBox.Show("Move Down Row/Cell And Select It");
       }
       if (e.KeyCode.Equals(Keys.Enter))
       {
           MessageBox.Show("Send Selected Cell Value To Class Level Variable");
       }
       e.Handled = true;
    }
 }

Currently I have some class level variable that contain datagridview CELL value that I am getting CellClick using below code but I want to do this features on keys using upper code...

private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
    value = dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString();
}

And my Class level variable is like...

public partial class Form1: Form
{
    string value = "";
    public MainForm()
    {
        InitializeComponent();
    }
}

So How can I do it using C#...???

UPDATE:

For UP/DOWN selection, I used the below code and its working. So now how to send the selected cell value to class level variable on pressing ENTER key...???

private void selectUpRow()
{
    DataGridView dgv = dataGridView1;
    int totalRows = dgv.Rows.Count;
    int rowIndex = dgv.SelectedCells[0].OwningRow.Index;
    if (rowIndex == 0)
        return;
    int colIndex = dgv.SelectedCells[0].OwningColumn.Index;
    DataGridViewRow selectedRow = dgv.Rows[rowIndex];
    dgv.ClearSelection();
    dgv.Rows[rowIndex - 1].Cells[colIndex].Selected = true;
}

private void selectDownRow()
{
    DataGridView dgv = dataGridView1;
    int totalRows = dgv.Rows.Count;
    int rowIndex = dgv.SelectedCells[0].OwningRow.Index;
    if (rowIndex == totalRows - 1)
        return;
    int colIndex = dgv.SelectedCells[0].OwningColumn.Index;
    DataGridViewRow selectedRow = dgv.Rows[rowIndex];
    dgv.ClearSelection();
    dgv.Rows[rowIndex + 1].Cells[colIndex].Selected = true;
}

Solution

  • So finally I got it working by trying Bla Bla Bla. Here is the short answer with the code...

    public partial class Form1 : Form
        {
            string value = "";
            public MainForm()
            {
                InitializeComponent();
            }
    
            private void textBox1_KeyDown(object sender, KeyEventArgs e)
            {
                if (dataGridView1.Visible == true)
                {
                    if (e.KeyCode.Equals(Keys.Up))
                    {
                        selectUpRow();
                    }
                    if (e.KeyCode.Equals(Keys.Down))
                    {
                        selectDownRow();
                    }
                    if (e.KeyCode.Equals(Keys.Enter))
                    {
                        selectCellValue();
                    }
                    e.Handled = true;
                }
            }
    
            private void selectUpRow()
            {
                DataGridView dgv = dataGridView1;
                int totalRows = dgv.Rows.Count;
                int rowIndex = dgv.SelectedCells[0].OwningRow.Index;
                if (rowIndex == 0)
                    return;
                int colIndex = dgv.SelectedCells[0].OwningColumn.Index;
                DataGridViewRow selectedRow = dgv.Rows[rowIndex];
                dgv.ClearSelection();
                dgv.Rows[rowIndex - 1].Cells[colIndex].Selected = true;
            }
    
            private void selectDownRow()
            {
                DataGridView dgv = dataGridView1;
                int totalRows = dgv.Rows.Count;
                int rowIndex = dgv.SelectedCells[0].OwningRow.Index;
                if (rowIndex == totalRows - 1)
                    return;
                int colIndex = dgv.SelectedCells[0].OwningColumn.Index;
                DataGridViewRow selectedRow = dgv.Rows[rowIndex];
                dgv.ClearSelection();
                dgv.Rows[rowIndex + 1].Cells[colIndex].Selected = true;
            }
    
            private void selectCellValue()
            {
                int rowIndex = dataGridView1.SelectedCells[0].OwningRow.Index;
                value = dataGridView1.Rows[rowIndex].Cells[0].Value.ToString();
            }
    
        }