Jayanta Mukherjee

Jayanta Mukherjee

  • NA
  • 120
  • 43.1k

Datagrid updating with previous selection data from combo box !

Apr 23 2022 1:00 AM

When I select an item from the combo box the datagrid should filter its data and show the filtered data in the datagrid. I've implemented the below code on the combo box's SelectionChanged event

			using(SQLiteConnection conn= new SQLiteConnection(@"Data Source="+Path.GetFullPath("./test.db")))
			{
				conn.Open();

				SQLiteCommand command = new SQLiteCommand("SELECT * FROM tbl WHERE Name LIKE @name", conn);
				command.Parameters.AddWithValue("@name", cmb.Text);
				command.ExecuteNonQuery();
				
				SQLiteDataAdapter adap = new SQLiteDataAdapter(command);
				
				DataTable dt = new DataTable("wth");
				adap.Fill(dt);
				
				dg.ItemsSource=dt.DefaultView;
				
				conn.Close();
			}

But when I first select an item from the combo box then datagrid shows nothing, then the 2nd time when I select an item from the combo box it filters the data as per my first selection and shows data in datagrid and so on...

How do I fix this?