Focus on a datagridview and textbox at the same time in a C# winform search box

Last Reply 6 days ago By dharmendr

Posted 8 days ago

Hii. I need help. I m working on point of sale. I need help on gridview and text box. I make item search form. But there is one problem. I want to focus on both control same time. If user write something on textbox user get result on gridview and user can also same time user enter key that data select and view data. 

You are viewing reply posted by: dharmendr 6 days ago.
Posted 6 days ago

Hi shakil,

You have to use DataGridView EditingControlShowing event. So that when you type TextBox will be focused.

Check this example. Now please take its reference and correct your code.

Database

For this example I have used of Northwind database that you can download using the link given below.

Download Northwind Database

Namepsaces

C#

using System.Data;
using System.Data.SqlClient;

VB.Net

Imports System.Data
Imports System.Data.SqlClient

Code

C#

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        txtName.Focus();
        dataGridView1.DataSource = this.PopulateDataGridView();
    }

    private void txtName_KeyUp(object sender, KeyEventArgs e)
    {
        dataGridView1.DataSource = this.PopulateDataGridView();
    }

    private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
    {
        txtName.Focus();
    }

    private DataTable PopulateDataGridView()
    {
        string query = "SELECT CustomerID, ContactName, Country FROM Customers";
        query += " WHERE ContactName LIKE '%' + @ContactName + '%'";
        query += " OR @ContactName = ''";
        string constr = @"Data Source=.;Initial Catalog=Northwind;User ID=sa;Password=pass@123";
        using (SqlConnection con = new SqlConnection(constr))
        {
            using (SqlCommand cmd = new SqlCommand(query, con))
            {
                cmd.Parameters.AddWithValue("@ContactName", txtName.Text.Trim());
                using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
                {
                    DataTable dt = new DataTable();
                    sda.Fill(dt);
                    return dt;
                }
            }
        }
    }
}

VB.Net

Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
    txtName.Focus()
    dataGridView1.DataSource = Me.PopulateDataGridView()
End Sub

Private Sub txtName_KeyUp(sender As System.Object, e As System.Windows.Forms.KeyEventArgs) Handles txtName.KeyUp
    dataGridView1.DataSource = Me.PopulateDataGridView()
End Sub

Private Sub dataGridView1_EditingControlShowing(sender As Object, e As DataGridViewEditingControlShowingEventArgs) Handles dataGridView1.EditingControlShowing
    txtName.Focus()
End Sub

Private Function PopulateDataGridView() As DataTable
    Dim query As String = "SELECT CustomerID, ContactName, Country FROM Customers"
    query &= " WHERE ContactName LIKE '%' + @ContactName + '%'"
    query &= " OR @ContactName = ''"
    Dim constr As String = "Data Source=.;Initial Catalog=Northwind;User ID=sa;Password=pass@123"
    Using con As SqlConnection = New SqlConnection(constr)
        Using cmd As SqlCommand = New SqlCommand(query, con)
            cmd.Parameters.AddWithValue("@ContactName", txtName.Text.Trim())
            Using sda As SqlDataAdapter = New SqlDataAdapter(cmd)
                Dim dt As DataTable = New DataTable()
                sda.Fill(dt)
                Return dt
            End Using
        End Using
    End Using
End Function

Screnshot