How to select a complex/composite key in C# using combo-boxes?
I have a problem with C#.NET (Visual Studio 2010). I have 2 datatables in SQL2005 express :
- Building which its primary key is "building number",
- and Aparatment which its primary key is "building number" (foreign key to Building table) + "apartment number". (apartment is weak entity of building and its key is composite).
Both tables are part of a greater database which I've imported into the project as DataSet. I want to show the apartment details when I choose the building number and apartment number. Building number is set in an textbox (which its values are always building numbers chosen by a different control) and I want a combo-box to show only apartment numbers that the building in textbox has and not all apartment numbers in the table (wh开发者_StackOverflowich results in something like 1, 2, 3, 1, 4, 5, 1, 2, ... (because it selects all the rows)). Not all the buildings share the same apartment numbers or same the same quantity of apartments which I should add/change/delete on the fly.
Choosing the building part is done and running. I just have problem with showing only the current building apartments (at least the apartment numbers I need).
I don't want (more precisely not allowed) to use datagrid. "Detailed" controls only.
How do I do that? I have little experience with C# and I don't know how to work "to the max" with DataSets either. I've thought of maybe to create a view that will give me the results but I don't know how to import a view and set the building number as a parameter. Any other sane way is also welcome.
Please help.
Many thanks ahead, Shay.
I would create two dropdowns for this where their autopostback is set to true. On page load I would select all building ids and place them in its dropdown. When the dropdown is changed, an onchange event is fired which populates the 2nd dropdown with all the apartment ids for that corresponding building id.
OK I digged a little more here and found the answer to the second question. Thanks Ross for the DataRow[] solution and how to populate it (the first step that is). For future generations here's the relevant code:
DataRow[] apartmentCollection = dbBonusHwDataSet.tblApartment.Select("buildingNum='" + Convert.ToInt16(buildingNum_textbox.Text.ToString()) + "'");
apartmentNum_combox.DataSource = apartmentCollection;
apartmentNum_combox.DisplayMember = "apartmentNum";
I don't know why the code block is not displayed right. Sorry for using IE right now. But I can't see the code in the code tab...
精彩评论