相关文章推荐
时尚的海豚  ·  .NET ...·  1 周前    · 
温柔的西瓜  ·  C#教程推荐 - 知乎·  1 年前    · 
憨厚的烈酒  ·  C#中 ...·  1 年前    · 

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

@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;
                    dataGridView1.DataSource = dataView;
                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;
                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

Hi @ankit goel , If you want the text box control to be able to capture keyboard events, you can do it in a custom way. You can inherit from the DataGridViewTextBoxEditControl class and override its ProcessCmdKey method to handle keyboard events in the text box. I'm a bit confused about your logic; could you simplify your code to the bare minimum? What are you trying to do with down and up arow?

@Jiale Xue - MSFT , lets say i have two datagridview's datagridview 1 in which i am working and datagridview2 which is hidden and will be used for suggestions . Nwhen the cursor reaches the first column of datagridview1 a hidden (another) datagridview2 is popped up which was made invisible . now on textchanged event of textbox inside datagridview1 , whatever the user types , datagridview2 is giving suggestions according to the string the user type in the textbox of datagridview1 . for that i have loaded a dataset and bind it to that (hidden) datagridview2 on form load . for filtering suggestions i used dataview component . now what i have achieved is when user types something let say An , the datagridview2 gives results like Ankit , Anna but at the same time, i am trying to let the user use arrow key to move up and down in this datagridview2 so that when he presses enter the suggestion he chooses (Ankit or anna) gets copied to the first cell of datagridview . Hope this helps .