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
<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
<code>
Class MainWindow
Public Sub New()
InitializeComponent()
dataGrid1.ItemsSource = dtReplayWinLoss.DefaultView
End Sub
End Class
Application.xaml.vb
Class Application
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
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