Autocomplete datagridview textbox, with SubString search, similar to SQL Like or Contains
Good day guys, I am asking you a simple favor, I am trying to make a datagridview textbox with autocomplete, but the problem with the default autocomplete is that it only read at the prefix. What I want to do is something like you will saw in this site LINK but I can't make it work in datagridview as I am a bit confused on how I will integrate it in mine, below is my full code:
public Form2()
{
InitializeComponent();
}
SqlConnection conn = new SqlConnection(@"Data Source=DESKTOP-UAK52DL;Initial Catalog=Inventory;Integrated Security=True");
SqlCommand cd = new SqlCommand();
private void label15_Click(object sender, EventArgs e)
{
}
private void Form2_Load(object sender, EventArgs e)
{
DataGridViewTextBoxColumn dgvslno = new DataGridViewTextBoxColumn();
dgvslno.HeaderText = "Product Code";
dgvslno.Width = 200;
dataGridView1.Columns.Add(dgvslno);
DataGridViewTextBoxColumn dgvpro = new DataGridViewTextBoxColumn();
dgvpro.HeaderText = "Description";
dgvpro.Width = 300;
dataGridView1.Columns.Add(dgvpro);
DataGridViewTextBoxColumn dgvum = new DataGridViewTextBoxColumn();
dgvum.HeaderText = "UM";
dgvum.Width = 50;
dataGridView1.Columns.Add(dgvum);
DataGridViewTextBoxColumn dgvup = new DataGridViewTextBoxColumn();
dgvup.HeaderText = "UP";
dgvup.Width = 100;
dataGridView1.Columns.Add(dgvup);
DataGridViewTextBoxColumn dgvqty = new DataGridViewTextBoxColumn();
dgvqty.HeaderText = "QTY";
dgvqty.Width = 100;
dataGridView1.Columns.Add(dgvqty);
DataGridViewTextBoxColumn dgvamt = new DataGridViewTextBoxColumn();
dgvamt.HeaderText = "Amount";
dgvamt.Width = 150;
dataGridView1.Columns.Add(dgvamt);
DataGridViewTextBoxColumn dgvmanu = new DataGridViewTextBoxColumn();
dgvmanu.HeaderText = "Manu. Date";
dgvmanu.Width = 100;
dataGridView1.Columns.Add(dgvmanu);
DataGridViewTextBoxColumn dgvexp = new DataGridViewTextBoxColumn();
dgvexp.HeaderText = "Exp. Date";
dgvexp.Width = 100;
dataGridView1.Columns.Add(dgvexp);
DataGridViewTextBoxColumn dgvlot = new DataGridViewTextBoxColumn();
dgvlot.HeaderText = "Lot #";
dgvlot.Width = 100;
dataGridView1.Columns.Add(dgvlot);
cc();
cc2();
cc3();
}
public void cc()
{
conn.Open();
SqlCommand cmd = conn.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "SELECT Department from tbldept";
cmd.ExecuteNonQuery();
DataTable dt = new DataTable();
SqlDataAdapter Da = new SqlDataAdapter(cmd);
Da.Fill(dt);
foreach (DataRow dr in dt.Rows)
{
comboBox1.Items.Add(dr["Department"].ToString());
comboBox2.Items.Add(dr["Department"].ToString());
}
conn.Close();
}
public void cc2()
{
conn.Open();
SqlCommand cmd = conn.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "select distinct Supplier from tblmaster";
cmd.ExecuteNonQuery();
DataTable dt = new DataTable();
SqlDataAdapter Da = new SqlDataAdapter(cmd);
Da.Fill(dt);
foreach (DataRow dr in dt.Rows)
{
comboBox3.Items.Add(dr["Supplier"].ToString());
}
conn.Close();
}
public void cc3()
{
conn.Open();
SqlCommand cmd = conn.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "SELECT [Transaction] from [tbltransac]";
cmd.ExecuteNonQuery();
DataTable dt = new DataTable();
SqlDataAdapter Da = new SqlDataAdapter(cmd);
Da.Fill(dt);
foreach (DataRow dr in dt.Rows)
{
comboBox4.Items.Add(dr["Transaction"].ToString());
}
conn.Close();
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
conn.Open();
SqlCommand cmd = conn.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "select * from tbldept where Department = '" + comboBox1.SelectedItem.ToString() + "'";
cmd.ExecuteNonQuery();
DataTable dt = new DataTable();
SqlDataReader dr = cmd.ExecuteReader();
SqlDataAdapter Da = new SqlDataAdapter(cmd);
conn.Close();
}
private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
{
conn.Open();
SqlCommand cmd = conn.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "select * from tbldept where Department = '" + comboBox2.SelectedItem.ToString() + "'";
cmd.ExecuteNonQuery();
DataTable dt = new DataTable();
SqlDataReader dr = cmd.ExecuteReader();
SqlDataAdapter Da = new SqlDataAdapter(cmd);
conn.Close();
}
private void comboBox3_SelectedIndexChanged(object sender, EventArgs e)
{
conn.Open();
SqlCommand cmd = conn.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "select * from tblmaster where Supplier = '" + comboBox3.SelectedItem.ToString() + "'";
cmd.ExecuteNonQuery();
DataTable dt = new DataTable();
SqlDataAdapter Da = new SqlDataAdapter(cmd);
Da.Fill(dt);
conn.Close();
}
private void comboBox4_SelectedIndexChanged(object sender, EventArgs e)
{
conn.Open();
SqlCommand cmd = conn.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "select * from [tbltransac] where [Transaction] = '" + comboBox4.SelectedItem.ToString() + "'";
cmd.ExecuteNonQuery();
DataTable dt = new DataTable();
SqlDataAdapter Da = new SqlDataAdapter(cmd);
conn.Close();
}
//private void comboBox5_SelectedIndexChanged(object sender, EventArgs e)
//{
// conn.Open();
// SqlCommand cmd = conn.CreateCommand();
// cmd.CommandType = CommandType.Text;
// cmd.CommandText = "select * from tblmaster where ProductCode = '" + comboBox5.SelectedItem.ToString() + "'";
// cmd.ExecuteNonQuery();
// DataTable dt = new DataTable();
// SqlDataAdapter Da = new SqlDataAdapter(cmd);
// Da.Fill(dt);
// foreach (DataRow dr in dt.Rows)
// {
// textBox3.Text = dr["Description"].ToString();
// textBox4.Text = dr["UM"].ToString();
// textBox5.Text = dr["UP"].ToString();
// }
// conn.Close();
//}
public AutoCompleteStringCollection AutoCompleteLoad()
{
SqlConnection conn = new SqlConnection(@"Data Source=DESKTOP-UAK52DL;Initial Catalog=Inventory;Integrated Security=True");
SqlCommand cmd = new SqlCommand("Select ProductCode from tblmaster", conn);
conn.Open();
SqlDataReader dr = cmd.ExecuteReader();
AutoCompleteStringCollection mycoll = new AutoCompleteStringCollection();
while (dr.Read())
{
mycoll.Add(dr["ProductCode"].ToString());
}
return mycoll;
}
private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
}
private void dataGridView1_EditingControlShowing_1(object sender, DataGridViewEditingControlShowingEventArgs e)
{
int column = dataGridView1.CurrentCell.ColumnIndex;
string headerText = dataGridView1.Columns[column].HeaderText;
if (headerText.Equals("Product Code"))
{
TextBox tb = e.Control as TextBox;
if (tb != null)
{
tb.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
tb.AutoCompleteCustomSource = AutoCompleteLoad();
tb.AutoCompleteSource = AutoCompleteSource.CustomSource;
//tb.Add()
}
}
else
{
TextBox tb = e.Control as TextBox;
if (tb != null)
{
tb.AutoCompleteMode = AutoCompleteMode.None;
}
}
}
private void comboBox3_SelectedIndexChanged_1(object sender, EventArgs e)
{
}
}
}
c# sql autocomplete
add a comment |
Good day guys, I am asking you a simple favor, I am trying to make a datagridview textbox with autocomplete, but the problem with the default autocomplete is that it only read at the prefix. What I want to do is something like you will saw in this site LINK but I can't make it work in datagridview as I am a bit confused on how I will integrate it in mine, below is my full code:
public Form2()
{
InitializeComponent();
}
SqlConnection conn = new SqlConnection(@"Data Source=DESKTOP-UAK52DL;Initial Catalog=Inventory;Integrated Security=True");
SqlCommand cd = new SqlCommand();
private void label15_Click(object sender, EventArgs e)
{
}
private void Form2_Load(object sender, EventArgs e)
{
DataGridViewTextBoxColumn dgvslno = new DataGridViewTextBoxColumn();
dgvslno.HeaderText = "Product Code";
dgvslno.Width = 200;
dataGridView1.Columns.Add(dgvslno);
DataGridViewTextBoxColumn dgvpro = new DataGridViewTextBoxColumn();
dgvpro.HeaderText = "Description";
dgvpro.Width = 300;
dataGridView1.Columns.Add(dgvpro);
DataGridViewTextBoxColumn dgvum = new DataGridViewTextBoxColumn();
dgvum.HeaderText = "UM";
dgvum.Width = 50;
dataGridView1.Columns.Add(dgvum);
DataGridViewTextBoxColumn dgvup = new DataGridViewTextBoxColumn();
dgvup.HeaderText = "UP";
dgvup.Width = 100;
dataGridView1.Columns.Add(dgvup);
DataGridViewTextBoxColumn dgvqty = new DataGridViewTextBoxColumn();
dgvqty.HeaderText = "QTY";
dgvqty.Width = 100;
dataGridView1.Columns.Add(dgvqty);
DataGridViewTextBoxColumn dgvamt = new DataGridViewTextBoxColumn();
dgvamt.HeaderText = "Amount";
dgvamt.Width = 150;
dataGridView1.Columns.Add(dgvamt);
DataGridViewTextBoxColumn dgvmanu = new DataGridViewTextBoxColumn();
dgvmanu.HeaderText = "Manu. Date";
dgvmanu.Width = 100;
dataGridView1.Columns.Add(dgvmanu);
DataGridViewTextBoxColumn dgvexp = new DataGridViewTextBoxColumn();
dgvexp.HeaderText = "Exp. Date";
dgvexp.Width = 100;
dataGridView1.Columns.Add(dgvexp);
DataGridViewTextBoxColumn dgvlot = new DataGridViewTextBoxColumn();
dgvlot.HeaderText = "Lot #";
dgvlot.Width = 100;
dataGridView1.Columns.Add(dgvlot);
cc();
cc2();
cc3();
}
public void cc()
{
conn.Open();
SqlCommand cmd = conn.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "SELECT Department from tbldept";
cmd.ExecuteNonQuery();
DataTable dt = new DataTable();
SqlDataAdapter Da = new SqlDataAdapter(cmd);
Da.Fill(dt);
foreach (DataRow dr in dt.Rows)
{
comboBox1.Items.Add(dr["Department"].ToString());
comboBox2.Items.Add(dr["Department"].ToString());
}
conn.Close();
}
public void cc2()
{
conn.Open();
SqlCommand cmd = conn.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "select distinct Supplier from tblmaster";
cmd.ExecuteNonQuery();
DataTable dt = new DataTable();
SqlDataAdapter Da = new SqlDataAdapter(cmd);
Da.Fill(dt);
foreach (DataRow dr in dt.Rows)
{
comboBox3.Items.Add(dr["Supplier"].ToString());
}
conn.Close();
}
public void cc3()
{
conn.Open();
SqlCommand cmd = conn.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "SELECT [Transaction] from [tbltransac]";
cmd.ExecuteNonQuery();
DataTable dt = new DataTable();
SqlDataAdapter Da = new SqlDataAdapter(cmd);
Da.Fill(dt);
foreach (DataRow dr in dt.Rows)
{
comboBox4.Items.Add(dr["Transaction"].ToString());
}
conn.Close();
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
conn.Open();
SqlCommand cmd = conn.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "select * from tbldept where Department = '" + comboBox1.SelectedItem.ToString() + "'";
cmd.ExecuteNonQuery();
DataTable dt = new DataTable();
SqlDataReader dr = cmd.ExecuteReader();
SqlDataAdapter Da = new SqlDataAdapter(cmd);
conn.Close();
}
private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
{
conn.Open();
SqlCommand cmd = conn.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "select * from tbldept where Department = '" + comboBox2.SelectedItem.ToString() + "'";
cmd.ExecuteNonQuery();
DataTable dt = new DataTable();
SqlDataReader dr = cmd.ExecuteReader();
SqlDataAdapter Da = new SqlDataAdapter(cmd);
conn.Close();
}
private void comboBox3_SelectedIndexChanged(object sender, EventArgs e)
{
conn.Open();
SqlCommand cmd = conn.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "select * from tblmaster where Supplier = '" + comboBox3.SelectedItem.ToString() + "'";
cmd.ExecuteNonQuery();
DataTable dt = new DataTable();
SqlDataAdapter Da = new SqlDataAdapter(cmd);
Da.Fill(dt);
conn.Close();
}
private void comboBox4_SelectedIndexChanged(object sender, EventArgs e)
{
conn.Open();
SqlCommand cmd = conn.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "select * from [tbltransac] where [Transaction] = '" + comboBox4.SelectedItem.ToString() + "'";
cmd.ExecuteNonQuery();
DataTable dt = new DataTable();
SqlDataAdapter Da = new SqlDataAdapter(cmd);
conn.Close();
}
//private void comboBox5_SelectedIndexChanged(object sender, EventArgs e)
//{
// conn.Open();
// SqlCommand cmd = conn.CreateCommand();
// cmd.CommandType = CommandType.Text;
// cmd.CommandText = "select * from tblmaster where ProductCode = '" + comboBox5.SelectedItem.ToString() + "'";
// cmd.ExecuteNonQuery();
// DataTable dt = new DataTable();
// SqlDataAdapter Da = new SqlDataAdapter(cmd);
// Da.Fill(dt);
// foreach (DataRow dr in dt.Rows)
// {
// textBox3.Text = dr["Description"].ToString();
// textBox4.Text = dr["UM"].ToString();
// textBox5.Text = dr["UP"].ToString();
// }
// conn.Close();
//}
public AutoCompleteStringCollection AutoCompleteLoad()
{
SqlConnection conn = new SqlConnection(@"Data Source=DESKTOP-UAK52DL;Initial Catalog=Inventory;Integrated Security=True");
SqlCommand cmd = new SqlCommand("Select ProductCode from tblmaster", conn);
conn.Open();
SqlDataReader dr = cmd.ExecuteReader();
AutoCompleteStringCollection mycoll = new AutoCompleteStringCollection();
while (dr.Read())
{
mycoll.Add(dr["ProductCode"].ToString());
}
return mycoll;
}
private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
}
private void dataGridView1_EditingControlShowing_1(object sender, DataGridViewEditingControlShowingEventArgs e)
{
int column = dataGridView1.CurrentCell.ColumnIndex;
string headerText = dataGridView1.Columns[column].HeaderText;
if (headerText.Equals("Product Code"))
{
TextBox tb = e.Control as TextBox;
if (tb != null)
{
tb.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
tb.AutoCompleteCustomSource = AutoCompleteLoad();
tb.AutoCompleteSource = AutoCompleteSource.CustomSource;
//tb.Add()
}
}
else
{
TextBox tb = e.Control as TextBox;
if (tb != null)
{
tb.AutoCompleteMode = AutoCompleteMode.None;
}
}
}
private void comboBox3_SelectedIndexChanged_1(object sender, EventArgs e)
{
}
}
}
c# sql autocomplete
add a comment |
Good day guys, I am asking you a simple favor, I am trying to make a datagridview textbox with autocomplete, but the problem with the default autocomplete is that it only read at the prefix. What I want to do is something like you will saw in this site LINK but I can't make it work in datagridview as I am a bit confused on how I will integrate it in mine, below is my full code:
public Form2()
{
InitializeComponent();
}
SqlConnection conn = new SqlConnection(@"Data Source=DESKTOP-UAK52DL;Initial Catalog=Inventory;Integrated Security=True");
SqlCommand cd = new SqlCommand();
private void label15_Click(object sender, EventArgs e)
{
}
private void Form2_Load(object sender, EventArgs e)
{
DataGridViewTextBoxColumn dgvslno = new DataGridViewTextBoxColumn();
dgvslno.HeaderText = "Product Code";
dgvslno.Width = 200;
dataGridView1.Columns.Add(dgvslno);
DataGridViewTextBoxColumn dgvpro = new DataGridViewTextBoxColumn();
dgvpro.HeaderText = "Description";
dgvpro.Width = 300;
dataGridView1.Columns.Add(dgvpro);
DataGridViewTextBoxColumn dgvum = new DataGridViewTextBoxColumn();
dgvum.HeaderText = "UM";
dgvum.Width = 50;
dataGridView1.Columns.Add(dgvum);
DataGridViewTextBoxColumn dgvup = new DataGridViewTextBoxColumn();
dgvup.HeaderText = "UP";
dgvup.Width = 100;
dataGridView1.Columns.Add(dgvup);
DataGridViewTextBoxColumn dgvqty = new DataGridViewTextBoxColumn();
dgvqty.HeaderText = "QTY";
dgvqty.Width = 100;
dataGridView1.Columns.Add(dgvqty);
DataGridViewTextBoxColumn dgvamt = new DataGridViewTextBoxColumn();
dgvamt.HeaderText = "Amount";
dgvamt.Width = 150;
dataGridView1.Columns.Add(dgvamt);
DataGridViewTextBoxColumn dgvmanu = new DataGridViewTextBoxColumn();
dgvmanu.HeaderText = "Manu. Date";
dgvmanu.Width = 100;
dataGridView1.Columns.Add(dgvmanu);
DataGridViewTextBoxColumn dgvexp = new DataGridViewTextBoxColumn();
dgvexp.HeaderText = "Exp. Date";
dgvexp.Width = 100;
dataGridView1.Columns.Add(dgvexp);
DataGridViewTextBoxColumn dgvlot = new DataGridViewTextBoxColumn();
dgvlot.HeaderText = "Lot #";
dgvlot.Width = 100;
dataGridView1.Columns.Add(dgvlot);
cc();
cc2();
cc3();
}
public void cc()
{
conn.Open();
SqlCommand cmd = conn.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "SELECT Department from tbldept";
cmd.ExecuteNonQuery();
DataTable dt = new DataTable();
SqlDataAdapter Da = new SqlDataAdapter(cmd);
Da.Fill(dt);
foreach (DataRow dr in dt.Rows)
{
comboBox1.Items.Add(dr["Department"].ToString());
comboBox2.Items.Add(dr["Department"].ToString());
}
conn.Close();
}
public void cc2()
{
conn.Open();
SqlCommand cmd = conn.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "select distinct Supplier from tblmaster";
cmd.ExecuteNonQuery();
DataTable dt = new DataTable();
SqlDataAdapter Da = new SqlDataAdapter(cmd);
Da.Fill(dt);
foreach (DataRow dr in dt.Rows)
{
comboBox3.Items.Add(dr["Supplier"].ToString());
}
conn.Close();
}
public void cc3()
{
conn.Open();
SqlCommand cmd = conn.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "SELECT [Transaction] from [tbltransac]";
cmd.ExecuteNonQuery();
DataTable dt = new DataTable();
SqlDataAdapter Da = new SqlDataAdapter(cmd);
Da.Fill(dt);
foreach (DataRow dr in dt.Rows)
{
comboBox4.Items.Add(dr["Transaction"].ToString());
}
conn.Close();
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
conn.Open();
SqlCommand cmd = conn.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "select * from tbldept where Department = '" + comboBox1.SelectedItem.ToString() + "'";
cmd.ExecuteNonQuery();
DataTable dt = new DataTable();
SqlDataReader dr = cmd.ExecuteReader();
SqlDataAdapter Da = new SqlDataAdapter(cmd);
conn.Close();
}
private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
{
conn.Open();
SqlCommand cmd = conn.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "select * from tbldept where Department = '" + comboBox2.SelectedItem.ToString() + "'";
cmd.ExecuteNonQuery();
DataTable dt = new DataTable();
SqlDataReader dr = cmd.ExecuteReader();
SqlDataAdapter Da = new SqlDataAdapter(cmd);
conn.Close();
}
private void comboBox3_SelectedIndexChanged(object sender, EventArgs e)
{
conn.Open();
SqlCommand cmd = conn.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "select * from tblmaster where Supplier = '" + comboBox3.SelectedItem.ToString() + "'";
cmd.ExecuteNonQuery();
DataTable dt = new DataTable();
SqlDataAdapter Da = new SqlDataAdapter(cmd);
Da.Fill(dt);
conn.Close();
}
private void comboBox4_SelectedIndexChanged(object sender, EventArgs e)
{
conn.Open();
SqlCommand cmd = conn.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "select * from [tbltransac] where [Transaction] = '" + comboBox4.SelectedItem.ToString() + "'";
cmd.ExecuteNonQuery();
DataTable dt = new DataTable();
SqlDataAdapter Da = new SqlDataAdapter(cmd);
conn.Close();
}
//private void comboBox5_SelectedIndexChanged(object sender, EventArgs e)
//{
// conn.Open();
// SqlCommand cmd = conn.CreateCommand();
// cmd.CommandType = CommandType.Text;
// cmd.CommandText = "select * from tblmaster where ProductCode = '" + comboBox5.SelectedItem.ToString() + "'";
// cmd.ExecuteNonQuery();
// DataTable dt = new DataTable();
// SqlDataAdapter Da = new SqlDataAdapter(cmd);
// Da.Fill(dt);
// foreach (DataRow dr in dt.Rows)
// {
// textBox3.Text = dr["Description"].ToString();
// textBox4.Text = dr["UM"].ToString();
// textBox5.Text = dr["UP"].ToString();
// }
// conn.Close();
//}
public AutoCompleteStringCollection AutoCompleteLoad()
{
SqlConnection conn = new SqlConnection(@"Data Source=DESKTOP-UAK52DL;Initial Catalog=Inventory;Integrated Security=True");
SqlCommand cmd = new SqlCommand("Select ProductCode from tblmaster", conn);
conn.Open();
SqlDataReader dr = cmd.ExecuteReader();
AutoCompleteStringCollection mycoll = new AutoCompleteStringCollection();
while (dr.Read())
{
mycoll.Add(dr["ProductCode"].ToString());
}
return mycoll;
}
private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
}
private void dataGridView1_EditingControlShowing_1(object sender, DataGridViewEditingControlShowingEventArgs e)
{
int column = dataGridView1.CurrentCell.ColumnIndex;
string headerText = dataGridView1.Columns[column].HeaderText;
if (headerText.Equals("Product Code"))
{
TextBox tb = e.Control as TextBox;
if (tb != null)
{
tb.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
tb.AutoCompleteCustomSource = AutoCompleteLoad();
tb.AutoCompleteSource = AutoCompleteSource.CustomSource;
//tb.Add()
}
}
else
{
TextBox tb = e.Control as TextBox;
if (tb != null)
{
tb.AutoCompleteMode = AutoCompleteMode.None;
}
}
}
private void comboBox3_SelectedIndexChanged_1(object sender, EventArgs e)
{
}
}
}
c# sql autocomplete
Good day guys, I am asking you a simple favor, I am trying to make a datagridview textbox with autocomplete, but the problem with the default autocomplete is that it only read at the prefix. What I want to do is something like you will saw in this site LINK but I can't make it work in datagridview as I am a bit confused on how I will integrate it in mine, below is my full code:
public Form2()
{
InitializeComponent();
}
SqlConnection conn = new SqlConnection(@"Data Source=DESKTOP-UAK52DL;Initial Catalog=Inventory;Integrated Security=True");
SqlCommand cd = new SqlCommand();
private void label15_Click(object sender, EventArgs e)
{
}
private void Form2_Load(object sender, EventArgs e)
{
DataGridViewTextBoxColumn dgvslno = new DataGridViewTextBoxColumn();
dgvslno.HeaderText = "Product Code";
dgvslno.Width = 200;
dataGridView1.Columns.Add(dgvslno);
DataGridViewTextBoxColumn dgvpro = new DataGridViewTextBoxColumn();
dgvpro.HeaderText = "Description";
dgvpro.Width = 300;
dataGridView1.Columns.Add(dgvpro);
DataGridViewTextBoxColumn dgvum = new DataGridViewTextBoxColumn();
dgvum.HeaderText = "UM";
dgvum.Width = 50;
dataGridView1.Columns.Add(dgvum);
DataGridViewTextBoxColumn dgvup = new DataGridViewTextBoxColumn();
dgvup.HeaderText = "UP";
dgvup.Width = 100;
dataGridView1.Columns.Add(dgvup);
DataGridViewTextBoxColumn dgvqty = new DataGridViewTextBoxColumn();
dgvqty.HeaderText = "QTY";
dgvqty.Width = 100;
dataGridView1.Columns.Add(dgvqty);
DataGridViewTextBoxColumn dgvamt = new DataGridViewTextBoxColumn();
dgvamt.HeaderText = "Amount";
dgvamt.Width = 150;
dataGridView1.Columns.Add(dgvamt);
DataGridViewTextBoxColumn dgvmanu = new DataGridViewTextBoxColumn();
dgvmanu.HeaderText = "Manu. Date";
dgvmanu.Width = 100;
dataGridView1.Columns.Add(dgvmanu);
DataGridViewTextBoxColumn dgvexp = new DataGridViewTextBoxColumn();
dgvexp.HeaderText = "Exp. Date";
dgvexp.Width = 100;
dataGridView1.Columns.Add(dgvexp);
DataGridViewTextBoxColumn dgvlot = new DataGridViewTextBoxColumn();
dgvlot.HeaderText = "Lot #";
dgvlot.Width = 100;
dataGridView1.Columns.Add(dgvlot);
cc();
cc2();
cc3();
}
public void cc()
{
conn.Open();
SqlCommand cmd = conn.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "SELECT Department from tbldept";
cmd.ExecuteNonQuery();
DataTable dt = new DataTable();
SqlDataAdapter Da = new SqlDataAdapter(cmd);
Da.Fill(dt);
foreach (DataRow dr in dt.Rows)
{
comboBox1.Items.Add(dr["Department"].ToString());
comboBox2.Items.Add(dr["Department"].ToString());
}
conn.Close();
}
public void cc2()
{
conn.Open();
SqlCommand cmd = conn.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "select distinct Supplier from tblmaster";
cmd.ExecuteNonQuery();
DataTable dt = new DataTable();
SqlDataAdapter Da = new SqlDataAdapter(cmd);
Da.Fill(dt);
foreach (DataRow dr in dt.Rows)
{
comboBox3.Items.Add(dr["Supplier"].ToString());
}
conn.Close();
}
public void cc3()
{
conn.Open();
SqlCommand cmd = conn.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "SELECT [Transaction] from [tbltransac]";
cmd.ExecuteNonQuery();
DataTable dt = new DataTable();
SqlDataAdapter Da = new SqlDataAdapter(cmd);
Da.Fill(dt);
foreach (DataRow dr in dt.Rows)
{
comboBox4.Items.Add(dr["Transaction"].ToString());
}
conn.Close();
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
conn.Open();
SqlCommand cmd = conn.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "select * from tbldept where Department = '" + comboBox1.SelectedItem.ToString() + "'";
cmd.ExecuteNonQuery();
DataTable dt = new DataTable();
SqlDataReader dr = cmd.ExecuteReader();
SqlDataAdapter Da = new SqlDataAdapter(cmd);
conn.Close();
}
private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
{
conn.Open();
SqlCommand cmd = conn.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "select * from tbldept where Department = '" + comboBox2.SelectedItem.ToString() + "'";
cmd.ExecuteNonQuery();
DataTable dt = new DataTable();
SqlDataReader dr = cmd.ExecuteReader();
SqlDataAdapter Da = new SqlDataAdapter(cmd);
conn.Close();
}
private void comboBox3_SelectedIndexChanged(object sender, EventArgs e)
{
conn.Open();
SqlCommand cmd = conn.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "select * from tblmaster where Supplier = '" + comboBox3.SelectedItem.ToString() + "'";
cmd.ExecuteNonQuery();
DataTable dt = new DataTable();
SqlDataAdapter Da = new SqlDataAdapter(cmd);
Da.Fill(dt);
conn.Close();
}
private void comboBox4_SelectedIndexChanged(object sender, EventArgs e)
{
conn.Open();
SqlCommand cmd = conn.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "select * from [tbltransac] where [Transaction] = '" + comboBox4.SelectedItem.ToString() + "'";
cmd.ExecuteNonQuery();
DataTable dt = new DataTable();
SqlDataAdapter Da = new SqlDataAdapter(cmd);
conn.Close();
}
//private void comboBox5_SelectedIndexChanged(object sender, EventArgs e)
//{
// conn.Open();
// SqlCommand cmd = conn.CreateCommand();
// cmd.CommandType = CommandType.Text;
// cmd.CommandText = "select * from tblmaster where ProductCode = '" + comboBox5.SelectedItem.ToString() + "'";
// cmd.ExecuteNonQuery();
// DataTable dt = new DataTable();
// SqlDataAdapter Da = new SqlDataAdapter(cmd);
// Da.Fill(dt);
// foreach (DataRow dr in dt.Rows)
// {
// textBox3.Text = dr["Description"].ToString();
// textBox4.Text = dr["UM"].ToString();
// textBox5.Text = dr["UP"].ToString();
// }
// conn.Close();
//}
public AutoCompleteStringCollection AutoCompleteLoad()
{
SqlConnection conn = new SqlConnection(@"Data Source=DESKTOP-UAK52DL;Initial Catalog=Inventory;Integrated Security=True");
SqlCommand cmd = new SqlCommand("Select ProductCode from tblmaster", conn);
conn.Open();
SqlDataReader dr = cmd.ExecuteReader();
AutoCompleteStringCollection mycoll = new AutoCompleteStringCollection();
while (dr.Read())
{
mycoll.Add(dr["ProductCode"].ToString());
}
return mycoll;
}
private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
}
private void dataGridView1_EditingControlShowing_1(object sender, DataGridViewEditingControlShowingEventArgs e)
{
int column = dataGridView1.CurrentCell.ColumnIndex;
string headerText = dataGridView1.Columns[column].HeaderText;
if (headerText.Equals("Product Code"))
{
TextBox tb = e.Control as TextBox;
if (tb != null)
{
tb.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
tb.AutoCompleteCustomSource = AutoCompleteLoad();
tb.AutoCompleteSource = AutoCompleteSource.CustomSource;
//tb.Add()
}
}
else
{
TextBox tb = e.Control as TextBox;
if (tb != null)
{
tb.AutoCompleteMode = AutoCompleteMode.None;
}
}
}
private void comboBox3_SelectedIndexChanged_1(object sender, EventArgs e)
{
}
}
}
c# sql autocomplete
c# sql autocomplete
asked Dec 29 '18 at 2:31
Ysmael CailonYsmael Cailon
14
14
add a comment |
add a comment |
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53966209%2fautocomplete-datagridview-textbox-with-substring-search-similar-to-sql-like-or%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53966209%2fautocomplete-datagridview-textbox-with-substring-search-similar-to-sql-like-or%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown