A community member has associated this post with a similar question:
editable datagridview c#

Only moderators can edit this content.

Keydown event inside textbox within a a datagridview

ankit goel 746 Reputation points
2023-05-17T08:44:03.4633333+00:00

@Jack J Jun Hi sir ,
i am working on a datagridview where I am trying a cell of a datgridview as a textbox so that i can use and attach different events to it which are generally used with textbox control . below is the code which i have used . i found out that although the textbox events are working fine but when i press down key it is captured by the datagridview and not by the textbox .

     private void customControl11_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
        {
            if (customControl11.CurrentCell.ColumnIndex == 0) // Assuming the first column has an index of 0
            {
               TextBox textBox = e.Control as TextBox;
                if (textBox != null)
                {
                    textBox.TextChanged += TextBox_TextChanged; // Attach TextChanged event handler
                 

                    textBox.KeyDown += TextBox_KeyDown; // Attach KeyDown event handler
                    textBox.Enter += (textBoxSender, textBoxEvent) =>
                    {
                        MessageBox.Show("sddsd");
                        dataGridView1.Visible = true;
                        // Hide unwanted columns
                        dataGridView1.Columns["id"].Visible = false;
                        dataGridView1.Columns["current_balance"].Visible = false;
                        dataGridView1.Columns["opening_balance"].Visible = false;
                    };

                }
            }
        }

        private void TextBox_TextChanged(object sender, EventArgs e)
        {
            TextBox textBox = (TextBox)sender;
            string searchText = textBox.Text.Trim();
            dataView = dataSet.Tables[0].DefaultView;

            if (searchText.Length > 0) // strings
            {
                dataView.RowFilter = $"name LIKE '%{searchText}%'";
                if (dataView.Count == 0) // check if no rows match the filter
                {
                    searchText = searchText.Substring(0, searchText.Length - 1);
                    textBox.Text = searchText;
                }
                else
                {

                    dataGridView1.DataSource = dataView;
                }
            }
            else
            {

                dataGridView1.DataSource = dataSet.Tables[0];
            }
            textBox.Select(textBox.Text.Length, 0); // Set cursor at the end
        }

        private void TextBox_KeyDown(object sender, KeyEventArgs e)
        { 
            TextBox textBox = (TextBox)sender;

            //code for results up and down in popup datagridview

            if (dataGridView1.CurrentCell == null)
            {
                return;
            }
            else
            {
                int rpos = dataGridView1.CurrentCell.RowIndex;
                int cpos = dataGridView1.CurrentCell.ColumnIndex;
                switch (e.KeyCode)
                {
                    case Keys.Up:

                        rpos--;
                        if (rpos >= 0) dataGridView1.CurrentCell = dataGridView1.Rows[rpos].Cells[cpos];
                        e.Handled = true;
                        break;
                    case Keys.Down:
                        //int rpos1 = dgvUserTypes.CurrentCell.RowIndex;
                        //int cpos1 = dgvUserTypes.CurrentCell.ColumnIndex;
                        rpos++;
                        if (rpos < dataGridView1.Rows.Count) dataGridView1.CurrentCell = dataGridView1.Rows[rpos].Cells[cpos];
                        e.Handled = true;
                        break;
                    case Keys.Enter:
                        {
                            if (dataGridView1.Rows.Count > 0)
                            {
                                textBox.Text = dataGridView1.SelectedRows[0].Cells[1].Value.ToString();
                               // customControl11.Focus();
                              //  customControl11.CurrentCell = customControl11.Rows[0].Cells[0]; // Select the first cell
                                dataGridView1.Visible = false;
                            }
                            e.Handled = e.SuppressKeyPress = true;
                            break;
                        }
                }
            }
        }

as you can see that i have used the above textbox_keydown to capture the down and up arow so that i can used them to scroll the results the of a datagridview . it seems that datagridview's default behaviour of using arrow key for navigation is eating the textbox event. please suggest

Windows Forms
Windows Forms
A set of .NET Framework managed libraries for developing graphical user interfaces.
1,811 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,098 questions
{count} votes