Silverlight / WPF - Bind a silverlight datagrid to a web service dynamically

Asked By Nick S on 18-Jun-10 03:14 AM

Hi,

I have successfully managed to bind a silverlight datagrid to a web service which works fine.

However, I have decided to build the datagrid programatically in order for the user to add multiple datagrids by clicking a button.

I've attached my code below, you can see an additional datagrid is being populated by Private Sub mService_GetContactsCompleted

However, I won't know what the datagrid name is so this needs to be made dynamic.

I guess the ultimate answer would be using dg.itemsource = <Web Service reference here>

rather than using the GetContactsCompleted.

Does anyone have any suggestions?

Thanks!

Nick


Imports System.Windows.Markup
Imports System.Windows.Data

Partial Public Class MainPage
    Inherits UserControl
    Dim count As Integer
    Private WithEvents mService As New ServiceReference1.Service1Client()

    Public Sub New()
        InitializeComponent()
    End Sub

    Private Sub btnLoad_Click( _
    ByVal sender As System.Object, _
     ByVal e As System.Windows.RoutedEventArgs)

        '      mService.GetIncidentsAsync(sender)

    End Sub

    Private Sub mService_GetContactsCompleted( _
            ByVal sender As Object, _
            ByVal e As ServiceReference1 _
                    .GetIncidentsCompletedEventArgs) _
            Handles mService.GetIncidentsCompleted
        dataGrid1.ItemsSource = e.Result

        dataGrid1.Visibility = Windows.Visibility.Visible

    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles Button1.Click
        Dim Item As New TabItem()
        Dim datatemplate = "<DataTemplate xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation""><Image Source=""glossy.jpg"" Width=""100"" Height=""30""/></DataTemplate>"
        Dim template As DataTemplate = DirectCast(XamlReader.Load(datatemplate), DataTemplate)
        Item.HeaderTemplate = template
        Dim grid = New Grid()
        Item.Content = grid
        Dim dg As New DataGrid() With { _
        .Name = "dg",
        .ColumnHeaderHeight = "20",
        .RowHeight = "20",
        .Visibility = Windows.Visibility.Visible,
        .Margin = New Thickness(3, 2, -195, -154),
        .AutoGenerateColumns = False}

        'dg.ItemsSource =
        Dim dgcolum1 As New DataGridTextColumn()

        dgcolum1.Header = "Number #"
        dgcolum1.Binding = New Binding("Number")
        dg.Columns.Add(dgcolum1)

        Dim dgcolum2 As New DataGridTextColumn()

        dgcolum2.Header = "Description"
        dgcolum2.Binding = New Binding("Description")
        dg.Columns.Add(dgcolum2)

        ' dataGrid1.Visibility = Windows.Visibility.Visible


        ' Dim wsdataset As ServiceReference1.IncidentList

        'dataGrid1.ItemsSource = wsdataset

        ' Dim tb As New TextBlock() With { _
        ' .Text = "Text Item 1"}

        grid.Children.Add(dg)

        Dim item2 As New TabItem()
        item2.Header = "Header"
        item2.Content = New TextBox() With { _
             .Text = "Text Item 2"
         }

        TabControl1.Items.Add(Item)

        'TabControl1.Items.Add(item2)
    End Sub


End Class

samjayander thiagarajan replied to Nick S on 18-Jun-10 03:45 AM
Hi,

To populate multiple datagrid, you need to fetch each set of data every time you click your button.

So, in Click event of button, initiate the webservice call.

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles Button1.Click
  mService.GetIncidentsAsync(sender)
End Sub


and when your webservice call returns the result, create a new datagrid, and then bind the result to the datagrid. Your code almost looks correct. except a few changes..
Private Sub mService_GetContactsCompleted( _
      ByVal sender As Object, _
      ByVal e As ServiceReference1 _
          .GetIncidentsCompletedEventArgs) _
      Handles mService.GetIncidentsCompleted
    Dim Item As New TabItem()
    Dim datatemplate = "<DataTemplate xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation""><Image Source=""glossy.jpg"" Width=""100"" Height=""30""/></DataTemplate>"
    Dim template As DataTemplate = DirectCast(XamlReader.Load(datatemplate), DataTemplate)
    Item.HeaderTemplate = template
    Dim grid = New Grid()
    Item.Content = grid
    Dim dg As New DataGrid() With { _
    .Name = "dg",
    .ColumnHeaderHeight = "20",
    .RowHeight = "20",
    .Visibility = Windows.Visibility.Visible,
    .Margin = New Thickness(3, 2, -195, -154),
    .AutoGenerateColumns = False}
 
    'dg.ItemsSource =
    Dim dgcolum1 As New DataGridTextColumn()
 
    dgcolum1.Header = "Number #"
    dgcolum1.Binding = New Binding("Number")
    dg.Columns.Add(dgcolum1)
 
    Dim dgcolum2 As New DataGridTextColumn()
 
    dgcolum2.Header = "Description"
    dgcolum2.Binding = New Binding("Description")
    dg.Columns.Add(dgcolum2)
 
     'Bind your result here.
     dg..ItemsSource = e.Result
 
    grid.Children.Add(dg)
 
    Dim item2 As New TabItem()
    item2.Header = "Header"
    item2.Content = New TextBox() With { _
       .Text = "Text Item 2"
     }
 
    TabControl1.Items.Add(Item)
End Sub

Regards,
Sam
Nick S replied to samjayander thiagarajan on 18-Jun-10 07:08 AM

Hi Sam!


Thats great, this works perfectly!


Thank you very much for your precise and excellent response!!


Very much appriciated, thanks again!!


Nick