I have just start learning silverlight and I will now share my knowledge of my silverlight with you peoples. For my first post regarding silverlight I have choose datagrid control which is one of my favorite control if I look back to the asp.net. In this post I will show you how you can bind datagrid control with the data source and also about the formatting of the datagrid control. Below is the output of the example code, here you can see a datagrid which is used to display information about the Person. Like User id, First Name, Last Name, Email address, Contact number and date of birth.
Let us start our example, Below is the namespace which you need to add in yours xaml file , if you want to add the datagrid manual , if you drag and drop the data grid control from the tool bar then this namespace will be added automatically.
And blow is the xaml of the adding the datagrid and setting some of the properties of the datagrid control. Here you see that I have set the name of the datagrid and GridLineVisibility and AutoGenerateColumns properties of the datagrid. As I don't want datagrid to generate columns so I set this property to false, as I will add my columns in the datagrid column collections.
Below table list some of the basing properties of the data grid control. I have list down the properties with there description.
Defining Columns :
If you set autogeneratColumn property to true the column are generated automatically you can’t control how columns are ordered, how wide they are, how the values inside are formatted, and what header text is placed at the top. A far more powerful approach is to turn off automatic column generation by setting AutoGenerateColumns to false. You can then explicitly define the columns you want, with the settings you want, and in the order you want. To do this, you need to fill the DataGrid.Columns collection with the right column objects. Below table show the three columns supported by datagrid control.
In my example code I have used the DataGridTextColumn and DataGridTemplateColumn but not the DataGridCheckBoxColumn. In below table I have mention some of the properties of the columns of the datagrid control (Source).
In above table I have not mention all the properties , but some of them which I think are most commonly used. I have used SortMemberPath in the DataGridTemplateColulmn, so that user can sort the DataGridTemplateColumn as well, if you don't give the SortMemberPath then the DataGridTemplateColumn will not be sorted if you click on that column for sorting. And here is the code behind code which is used to bind the datagrid to the datasource.
Hope you have some idea about the datagrid control. In future post I will discover more about the datagrid control and share with you peoples.You can download the source code from here
All and any comments / bugs / suggestions are welcomed!