Question MVVM WPF Datatable to Datagrid binding issue

TurboTilly

New member
Joined
Nov 3, 2013
Messages
1
Programming Experience
1-3
Hi guys,

I am new to this forum and hope that all of you can bear with me since I am relatively new to VB.NET and am still trying to wrap my head around MVVM. I think it is a great way of designing an application; however, all I am able to track down on the web are C$ examples and only very little "Over my head" info given in vb.net. Unfortunately I don't have enough time to learn C$ so applying mvvm to vb.net proves difficult at best.

Here is my diellema and please bear with me here if this isn't the correct way of doing things, I am just trying to learn. I have a datatable that I eventually want to show on multiple gridviews in multiple windows.

I created a class called "DataModel"

VB.NET:
[COLOR=#666666][FONT=Segoe UI]Imports System.Data[/FONT][/COLOR]
[COLOR=#666666][FONT=Segoe UI]Imports System.ComponentModel[/FONT][/COLOR]

[COLOR=#666666][FONT=Segoe UI]Public Class DataModel[/FONT][/COLOR]
[COLOR=#666666][FONT=Segoe UI]    Implements INotifyPropertyChanged[/FONT][/COLOR]

[COLOR=#666666][FONT=Segoe UI]    'Public Sub New(ByVal _DT As DataView)[/FONT][/COLOR]
[COLOR=#666666][FONT=Segoe UI]    '    Me.TestDataView = _DT[/FONT][/COLOR]
[COLOR=#666666][FONT=Segoe UI]    'End Sub[/FONT][/COLOR]

[COLOR=#666666][FONT=Segoe UI]    Private _testDataView As New DataView[/FONT][/COLOR]

[COLOR=#666666][FONT=Segoe UI]    Public Property TestDataView() As DataView[/FONT][/COLOR]
[COLOR=#666666][FONT=Segoe UI]        Get[/FONT][/COLOR]
[COLOR=#666666][FONT=Segoe UI]            Return _testDataView[/FONT][/COLOR]
[COLOR=#666666][FONT=Segoe UI]        End Get[/FONT][/COLOR]
[COLOR=#666666][FONT=Segoe UI]        Set(value As DataView)[/FONT][/COLOR]
[COLOR=#666666][FONT=Segoe UI]            _testDataView = value[/FONT][/COLOR]
[COLOR=#666666][FONT=Segoe UI]            InvokePropertyChanged("TestDataView")[/FONT][/COLOR]
[COLOR=#666666][FONT=Segoe UI]        End Set[/FONT][/COLOR]
[COLOR=#666666][FONT=Segoe UI]    End Property[/FONT][/COLOR]

[COLOR=#666666][FONT=Segoe UI]    Public Sub InvokePropertyChanged(Properties As String)[/FONT][/COLOR]
[COLOR=#666666][FONT=Segoe UI]        RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(Properties))[/FONT][/COLOR]
[COLOR=#666666][FONT=Segoe UI]    End Sub[/FONT][/COLOR]

[COLOR=#666666][FONT=Segoe UI]    Public Event PropertyChanged(sender As Object, e As PropertyChangedEventArgs) Implements INotifyPropertyChanged.PropertyChanged[/FONT][/COLOR]
[COLOR=#666666][FONT=Segoe UI]End Class[/FONT][/COLOR]

and I am currently adding data at the Application Startup in Application.XAML.vb

VB.NET:
[COLOR=#666666][FONT=Segoe UI]Imports System.Data[/FONT][/COLOR]

[COLOR=#666666][FONT=Segoe UI]Class Application[/FONT][/COLOR]

[COLOR=#666666][FONT=Segoe UI]    ' Application-level events, such as Startup, Exit, and DispatcherUnhandledException[/FONT][/COLOR]
[COLOR=#666666][FONT=Segoe UI]    ' can be handled in this file.[/FONT][/COLOR]

[COLOR=#666666][FONT=Segoe UI]    Private Sub Application_Startup(sender As Object, e As StartupEventArgs) Handles Me.Startup[/FONT][/COLOR]


[COLOR=#666666][FONT=Segoe UI]        Dim dataTableView As DataModel[/FONT][/COLOR]

[COLOR=#666666][FONT=Segoe UI]        Dim dataTable As DataTable[/FONT][/COLOR]

[COLOR=#666666][FONT=Segoe UI]        dataTable.Columns.Add("DeptID", GetType(System.Int32))[/FONT][/COLOR]
[COLOR=#666666][FONT=Segoe UI]        dataTable.Columns.Add("DepartmentName", GetType(System.String))[/FONT][/COLOR]
[COLOR=#666666][FONT=Segoe UI]        dataTable.Columns.Add("HOD", GetType(System.String))[/FONT][/COLOR]
[COLOR=#666666][FONT=Segoe UI]        dataTable.Columns.Add("FacultyCount", GetType(System.String))[/FONT][/COLOR]

[COLOR=#666666][FONT=Segoe UI]        Dim row As DataRow = dataTable.NewRow()[/FONT][/COLOR]
[COLOR=#666666][FONT=Segoe UI]        row("DeptID") = 1[/FONT][/COLOR]
[COLOR=#666666][FONT=Segoe UI]        row("DepartmentName") = "CS&E"[/FONT][/COLOR]
[COLOR=#666666][FONT=Segoe UI]        row("HOD") = "John"[/FONT][/COLOR]
[COLOR=#666666][FONT=Segoe UI]        row("FacultyCount") = 20[/FONT][/COLOR]
[COLOR=#666666][FONT=Segoe UI]        dataTable.Rows.Add(row)[/FONT][/COLOR]

[COLOR=#666666][FONT=Segoe UI]        row = dataTable.NewRow()[/FONT][/COLOR]
[COLOR=#666666][FONT=Segoe UI]        row("DeptID") = 2[/FONT][/COLOR]
[COLOR=#666666][FONT=Segoe UI]        row("DepartmentName") = "Mech"[/FONT][/COLOR]
[COLOR=#666666][FONT=Segoe UI]        row("HOD") = "Bo Yo"[/FONT][/COLOR]
[COLOR=#666666][FONT=Segoe UI]        row("FacultyCount") = 23[/FONT][/COLOR]
[COLOR=#666666][FONT=Segoe UI]        dataTable.Rows.Add(row)[/FONT][/COLOR]

[COLOR=#666666][FONT=Segoe UI]        dataTableView.TestDataView = dataTable.DefaultView[/FONT][/COLOR]
[COLOR=#666666][FONT=Segoe UI]    End Sub[/FONT][/COLOR]
[COLOR=#666666][FONT=Segoe UI]End Class[/FONT][/COLOR]

Here is the MainWindow.xaml (I only created a datagrid to display the datatable and set the datacontext of the window to the class DataModel)


VB.NET:
[COLOR=#666666][FONT=Segoe UI]<Window x:Class="MainWindow"[/FONT][/COLOR]
[COLOR=#666666][FONT=Segoe UI]        DataContext="DataModel"[/FONT][/COLOR]
[COLOR=#666666][FONT=Segoe UI]    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"[/FONT][/COLOR]
[COLOR=#666666][FONT=Segoe UI]    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"[/FONT][/COLOR]
[COLOR=#666666][FONT=Segoe UI]    Title="MainWindow" Height="350" Width="525">[/FONT][/COLOR]
[COLOR=#666666][FONT=Segoe UI]    <Grid>[/FONT][/COLOR]
[COLOR=#666666][FONT=Segoe UI]        <Grid.ColumnDefinitions>[/FONT][/COLOR]
[COLOR=#666666][FONT=Segoe UI]            <ColumnDefinition Width="41*"/>[/FONT][/COLOR]
[COLOR=#666666][FONT=Segoe UI]            <ColumnDefinition Width="6*"/>[/FONT][/COLOR]
[COLOR=#666666][FONT=Segoe UI]        </Grid.ColumnDefinitions>[/FONT][/COLOR]
[COLOR=#666666][FONT=Segoe UI]        <DataGrid ItemsSource="{Binding TestDataView}" HorizontalAlignment="Left" Height="273" Margin="10,25,0,0" VerticalAlignment="Top" Width="480" Grid.ColumnSpan="2" />[/FONT][/COLOR]

[COLOR=#666666][FONT=Segoe UI]    </Grid>[/FONT][/COLOR]
[COLOR=#666666][FONT=Segoe UI]</Window>[/FONT][/COLOR]

There are no errors but I can't see any values in my datagrid. I really don't know what I am doing wrong here. Would any of you guys be willing to help me out? Thank you very much in advance
 
Firstly, you never actually set the DataContext of the window. In your XAML you specify "DataModel" as the DataContext, which isn't going to do anything. The DataContext has to be an object containing data to display. DataModel is a type, where you actually want to use an instance of the DataModel type. It's like you can't just have dog as a pet; you have to have A dog.

If you want to set the DataContext in XAML then you have to use an object that is accessible via XAML. I suggest that you read the documentation for the DataContext property and follow the appropriate links from there.

The alternative is to set the DataContext in code, which means assigning your 'dataTableView' variable to the DataContext property. You have more issues there though. Look at this section of your code:
        Dim dataTableView As DataModel

        Dim dataTable As DataTable

        dataTable.Columns.Add("DeptID", GetType(System.Int32))
        dataTable.Columns.Add("DepartmentName", GetType(System.String))
        dataTable.Columns.Add("HOD", GetType(System.String))
        dataTable.Columns.Add("FacultyCount", GetType(System.String))

        Dim row As DataRow = dataTable.NewRow()
        row("DeptID") = 1
        row("DepartmentName") = "CS&E"
        row("HOD") = "John"
        row("FacultyCount") = 20
        dataTable.Rows.Add(row)

        row = dataTable.NewRow()
        row("DeptID") = 2
        row("DepartmentName") = "Mech"
        row("HOD") = "Bo Yo"
        row("FacultyCount") = 23
        dataTable.Rows.Add(row)

        dataTableView.TestDataView = dataTable.DefaultView
Where is that code do you actually create a DataModel object or a DataTable object? You don't. Both your variables are Nothing, so you must be getting NullReferenceExceptions when you run it. Just like the real-world objects they are based on, programming objects don't just materialise out of thin air. If you want a DataModel object then you have to create a DataModel object.
 
Back
Top