views:

71

answers:

1

Hi,

I have a user control...and the base page(s) which uses this user control has a dataset which will be used by the user control.

My dataset is dynamic...means..it can have different number of columns depending upon which page my usercontrol is implemented. Is there any control in wpf which i can use to bind this dataset (without knowing the column information) ...i mean similar to datagrid/gridview in 2.0 ?

+3  A: 

Take a look at the WPF toolkit, this contains a Grid which meets your requirements.

This toolkit is built by Microsoft, and part of the Microsoft Shared Source Initiative (see the link I provided).

It's not supported my Microsoft though, so if you get a bug you can use their forums but not call MS support.

If you do want to do it yourself, you can for example write some code that takes a List<T>, you get the generic type, get the properties, iterate over them, write the column headers, iterate over all the items in the list and write all the properties.

I wrote this code a while back to write a List to an HTML table, I hope it's useful:

public void PrintList<T>(List<T> list)
{
    if(null!=list.FirstOrDefault())
    {
       Type t = typeof(list[0]);
       PropertyInfo[] properties = t.GetProperties();

       // properties = list of all properties.

       print("<table><tr>");

       foreach(var property in properties)
       {
          // print property title
          print(string.Format("<th>{0}</th>", property.Name));
       }   


       print("</tr>");

       foreach(var item in list)
       {
          print("<tr>");
          foreach(var property in properties)
          {
               var propertyValue = t.InvokeMember(property.Name, BindingFlags.GetProperty, null, item, new object[] {});
               print(string.Format("<td>{0}</td>", propertyValue));
          }

          print("</tr>");
       }

       print("</table>");
    }
}
Snake
Is WPF Toolkit from Microsoft ?..Because my client is verymuch strict abt "not using-third party controls" :(
Anish