My application is in Silverlight(c#.Net)
I have a form with three combo boxes,Country,State,City.
My requirement is I want to populate my State Combo box at the Selectionchanged Event of Country combo box.
When the user select some country than the State of that Country should be populated in State combo.
Database Tables:
- Country Table-(CountryID,CountryName)
- State Table-(StateID,StateName,CountryId)
- City Table-(CityID,CityName,StateId)
To retrieve value from Database im coding in Service.svc.cs File
This is my Code. This code will get Country Record.
public class GetCountry
{
public string CountryName { get; set; }
}
[OperationContract]
public List<GetCountry> GetCountryRecord()
{
using (Entities context = new Entities())
{
return (from c in context.CountryMasters
select new GetCountry
开发者_C百科 {
CountryName = c.CountryName,
}).ToList<GetCountry>();
}
}
//Code to Get State from State_Master table
public class GetState
{
public string StateName { get; set; }
}
[OperationContract]
public List<GetState> GetStateRecord(int countryId)
{
using (Entities context = new Entities())
{
return (from c in context.StateMasters
select new GetState
{
StateName = c.StateName,
}).ToList<GetState>();
}
}
//End of State Code
This is my code in Form.xaml.cs. It will populate countries in Country combo box
client = new ServiceReference1.AlumniServiceClient();
client.GetCountryRecordCompleted += (s, ea) =>
{
cboCountry.ItemsSource = ea.Result.Select(b => b.CountryName).ToList();
};
client.GetCountryRecordAsync();
Please Suggest me Changes in the query in my Service.svc.cs file to get my required result or any other way to get it.
return (**from c in context.StateMasters
select new GetState**
This is my code in Form.xaml.cs at selectionChanged Event of Country Combobox
private void cboCountry_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
var client = new ServiceReference1.AlumniServiceClient();
client.GetStateRecordCompleted += (s, ea) =>
{
cboState.ItemsSource = ea.Result.Select(b => b.StateName).ToList(); };
client.GetStateRecordAsync();
}
But this will get me All the States present in the StatesMaster
This might not answer your direct question but I believe its a better solution to what you are trying to achieve
Let your Country data construct on the client side have a property of ObservableCollection<State>
, then bind the State ComboBox
's ItemsSource
to the SelectedItem.States
of the Countries ComboBox
(using ElementName binding), then you don't need to worry about SelectionChanged
events etc. The same will apply to State & City relationship.
As a non-US citizen I feel obliged to remind you that most other Countries do not have 'States' :)
This might not answer your direct question but I believe its a better solution to what you are trying to achieve
Let your Country data construct on the client side have a property of ObservableCollection<State>
, then bind the State ComboBox's ItemsSource
to the SelectedItem.States
of the Countries ComboBox (using ElementName
binding), then you don't need to worry about SelectionChanged
events etc. The same will apply to State & City relationship.
As a non-US citizen I feel obliged to remind you that most other Countries do not have 'States' :)
精彩评论