Faced such a problem. As if everything works, click on any value in the DataGridView, and it automatically appears in TextBox through DataBings, for further editing. But when I find a value in a DataGridView through a search, it already stops displaying in TextBoxes. What's my mistake ?

Here is the search code:

private void textBox1_KeyPress(object sender, KeyPressEventArgs e) { if (e.KeyChar == (char)13) { if (string.IsNullOrEmpty(textBox1.Text)) { dataGridView1.DataSource = serijospakavimoprotokolasBindingSource; } else { var query = from o in serijospakavimoprotokolasBindingSource.DataSource as List<Serijos_pakavimo_protokolas> where o.Serijos_Nr == textBox1.Text.Trim() || o.Serijos_Nr.Contains(textBox1.Text) || o.Pavadinimas.Contains(textBox1.Text) || o.Pakuote == textBox1.Text || o.Serijos_dydis.Contains(textBox1.Text) select o; dataGridView1.DataSource = query.ToList(); } } } 

Update

 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Data.Entity; using System.Data.Entity.Infrastructure; using System.Data.SqlClient; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace WindowsFormsApplication11 { public partial class Form1 : Form { SVF_ProtokolaiEntities serija; public Form1() { InitializeComponent(); } private void bntNew_Click(object sender, EventArgs e) //New insert { try { tabControl1.Enabled = true; textBox15.Focus(); Serijos_pakavimo_protokolas c = new Serijos_pakavimo_protokolas(); serija.Serijos_pakavimo_protokolas.Add(c); serijospakavimoprotokolasBindingSource.Add(c); serijospakavimoprotokolasBindingSource.MoveLast(); } catch (Exception ex) { MessageBox.Show(ex.Message, "Message", MessageBoxButtons.OK, MessageBoxIcon.Error); } } private void btnEdit_Click(object sender, EventArgs e) //edit { tabControl1.Enabled = true; textBox15.Focus(); } private void btnCancel_Click(object sender, EventArgs e) //cancel edit { tabControl1.Enabled = false; serijospakavimoprotokolasBindingSource.ResetBindings(false); foreach (DbEntityEntry entry in serija.ChangeTracker.Entries()) { switch (entry.State) { case EntityState.Added: entry.State = EntityState.Detached; break; case EntityState.Modified: entry.State = EntityState.Unchanged; break; case EntityState.Deleted: entry.Reload(); break; } } } private void btnSave_Click(object sender, EventArgs e) //save { try { serijospakavimoprotokolasBindingSource.EndEdit(); serija.SaveChangesAsync(); tabControl1.Enabled = false; } catch (Exception ex) { MessageBox.Show(ex.Message, "Message", MessageBoxButtons.OK, MessageBoxIcon.Error); serijospakavimoprotokolasBindingSource.ResetBindings(false); } } private void Form1_Load(object sender, EventArgs e) //Form { tabControl1.Enabled = false; serija = new SVF_ProtokolaiEntities(); serijospakavimoprotokolasBindingSource.DataSource = serija.Serijos_pakavimo_protokolas.ToList(); } private void dataGridView1_KeyDown(object sender, KeyEventArgs e) //del from grid { if (e.KeyCode == Keys.Delete) { if (MessageBox.Show("Ar tikrai norite ištrinti ?", "Message", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) { serija.Serijos_pakavimo_protokolas.Remove(serijospakavimoprotokolasBindingSource.Current as Serijos_pakavimo_protokolas); serijospakavimoprotokolasBindingSource.RemoveCurrent(); } } } private void textBox1_KeyPress(object sender, KeyPressEventArgs e) //search { if (e.KeyChar == (char)13) { if (string.IsNullOrEmpty(textBox1.Text)) { dataGridView1.DataSource = serijospakavimoprotokolasBindingSource; } else { var query = from o in serijospakavimoprotokolasBindingSource.DataSource as List<Serijos_pakavimo_protokolas> where o.Serijos_Nr == textBox1.Text.Trim() || o.Serijos_Nr.Contains(textBox1.Text) || o.Pavadinimas.Contains(textBox1.Text) || o.Pakuote == textBox1.Text || o.Serijos_dydis.Contains(textBox1.Text) select o; dataGridView1.DataSource = query.ToList(); } } } private void button1_Click(object sender, EventArgs e) //Eksporto mygtukas { Form2 fr2 = new Form2(); fr2.ShowDialog(); } private void btnSearch_Click(object sender, EventArgs e) { } private void button2_Click(object sender, EventArgs e) { } } } 

Before searching

before searching

After searching

after searching

Finds, but does not display.

  • The last line assigns a new data source from the filtered query. And to textboxes, as I understand it, the original source remains attached. - Alexander Petrov
  • Right, but I need a result from the search to be displayed in text boxes - Vadim
  • Ok, I will continue to fix the code without seeing it. After assigning a new data source to the grid, textboxes need to add this source (without forgetting to clear the old one). Or instead of List<T> take a collection that supports filtering, for example, DataTable . - Alexander Petrov
  • If you are given an exhaustive answer, mark it as correct (a daw opposite the selected answer). - Nicolas Chabanovsky ♦
  • no final decision yet - Vadim

1 answer 1

 if (e.KeyChar == (char)13) { string text = textBox1.Text.Trim(); if (text == string.Empty) { dataGridView1.DataSource = serijospakavimoprotokolasBindingSource; textBoxNumber.DataBindings.Clear(); textBoxNumber.DataBindings.Add("Text", serijospakavimoprotokolasBindingSource, "Serijos_Nr"); } else { var query = (from o in serijospakavimoprotokolasBindingSource.DataSource as List<Serijos_pakavimo_protokolas> where o.Serijos_Nr.Contains(text) || o.Pavadinimas.Contains(text) || o.Pakuote == text || o.Serijos_dydis.Contains(text) select o).ToList(); if (query.Any()) { dataGridView1.DataSource = query; textBoxNumber.DataBindings.Clear(); textBoxNumber.DataBindings.Add("Text", query, "Serijos_Nr"); } } } 

textBoxNumber is the text box to which the binding was made. Replace the title with the one you have.
"Serijos_Nr" is the property to which this textbox is bound. Also replace the correct one. But I think I guessed it.

The logic is elementary: when something is found, we bind the datagrid and textbox to the found data. When nothing is found, we return the original bindings.


Code Tips: Give all the control speaking names. What is textBox1 ? If it is intended to search / filter, then name it textBoxSearch or textBoxFilter . Etc.
I introduced an additional local variable, text , due to which the code was greatly reduced. Use a similar technique.
Here is the code: .DataSource as List<T> cumbersome and slow. Here it is better to directly use the list that is the original source of data.

  • I have more than 100 textboxes attached to one line from the datagrid. The search happens with tectbox1 - Vadim
  • @Vadim - Hmm ... Well, the binding code, generated by the designer, is copied and rendered to the method. And call it. If it is slow, you have to abandon this method and use, for example, DataTable - it has built-in filtering capability. - Alexander Petrov
  • I have more than 100 textboxes attached to one line from the datagrid. Search happens with tectbox1. Serijos_Nr textbox15. The logic is such that the upper textboxes of 6 pieces are filled at once, and in the course of production, it is necessary to find the necessary series and continue as far as possible to fill and save. - Vadim
  • Maybe you have some other option or example for my task, or maybe you as a professional could write a decent example? I'll send you tea from production :) - Vadim
  • @ Vadim - Generated 200 textboxes, pribindil. Works without brakes. Just have to copy the binding code of all your textboxes. - Alexander Petrov