i




 
Thread Rating:
  • 1 Votes - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Populating DataGrid in Silverlight 4.0 using C#
11-26-2012, 10:41 AM
Post: #1
Populating DataGrid in Silverlight 4.0 using C#
DataGrid in Silverlight is just like we have GridView in ASP.NET or DataGrid in WinForms. See how to work with it.

Drag and drop a DataGrid from Tools to your SL work area. And assign it some Name.

The good thing about XAML is that everything is an object instead of specific datatype element.

So, if you have an array of strings or integers, you can assign them to DataGrid source.

If you have any List<Class>, and all the members of that class have a ToString() method implemented, then you can assign that List<Class> to its source as well.

If any member doesn't implement ToString(), its type is displayed in the DataGrid cell. Lets see the following example.

Code:
string[] Words = "H e l l o W o r l d !".Split();
dataGrid1.ItemsSource = Words;

The outcome will be:

[Image: StringSplitDataGrid_0695d3ca-d884-4c84-a...f32d2f.png]

So, similarly, in case of some List<Class>, all the members will become Columns and List items will become rows. For example:

Code:
public class Human
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public int Age { get; set; }
    public bool Available { get; set; }
}

public Page()
{
    InitializeComponent();

    List<Human> list = new List<Human>();
    int itemsCount = 100;

    for (int i = 0; i < itemsCount; i++)
    {
        list.Add(new Human()
        {
            FirstName = "First",
            LastName = "Last",
            Age = i,
            Available = (i % 2 == 0)
        });
    }

    dataGrid1.ItemsSource = list;
}

So, the output will be:

[Image: DataListDataGrid_45f8a7e8-c576-4771-8d77...a74340.png]

Hopes this helps alot!





View Asad Ali's profile on LinkedIn

Quote
11-26-2012, 10:47 AM
Post: #2
RE: Populating DataGrid in Silverlight 4.0 using C#
Thank you magnum_2007 for posting the thread with good detail.

[Image: 2604595733.png]
Quote
11-26-2012, 12:55 PM
Post: #3
RE: Populating DataGrid in Silverlight 4.0 using C#
You are welcome Nisar...



View Asad Ali's profile on LinkedIn

Quote




User(s) browsing this thread: 1 Guest(s)