Click here to Skip to main content
15,843,593 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I created AutoComplete Textbox for a GridView. I have selected value from two columns from the database, one (Kontonummer) and the other is (Navn). Now if I select value from textbox, both value comes from both columns (Kontonummer and Navn) in the GridView Cell (Konto), But I only need the column (Kontonummer) value to get in the GridView Cell (Konto). Can you help me with that?


What I have tried:

private void dgvAddKontoNr_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
    {
        try
        {                
            string headerText = dgvAddKontoNr.Columns["Konto"].HeaderText;

            if (dgvAddKontoNr.CurrentCell.ColumnIndex == 3)
            {
                if (headerText.Equals("Konto"))
                {
                    if (e.Control is TextBox)
                    {
                        tb = e.Control as TextBox;
                        if (tb != null)
                        {
                            tb.AutoCompleteMode = AutoCompleteMode.Suggest;
                            tb.AutoCompleteSource = AutoCompleteSource.CustomSource;
                            AutoCompleteStringCollection coll = new AutoCompleteStringCollection();
                            tb.AutoCompleteCustomSource = coll;
                            con.Open();
                            SqlDataAdapter da = new SqlDataAdapter("SELECT CAST(Kontonummer AS nvarchar (255)) + ' - ' + Navn AS Kontonummer FROM Kontoplan WHERE LockKonto = 'True' AND KlientId = (SELECT Id FROM Klient WHERE Navn = @Navn)", con);
                            da.SelectCommand.Parameters.AddWithValue("@Navn", bogf.cmbKlient.Text);
                            DataTable dt = new DataTable();
                            da.Fill(dt);

                            for (int i = 0; i < dt.Rows.Count; i++)
                            {
                                string konto = dt.Rows[i]["Kontonummer"].ToString();
                                //string navn = dt.Rows[i]["Navn"].ToString();
                                coll.Add(konto);
                                //coll.Add(navn);
                            }
                            con.Close();
                        }
                        tb.MinimumSize = new Size(345, 25);
                        tb.BorderStyle = BorderStyle.Fixed3D;
                    }                        
                }

                else
                {
                    TextBox tb = e.Control as TextBox;

                    if (tb != null)
                    {
                        tb.AutoCompleteMode = AutoCompleteMode.None;
                    }
                }
            }    
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }
Posted
Comments
Gerry Schmitz 15-Nov-20 10:59am    
No idea what you're talking about. Two of one; one of two ...
Member 14085040 17-Nov-20 16:54pm    
Hi Gerry.
Because the first column is code and the other is text, for each code has its text. But I only need the code in the Grideview.
BillWoodruff 15-Nov-20 23:33pm    
You need to describe clearly:

1. where is the TextBox ... in the grid, outside the grid ?

2. what is the source of the autocomplete data

3. what "multiple columns means here.
Member 14085040 17-Nov-20 16:50pm    
Hi BillWoodruff.
1. The TextBox is for displaying autocomlete data from Database.
2. The source is tables from the Database
3. Multiple columns are the 2 tables selected from DB for the TextBox to in grid, where I only need the first table displayed in the grid.
BillWoodruff 17-Nov-20 21:09pm    
Show the code that sets the AutoComplete data for the TextBox. Answer question #1 above.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900