Click here to Skip to main content
15,884,085 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
Hi,

Just started working with WPF and VB using VS 2012 express. We're just running some tests to see how things work and have a couple of issues using a datagrid to display the data from a data table.

Overview: In the MainWindow.xaml we defined a grid which contains a datagrid and named it dataGrid1 with six columns and it shows up as expected in the Designer. Rather than the Uri option, we used Startup in Application.xaml. At this point if we execute, the window shows the datagrid as a single row, six columns, all blank - as expected.

Next we added a new module and defined dtReplayWinLoss as Public New DataTable. We then created a public function to populate the datatable. Inspecting the data table with the debugger shows the data is correct and as expected. The populate function is called from our Application_Startup Sub in Application.xaml.vb and is called just before we do the mainWindow.Show().

The next step was to bind the datagrid and data table. We tried it in the xaml, but it didn't seem to work and I suspect there was some type of reference issue.
We tried doing the binding after we called the populate function, but the dataGrid1 was not accessible. So we placed the binding code in the MainWindow constructor in MainWindow.xaml.vb and it appears to work. Here's the code for that:

dataGrid1.ItemsSource = dtReplayWinLoss.DefaultView

When run the program the datagrid appears with the correct number of rows and columns now instead of just the single row. The only problem is there is no data displayed in any of the cells. Can anyone help me out here?

BTW, here's the code snippets, just in case you need them:

MainWindow.xaml
VB
<code><Window x:Class="MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:extToolkit="http://schemas.microsoft.com/winfx/2006/xaml/presentation/toolkit/extended"
    xmlns:xctk="http://schemas.xceed.com/wpf/xaml/toolkit"
    xmlns:xcdg="http://schemas.xceed.com/wpf/xaml/datagrid"
    Title="League Dashboard" Height="600" Width="900" UseLayoutRounding="False">

    <Grid>
        <DataGrid Name="dataGrid1"
		          Grid.Row="0"
		          Grid.Column="0"
		          Margin="10"
		          HorizontalAlignment="Left"
		          VerticalAlignment="Top"
		          AutoGenerateColumns="False"
		          MinRowHeight="26" removed="Red"  Foreground="White">
            <DataGrid.Columns>
                <DataGridTextColumn Width="120"  />     
                <DataGridTextColumn Width="80" />
                <DataGridTextColumn Width="80" />
                <DataGridTextColumn Width="80" />
                <DataGridTextColumn Width="80" />
                <DataGridTextColumn Width="100" />
            </DataGrid.Columns>
        </DataGrid>
    </Grid>
</Window></code>


MainWindow.xaml.vb
VB
<code>
Class MainWindow 

    Public Sub New()

        ' This call is required by the designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.

        dataGrid1.ItemsSource = dtReplayWinLoss.DefaultView

    End Sub
End Class


Application.xaml.vb
VB
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)

        If (asAppstatus.strStatusLeagueStandingsWindow = APP_STATUS_SHUT_DOWN) Then
            Application.Current.Shutdown()
            Exit Sub
        End If

        Dim mainWindow As New MainWindow()
        intPopulateReplayDataTable(dtReplayWinLoss)
        mainWindow.Show()

    End Sub
End Class


Module1.vb
VB
Module Module1
    Public asAppstatus As New AppStatus
    Public LDIni As New LDIni
    Public dtReplayWinLoss As New DataTable

    Public Function intPopulateReplayDataTable(ByRef dt As DataTable) As Integer

         Dim dc As DataColumn

        With dt
            .TableName = "ReplayWinLoss"
            dc = New DataColumn("TeamName", System.Type.GetType("System.String"))
            dc.Caption = "Senior Cicuit"
            dc.DefaultValue = "XXX"
            .Columns.Add(dc)
            dc = New DataColumn("Games", System.Type.GetType("System.Int32"))
            .Columns.Add(dc)
            dc = New DataColumn("Wins", System.Type.GetType("System.Int32"))
            .Columns.Add(dc)
            dc = New DataColumn("Losses", System.Type.GetType("System.Int32"))
            .Columns.Add(dc)
            dc = New DataColumn("Ties", System.Type.GetType("System.Int32"))
            .Columns.Add(dc)
            dc = New DataColumn("PCT", System.Type.GetType("System.Single"))
            .Columns.Add(dc)
            .Rows.Add("Sommerville)", 161, 84, 77, 0.522)
            .Rows.Add("North 40", 162, 82, 80, 0, 0.506)
            .Rows.Add("Easterling", 162, 85, 77, 0, 0.525)
            .Rows.Add("Southside", 162, 85, 77, 0, 0.525)
        End With
    End Function
End Module


Any suggestions would be appreciated and thanks for your time.

Regards,
Larry
Posted
Comments
Anh Nguyen Trong 22-Aug-13 5:51am    
I got the same problem. Let try DataGridView.

Thanks Anh for your input. I've solved the problem this morning and the solution was very simple and sorta makes me feel like an idiot. I had originally tried to do the binding in the xaml, but for whatever reason it did not work. So I removed all the binding from the xaml including those in the datagridtextcolumns and then added the

dataGrid1.ItemsSource = dtReplayWinLoss.DefaultView

code to MainWindow.xaml.vb. The Grid grid then showed up with correct number of columns and rows, but no data. Putting the Binding back into the datagridtextcolumn did the trick. Why I didn't see that right away is beyond me.

Regards,
Larry
 
Share this answer
 
I got the same problem and find out the solution:

http://www.c-sharpcorner.com/uploadfile/raj1979/show-data-in-wpf-datagrid-using-dataset-data-template/[^]

Please see and hope your problem is solved.

Trong Anh
 
Share this answer
 

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