2
Reply

AutoComplete textbox(column) in Datagridview from sql Databa

Mohammed Khadir

Mohammed Khadir

Oct 31 2016 5:23 AM
313
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace Supplier
{
public partial class auotest : Form
{
SqlConnection con = new SqlConnection(@"Data Source=.;Initial Catalog=SupplierDB;Integrated Security=True");
public auotest()
{
InitializeComponent();
}
private void auotest_Load(object sender, EventArgs e)
{
con.Open();
SqlCommand cmd = con.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "Select * from ProductDetails";
cmd.ExecuteNonQuery();
DataTable dt = new DataTable();
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(dt);
dataGridView1.DataSource = dt;
con.Close();
}
private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
string g = dataGridView1.Columns[3].HeaderText;
if (g.Equals("CompanyID"))
{
TextBox auto_texts = e.Control as TextBox;
if (auto_texts != null)
{
auto_texts.AutoCompleteMode = AutoCompleteMode.Suggest;
auto_texts.AutoCompleteSource = AutoCompleteSource.CustomSource;
AutoCompleteStringCollection coll = new AutoCompleteStringCollection();
// add_items(coll);
auto_texts.AutoCompleteCustomSource = coll;
con.Open();
string sql = " select CompanyID from ProductDetails";
SqlDataAdapter sda = new SqlDataAdapter(sql, con);
DataTable dt = new DataTable();
sda.Fill(dt);
for (int i = 0; i < dt.Rows.Count; i++)
{
string name = dt.Rows[i]["CompanyID"].ToString();
coll.Add(name);
}
con.Close();
}
}
}
}
}
 
In a Column[3] of DataGridview is CompanyID column here for that column i have used the above code to autocomplete n now i want to have same for product name, productId and remaining columns too, i tried changing column header text [0] or [1] or others too with that i changed the names for respective columns from database too but i didnt got any result, please help me how to get the better result for this,

thanks everyone


Answers (2)