You can't bind to same source and expect to get result you looking for.
To get what you want you need to unbind ddlb
. And instead, on event SelectedIndexChanged
or SelectedItemChanged
of ddla
you should filter the rows from the data table and load into the ddlb
.
There are actually few ways of doing it. One, is to have 2 tables in dataset and use relationship, with one combo bind to table a
and another bind to table b
. This way, when you click in a
, the related rows will show up in b
create cascading dropdownlist from cached data
-
01-06-2022 - |
Question
In my application I have written a main query like
SqlDataAdapter cmd0 = new SqlDataAdapter(select a,b,c,d, from table1,sqlconn)
then I have cached this query since table1 is a very huge table and I don't want to call the database everytime user makes some change.
DataSet ds0 = new DataSet();
cmd0.Fill(ds0);
DataView source0 = new DataView(ds0.Tables[0]);
DataTable dt0 = new DataTable();
cmd0.Fill(dt0);
Cache["data"] = ds0;
I have 2 dropdownlists for a and b. I get the cached column a and bind the data in the dropdownlists
//dropdown for a
DataSet dataset_a = new DataSet();
dataset_a = (DataSet)Cache["data"];
DataView dataview_a = dataset_a.Tables[0].DefaultView;
dataview_a.Sort = "a";
DataTable datatable_a = dataview_a.ToTable(true, "a");
ddla.DataSource = datatable_a;
ddla.DataTextField = "a";
ddla.DataValueField = "a";
ddla.DataBind();
Now I try to populate dropdownlist for b initally by the same way as I did for ddla
//dropdown for b
DataSet dataset_b = new DataSet();
dataset_b = (DataSet)Cache["data"];
DataView dataview_b = dataset_b.Tables[0].DefaultView;
dataview_b.Sort = "b";
DataTable datatable_b = dataview_b.ToTable(true, "b");
ddlb.DataSource = datatable_b;
ddlb.DataTextField = "b";
ddlb.DataValueField = "b";
ddlb.DataBind();
when I change my selection in ddla(dropdownlist for a) I want the dropdown list for b also to be changed. (Cascading dropdownlists)
So basically I want to fire the query like:
Select b from table1 where a=ddla.SelectedItem.ToString()
but I don't kno how to do it with the cached data.
Please help me! Thank you!!!
Solution
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow