WCF returning observable collection.

2 posts, 1 answers
  1. Henry
    Henry avatar
    44 posts
    Member since:
    Feb 2013

    Posted 19 Mar 2013 Link to this post

    Hi,

    I want to know how to convert a data table to an ObservableCollection and bind it to RadGrid. I am using a WCF service in my application and it will return the observable collection which is then bound to radgrid. It would be very helpful if someone can give a sample solution.

    Any help appreciated,
    Henry Gibbs.
  2. Answer
    Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 20 Mar 2013 Link to this post

    Hi,

    I think you want to bind an ObservableCollection returned by a WCF service to a RadGrid. Please have a look into the sample code provided below.

    C#:
    [ServiceContract]
    public interface IDatabaseService
    {
        [OperationContract]
        ObservableCollection<Employee> GetNames();
    }
     
    [DataContract]
    public class Employee
    {
        [DataMember]
        public int empno
        {
            get;
            set;
        }
     
        [DataMember]
        public string empname
        {
            get;
            set;
        }
    }

    public ObservableCollection<Employee> GetNames()
    {
        ObservableCollection<Employee> employees = new ObservableCollection<Employee>();
        connectString = System.Configuration.ConfigurationManager.ConnectionStrings["myConnectionString"].ToString();
        connectionObject = new SqlConnection(connectString);
        try
        {
            if (connectionObject.State != ConnectionState.Open)
            {
                connectionObject.Open();
            }
            string selQuery = "select empname,empno from Employee";
            SqlCommand comm = new SqlCommand(selQuery, connectionObject);
            SqlDataReader reader = comm.ExecuteReader();
            while (reader.Read())
            {
                Employee em = new Employee();
                em.empno = int.Parse(reader["empno"].ToString());
                em.empname = Convert.ToString(reader["empname"]);
                employees.Add(em);
            }
        }
        catch (Exception ex)
        {
        }
        finally
        {
            if (connectionObject.State != ConnectionState.Closed)
            {
                connectionObject.Close();
            }
        }
        return employees;
    }

    Then you can bind the Collection to your RadGridView as follows.

    C#:
    DatabaseServiceClient client = new DatabaseServiceClient();
     
    private void Button1_Click(object sender, RoutedEventArgs e)
    {
        client.GetNamesCompleted += new EventHandler<GetNamesCompletedEventArgs>(client_GetNamesCompleted);
        client.GetNamesAsync();
    }
     
    void client_GetNamesCompleted(object sender, GetNamesCompletedEventArgs e)
    {
        RadGrid1.ItemsSource = e.Result;          
    }

    Thanks,
    Shinu.
  3. DevCraft banner
Back to Top