wpf - Not able to make cascading comboboxes work -
here code create cascading comboboxes. trying populate family combobox(combobox2) based on value selected segment name(combox1).i able populate first combo static resource second 1 shows blank. calling method called "fillcomboboxfamilydata(segmentcode)" on selection change event of first combobox.
xaml code:
<grid height="142" horizontalalignment="left" margin="49,113,0,0" name="grid3" verticalalignment="top" width="904"> <combobox height="23" horizontalalignment="left" margin="35,26,0,0" name="combobox1" verticalalignment="top" width="205" itemssource="{binding source={staticresource tblsegmentviewsource}}" displaymemberpath="segment name" selectedvaluepath="segment code" selectionchanged="combobox1_selectionchanged"/> <combobox height="23" horizontalalignment="right" margin="0,26,395,0" name="combobox2" verticalalignment="top" width="205" /> <window.resources> <collectionviewsource x:key="tblsegmentviewsource" source="{binding path=tblsegment, source={staticresource brickdataset}}" /> <collectionviewsource x:key="tblfamilyviewsource" source="{binding path=tblfamily, source={staticresource brickdataset}}" />
*brickdataset main dataset pulling tables from.*
c#:
private void combobox1_selectionchanged(object sender, selectionchangedeventargs e) { messagebox.show(combobox1.selectedvalue.tostring()); segmentcode = convert.toint32(combobox1.selectedvalue.tostring()); fillcomboboxfamilydata(segmentcode); } public void fillcomboboxfamilydata(int segment_code) { string connstring = "data source=.\\sqlexpress;attachdbfilename=c:\\documents , settings\\dchaman\\my documents\\pdrt.mdf;integrated security=true;connect timeout=30;user instance=true "; sqlconnection con = new sqlconnection(connstring); con.open(); sqlcommand cmd = new sqlcommand(); cmd.connection = con; cmd.commandtype = system.data.commandtype.text; cmd.commandtext = "select tblfamily.[family name],tblfamily.[family code] tblfamily (tblfamily.[segment code] = @segmentcode)"; cmd.parameters.addwithvalue("@segmentcode", segment_code); dataset objds = new dataset(); sqldataadapter dadapter = new sqldataadapter(); dadapter.selectcommand = cmd; dadapter.fill(objds); con.close(); if (objds.tables[0].rows.count > 0) { messagebox.show("here am"); combobox2.datacontext = objds.tables; combobox2.items.insert(0, "--select family name--"); combobox2.displaymemberpath = "family name"; combobox2.selectedvalue = "family code"; } }
i banging head table right now.please save me!
you aren't setting combobox2's itemssource
. datacontext
effect binding statements on it. if set itemssource
correct table instead of datacontext
, should on right path:
if (objds.tables[0].rows.count > 0) { messagebox.show("here am"); combobox2.itemssource = ((ilistsource)objds.tables[0]).getlist(); // set itemssource instead of datacontext combobox2.displaymemberpath = "family name"; combobox2.selectedvalue = "family code"; }
note when set itemssource
, line inserts text items
property fail, can not use both itemssource
, items
together.
Comments
Post a Comment