Click here to Skip to main content
15,885,366 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi guys,

so I am 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
Imports System.Data
Imports System.ComponentModel

Public Class DataModel
    Implements INotifyPropertyChanged

    'Public Sub New(ByVal _DT As DataView)
    '    Me.TestDataView = _DT
    'End Sub

    Private _testDataView As New DataView

    Public Property TestDataView() As DataView
        Get
            Return _testDataView
        End Get
        Set(value As DataView)
            _testDataView = value
            InvokePropertyChanged("TestDataView")
        End Set
    End Property

    Public Sub InvokePropertyChanged(Properties As String)
        RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(Properties))
    End Sub

    Public Event PropertyChanged(sender As Object, e As PropertyChangedEventArgs) Implements INotifyPropertyChanged.PropertyChanged
End Class


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

VB
Imports System.Data

Class Application

    ' Application-level events, such as Startup, Exit, and DispatcherUnhandledException
    ' can be handled in this file.

    Private Sub Application_Startup(sender As Object, e As StartupEventArgs) Handles Me.Startup


        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
    End Sub
End Class


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)

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

    </Grid>
</Window>


The problem is, 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
Posted
Updated 2-Nov-13 18:55pm
v3

1 solution

Add in Mode=TwoWay to set it from the code

<datagrid itemssource="{Binding TestDataView, Mode=TwoWay" horizontalalignment="Left" height="273" margin="10,25,0,0" verticalalignment="Top" width="480" grid.columnspan="2" />
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900