Friday 11 June 2010

Data Bind in Silverlight DataGrid


To show data in datagrid view, we need to assign a list to the itemsource of datagrid.
dgSalesOfficeLiveAgent.ItemsSource = e.Result;
e.Result – is the list of data.
Above is enough for .cs file. But in .xaml we should configure the datagrid. There are lots of property of datagrid. According to our requirement it is configurable.  Most important things are column configuration. Silverlight datagrid is very powerful. A column can be editable. It may contain multiple controls like TextBlock, Image, TextBox etc. We can combine various controls simultaneous. Basic structure of a datagrid –

There are three different type of column declare above.
1. First one simply text binding. It is highly used to bind normal text. Example - How to use it.
Header – is the name to be appears on column head.
FirstName – is the property name of list which is assigned to the datagrid itemsourch.
2. Second one is used when we want to modify our text like bold or coloring etc we have to use control like <TextBlock/>. We may use <Image/> tag to store image. Example –
In <Image/> tag there are various property has set. We can add event like OnMouseleftbuttonUp to fire some event on mouse click.
3. Third Option is used when we want to add multiple controls within a column.
To manipulate datagrid from .cs file we can use LoddingRow event of datagrid. To search control of datagrid we have to search that control by it name –
FrameworkElement eleBargainingRoomButton = dgModelHome.Columns[3].GetCellContent(e.Row);
                Button btnBargainingRoomButton = eleBargainingRoomButton.FindName("btnBargainingRoom") as Button;
btnBargainingRoom – Is the Button control name inside datagrid.
Columns[3] – Button is in fourth column.