Reputation: 7
I have a combox box which corresponds to a data grid , when I select a value with the selected index changed function the data grid reflect this.
The problem : The combobox currently displays info like this
Sep 2 2013 1:54PM
Sep 2 2013 1:55PM
Sep 3 2013 1:54PM
What I want : I want the combobox to keep the same functionalty e.g when I select a value it interactes with the datagrid , But i want it only to display a DISTINCT date e.g the combobox should display :
Sep 2 2013
Sep 3 2013 etc
Anyone got any ideas how to do this ? I'm new to c# and wpf so not sure how
Upvotes: 0
Views: 3347
Reputation: 1
DataTable dt = new DataTable();
SqlConnection con = new SqlConnection("Data Source=localhost;Initial Catalog=testing;Userid=abc;pwd=abc");
SqlCommand cmd = new SqlCommand("select ID,date from tbl_test",con);
con.Open();
sqldatareader drw = cmd.ExecuteReader();
dt.Load(drw);
DataRow dr = dt.NewRow();
dr["id"] = "0";
dr["date"] = "Select date";
dt.Rows.InsertAt(dr, 0);
cmb_test.DataSource = dt;
cmb_test.DisplayMember = "date";
cmb_test.ValueMember = "id";
cmb_test.SelectedIndex = 0;
selected changed event change code:
sqlcommand cmd = new sqlcommand("select ID,date from tbl_test where id="+cmb_test_send.SelectedValue+",con);
con.Open();
sqldatareader drw = cmd.ExecuteReader();
dt.Load(drw);
sample_datagridview.DataSource=dt; `
Upvotes: 0
Reputation: 1002
You could use linq to limit your data and bind it to the datagrid.
I don't know what your data looks like but if its a list or array of strings you could do something like this.
//this gets the first 11 characters of the date, you may have to change it slightly for days that have 2 digits but this is just an example, it then makes a distinct list.
dropdown.DataSource = data.Select(s=>s.SubString(0,11)).Distinct();
datagrid.DataSource = data.Where(w=> w.StartsWith(dropdown.SelectedValue));
There maybe better ways of doing it but this may help. You may have to add using statements if you havent got them already. System.Collections.Generic and System.Linq
Remember to rebind after.
Upvotes: 1
Reputation: 32671
For this you need to filter out your binding source before binding to the data grid.
Upvotes: 0