Hi SUJAYS,
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
Namespaces
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)
{
BindDataGrid();
}
private void OnEditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
if (dataGridView1.Columns[1].HeaderText.Equals("Name"))
{
TextBox textBox = e.Control as TextBox;
if (textBox != null)
{
textBox.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
textBox.AutoCompleteSource = AutoCompleteSource.CustomSource;
textBox.AutoCompleteCustomSource = AutoCompleteItems();
}
}
}
private void BindDataGrid()
{
string constring = @"Server=.\SQL2014;DataBase=Northwind;UID=sa;PWD=pass@123";
using (SqlConnection con = new SqlConnection(constring))
{
using (SqlCommand cmd = new SqlCommand("SELECT TOP 10 * FROM Customers", con))
{
cmd.CommandType = CommandType.Text;
using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
{
using (DataTable dt = new DataTable())
{
sda.Fill(dt);
dataGridView1.AutoGenerateColumns = false;
dataGridView1.ColumnCount = 3;
dataGridView1.Columns[0].Name = "Id";
dataGridView1.Columns[0].HeaderText = "Id";
dataGridView1.Columns[0].DataPropertyName = "CustomerID";
dataGridView1.Columns[1].Name = "Name";
dataGridView1.Columns[1].HeaderText = "Name";
dataGridView1.Columns[1].DataPropertyName = "ContactName";
dataGridView1.Columns[2].Name = "Country";
dataGridView1.Columns[2].HeaderText = "Country";
dataGridView1.Columns[2].DataPropertyName = "Country";
dataGridView1.DataSource = dt;
}
}
}
}
}
public AutoCompleteStringCollection AutoCompleteItems()
{
AutoCompleteStringCollection acsc = new AutoCompleteStringCollection();
string conString = @"Server=.\SQL2014;DataBase=Northwind;UID=sa;PWD=pass@123";
string query = "SELECT DISTINCT Country FROM Customers WHERE Country IS NOT NULL";
using (SqlConnection con = new SqlConnection(conString))
{
SqlCommand cmd = new SqlCommand(query, con);
con.Open();
SqlDataReader sdr = cmd.ExecuteReader();
while (sdr.Read())
{
acsc.Add(sdr["Country"].ToString());
}
con.Close();
}
return acsc;
}
}
VB.Net
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
BindDataGrid()
End Sub
Private Sub OnEditingControlShowing(ByVal sender As Object, ByVal e As DataGridViewEditingControlShowingEventArgs) Handles dataGridView1.EditingControlShowing
If dataGridView1.Columns(1).HeaderText.Equals("Name") Then
Dim textBox As TextBox = TryCast(e.Control, TextBox)
If textBox IsNot Nothing Then
textBox.AutoCompleteMode = AutoCompleteMode.SuggestAppend
textBox.AutoCompleteSource = AutoCompleteSource.CustomSource
textBox.AutoCompleteCustomSource = AutoCompleteItems()
End If
End If
End Sub
Private Sub BindDataGrid()
Dim constring As String = "Server=.\SQL2014;DataBase=Northwind;UID=sa;PWD=pass@123"
Using con As SqlConnection = New SqlConnection(constring)
Using cmd As SqlCommand = New SqlCommand("SELECT TOP 10 * FROM Customers", con)
cmd.CommandType = CommandType.Text
Using sda As SqlDataAdapter = New SqlDataAdapter(cmd)
Using dt As DataTable = New DataTable()
sda.Fill(dt)
dataGridView1.AutoGenerateColumns = False
dataGridView1.ColumnCount = 3
dataGridView1.Columns(0).Name = "Id"
dataGridView1.Columns(0).HeaderText = "Id"
dataGridView1.Columns(0).DataPropertyName = "CustomerID"
dataGridView1.Columns(1).Name = "Name"
dataGridView1.Columns(1).HeaderText = "Name"
dataGridView1.Columns(1).DataPropertyName = "ContactName"
dataGridView1.Columns(2).Name = "Country"
dataGridView1.Columns(2).HeaderText = "Country"
dataGridView1.Columns(2).DataPropertyName = "Country"
dataGridView1.DataSource = dt
End Using
End Using
End Using
End Using
End Sub
Public Function AutoCompleteItems() As AutoCompleteStringCollection
Dim acsc As AutoCompleteStringCollection = New AutoCompleteStringCollection()
Dim conString As String = "Server=.\SQL2014;DataBase=Northwind;UID=sa;PWD=pass@123"
Dim query As String = "SELECT DISTINCT Country FROM Customers WHERE Country IS NOT NULL"
Using con As SqlConnection = New SqlConnection(conString)
Dim cmd As SqlCommand = New SqlCommand(query, con)
con.Open()
Dim sdr As SqlDataReader = cmd.ExecuteReader()
While sdr.Read()
acsc.Add(sdr("Country").ToString())
End While
con.Close()
End Using
Return acsc
End Function
End Class
Screenshot