Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get DataGridView AutoComplete to show for only one column?

Problem is the AutoComplete also shows up for columns other than column 1. It won't initially until I enter text in column 1 and then it will start showing for other columns.

My code :

public AutoCompleteStringCollection ClientListDropDown()
{
    AutoCompleteStringCollection asc = new AutoCompleteStringCollection();
    try
    {
        Query = "Select top 5 title from customer "; // just removed  where name like '%" + txtDVNo.Text + "%' plz check
        cmd = new SqlCommand(Query, GlobalVars.conn);
        dr = cmd.ExecuteReader();
        if ((dr != null) && (dr.HasRows))
            while (dr.Read())
                asc.Add(dr.GetValue(0).ToString());
        dr.Close();
        cmd.Dispose();
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
    return asc;
}

private void dgvDVDetails_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
    if (dgvDVDetails.CurrentCell.ColumnIndex == 1)
    {
        TextBox prodCode = e.Control as TextBox;
        if (prodCode != null)
        {
            prodCode.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
            prodCode.AutoCompleteCustomSource = ClientListDropDown();
            prodCode.AutoCompleteSource = AutoCompleteSource.CustomSource;

        }
    }
}
like image 919
user2982914 Avatar asked Feb 13 '23 16:02

user2982914


1 Answers

Try This:

private void dgvDVDetails_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
    if (dgvDVDetails.CurrentCell.ColumnIndex == 1)
    {
        TextBox prodCode = e.Control as TextBox;
        if (prodCode != null)
        {
            prodCode.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
            prodCode.AutoCompleteCustomSource = ClientListDropDown();
            prodCode.AutoCompleteSource = AutoCompleteSource.CustomSource;

        }
    }
   else
   {
       TextBox prodCode = e.Control as TextBox;
       if (prodCode != null)
        {
            prodCode.AutoCompleteMode = AutoCompleteMode.None;
        }
   }
}
like image 80
Sudhakar Tillapudi Avatar answered Feb 19 '23 21:02

Sudhakar Tillapudi