Hi,
How to bind data with ComboBox in silverlight.
Thanks, in advance
Loading
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Priya LingePosted Aug 30, 2011, 12:29 AM
MainPage.Xaml : We have Combobox
Service.svc.cs : Select CustomerName from Customer Table as below,
[ServiceContract(Namespace = "")]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class Service1
{
SqlConnection con = new SqlConnection("Data Source= TFServer\\;Initial Catalog=Pub4;Persist Security Info=True;User ID=trustfort;Password=Admin@123");
SqlDataReader dr;
List
[OperationContract]
public List
{
con.Open();
string qry = "select Name from Customer";
SqlCommand cmd = new SqlCommand(qry, con);
dr = cmd.ExecuteReader();
while (dr.Read())
{
list.Add(dr[0].ToString());
}
con.Close();
return list.ToList();
}
}
Here we return list of customer names ,those names we are going to bind the Combobox as below,
public partial class MainPage : UserControl
{
List
public MainPage()
{
InitializeComponent();
getdata();
}
public void getdata()
{
ServiceReference1.Service1Client service = new ServiceReference1.Service1Client();
service.dataCompleted += new EventHandler
service.dataAsync();
}
void service_dataCompleted(object sender, ServiceReference1.dataCompletedEventArgs e)
{
items = e.Result.ToList();
for (int i = 0; i < items.Count; i++)
{
comboBox1.Items.Add(items[i].ToString()); // Here we add names in combobox.
}
}
}
}
Please see attached file.
Hope this will help you.
Thanks.