none
creating and editing a wpf datagrid

    Question

  • Hi,

          I am just migrating from Windows Forms to WPF. I need immediate help on creating an unbound datagrid with 2 columns ('Type' and 'Telephone No') and adding records to it. Can someone please give me a comprehensive code in vb.net / xaml for this

    Thanks & regards

    Sanjish

    Sunday, January 26, 2014 5:15 PM

Answers

  • Hi,

    Here is my sample, it just set the Window's DataContext to a DataTable:

    Class MainWindow
        Public Sub New()
    
            ' This call is required by the designer.
            InitializeComponent()
    
            Dim dt As New DataTable()
            dt.Columns.Add("Type", GetType(String))
            dt.Columns.Add("Telephone No", GetType(String))
            dt.Rows.Add(New Object(1) {"Type1", "732-757-2923"})
            dt.Rows.Add(New Object(1) {"Type2", "732-757-2923"})
            dt.Rows.Add(New Object(1) {"Type3", "732-757-2923"})
            dt.Rows.Add(New Object(1) {"Type2", "732-757-2923"})
            dt.Rows.Add(New Object(1) {"Type3", "732-757-2923"})
            dt.Rows.Add(New Object(1) {"Type1", "732-757-2923"})
            DataContext = dt.AsDataView()
        End Sub
    End Class

    XAML:

    <DataGrid ItemsSource="{Binding}" />

    Screenshot:


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Marked as answer by SANJISH Saturday, February 01, 2014 11:57 AM
    Monday, January 27, 2014 7:42 AM
  • Okay, we can just set different DataContext for the different DataGrid:

    Public Sub New()
    
            ' This call is required by the designer.
            InitializeComponent()
    
            ' Add any initialization after the InitializeComponent() call.
            Dim dt As New DataTable()
            dt.Columns.Add("Type", GetType(String))
            dt.Columns.Add("Telephone No", GetType(String))
            dt.Rows.Add(New Object(1) {"Type1", "732-757-2923"})
            dt.Rows.Add(New Object(1) {"Type2", "732-757-2923"})
            dt.Rows.Add(New Object(1) {"Type3", "732-757-2923"})
            dt.Rows.Add(New Object(1) {"Type2", "732-757-2923"})
            dt.Rows.Add(New Object(1) {"Type3", "732-757-2923"})
            dt.Rows.Add(New Object(1) {"Type1", "732-757-2923"})
    
            dg1.DataContext = dt.AsDataView()
    
            Dim dt2 As New DataTable()
            dt2.Columns.Add("Type", GetType(String))
            dt2.Columns.Add("Telephone No", GetType(String))
            dt2.Rows.Add(New Object(1) {"Type3", "732-757-2923"})
            dt2.Rows.Add(New Object(1) {"Type4", "732-757-2923"})
            dt2.Rows.Add(New Object(1) {"Type5", "732-757-2923"})
            dg2.DataContext = dt2.AsDataView()
    
        End Sub

    XAML:

    <DataGrid x:Name="dg1" Grid.Column="0" ItemsSource="{Binding}" />
    <DataGrid x:Name="dg2" Grid.Column="1" ItemsSource="{Binding}" />

    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Marked as answer by SANJISH Monday, February 03, 2014 1:56 PM
    Monday, February 03, 2014 5:09 AM

All replies

  • Hi,

    Here is my sample, it just set the Window's DataContext to a DataTable:

    Class MainWindow
        Public Sub New()
    
            ' This call is required by the designer.
            InitializeComponent()
    
            Dim dt As New DataTable()
            dt.Columns.Add("Type", GetType(String))
            dt.Columns.Add("Telephone No", GetType(String))
            dt.Rows.Add(New Object(1) {"Type1", "732-757-2923"})
            dt.Rows.Add(New Object(1) {"Type2", "732-757-2923"})
            dt.Rows.Add(New Object(1) {"Type3", "732-757-2923"})
            dt.Rows.Add(New Object(1) {"Type2", "732-757-2923"})
            dt.Rows.Add(New Object(1) {"Type3", "732-757-2923"})
            dt.Rows.Add(New Object(1) {"Type1", "732-757-2923"})
            DataContext = dt.AsDataView()
        End Sub
    End Class

    XAML:

    <DataGrid ItemsSource="{Binding}" />

    Screenshot:


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Marked as answer by SANJISH Saturday, February 01, 2014 11:57 AM
    Monday, January 27, 2014 7:42 AM
  • Hi,

        Thanks for the solution . Since I have 2 Datagrids on my form how do I bind both of them to different datatables and write the appropriate xaml for both as <DataGrid ItemsSource="{Binding}" />  does not tell me which datatable to bind to .

    Please help

    Regards

    Sanjish

    Saturday, February 01, 2014 12:02 PM
  • Okay, we can just set different DataContext for the different DataGrid:

    Public Sub New()
    
            ' This call is required by the designer.
            InitializeComponent()
    
            ' Add any initialization after the InitializeComponent() call.
            Dim dt As New DataTable()
            dt.Columns.Add("Type", GetType(String))
            dt.Columns.Add("Telephone No", GetType(String))
            dt.Rows.Add(New Object(1) {"Type1", "732-757-2923"})
            dt.Rows.Add(New Object(1) {"Type2", "732-757-2923"})
            dt.Rows.Add(New Object(1) {"Type3", "732-757-2923"})
            dt.Rows.Add(New Object(1) {"Type2", "732-757-2923"})
            dt.Rows.Add(New Object(1) {"Type3", "732-757-2923"})
            dt.Rows.Add(New Object(1) {"Type1", "732-757-2923"})
    
            dg1.DataContext = dt.AsDataView()
    
            Dim dt2 As New DataTable()
            dt2.Columns.Add("Type", GetType(String))
            dt2.Columns.Add("Telephone No", GetType(String))
            dt2.Rows.Add(New Object(1) {"Type3", "732-757-2923"})
            dt2.Rows.Add(New Object(1) {"Type4", "732-757-2923"})
            dt2.Rows.Add(New Object(1) {"Type5", "732-757-2923"})
            dg2.DataContext = dt2.AsDataView()
    
        End Sub

    XAML:

    <DataGrid x:Name="dg1" Grid.Column="0" ItemsSource="{Binding}" />
    <DataGrid x:Name="dg2" Grid.Column="1" ItemsSource="{Binding}" />

    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Marked as answer by SANJISH Monday, February 03, 2014 1:56 PM
    Monday, February 03, 2014 5:09 AM
  • Hi,

          Thanks a lot

    Regards

    Sanjish

    Monday, February 03, 2014 1:56 PM