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?
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); } }
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)