0

I use Combobox to filter Datagridview data loaded from a database. When form loads, datagridview and combobox get filled by data. Combobox doesn't have selected values, but when I choose an item from a dropdown, datagridview filters data just fine. But, when I want to start over by pressing button refresh, just to load data again in datagridview, only filtered data is shown.

How to get all data shown in datagridview before touching combobox?

Code so far...

private void form1_load(object sender, EventArgs e)
{
this.comboBox1.SelectedIndexChanged +=new System.EventHandler(comboBox1_SelectedIndexChanged);
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
dv = dt.DefaultView;
dv.RowFilter=string.Format("Status LIKE '%{0}'", comboBox1.SelectedItem.ToString());
dataGridView1.DataSource = dv;
}
private void button1refresh_Click(object sender, EventArgs e)
{
comboBox1.DataSource = null; //doesn't work
comboBox1.Text = ""; //clears first item in combobox, but still filters
comboBox1.Items.Clear(); //clears items but still filters
comboBox1.SelectedIndex = -1; //sets combobox to -1, but filters
comboBox1.ResetText(); //doesnt work
dt.Clear();
dataGridView1.DataSource = null;
dataGridView1.Rows.Clear();
dataGridView1.Refresh();
FilllData(); //invoke function to fill the datagridview again and combobox
//this function works great at first run, but after clicking on refresh, it show's only filtered data 
}

Thank you in advance

Boris P
  • 86
  • 2
  • 10
  • Please post the code for FillData(). – Melanie Aug 01 '16 at 19:57
  • well, filldata function only connects to database and selects data then populates datagridview and combobox, like conn = new MySqlConnection(connString); try { conn.Open(); MySqlCommand cmdsel = new MySqlCommand(query2, conn);MySqlDataAdapter da = new MySqlDataAdapter(cmdsel);da.Fill(dt);dataGridView1.DataSource = dt; conn.Close(); da.Dispose(); dt.Dispose(); } catch (Exception ex) { MessageBox.Show(ex.Message); } – Boris P Aug 01 '16 at 20:59
  • But what's query2? Should it return your entire dataset or something else? – Melanie Aug 02 '16 at 14:41
  • yes, query selects data from db from many tables, nothing else – Boris P Aug 03 '16 at 19:38
  • dv = dt.DefaultView; dv.RowFilter = ""; dataGridView1.DataSource = dv; this worked fine – Boris P Aug 05 '16 at 18:36

0 Answers0