focus Textbox inside datagridview

ankit goel 746 Reputation points
2023-05-16T13:31:31.95+00:00

@Jack J Jun , Hi i am trying to set the focus to the very first cell of the DataGridview on form load . when run the code the first cell gets the focus but the problem is that its enter event doesn't fire until i press a key. what i am looking is a way , when it gets focuses it should immediately display a datagridview . for the reference the datagridview inside which a textbox is declared is named as customControl11 (not datagridview1)


  private void Form2_Load(object sender, EventArgs e)
        {
          
          customControl11.Focus();
          customControl11.CurrentCell = customControl11.Rows[0].Cells[0]; // Select the first cell
			 }
// datagridview events 
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) =>
                    {
                        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 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:
                        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;
                        }
                }
            }
        }
Windows Forms
Windows Forms
A set of .NET Framework managed libraries for developing graphical user interfaces.
1,864 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,490 questions
{count} votes

1 answer

Sort by: Most helpful
  1. ankit goel 746 Reputation points
    2023-07-15T06:28:16.5+00:00

    The problem has been solved by using customControl11.BeginEdit(true);

    0 comments No comments