问题描述
刚开始使用VS 2012 Express与WPF和VB合作。我们只是运行一些测试来查看工作原理并使用数据网格显示数据表中的数据有几个问题。
概述:在MainWindow.xaml我们定义了一个包含数据网格的网格,并将其命名为dataGrid1,有六列,并在Designer中按预期显示。我们在Application.xaml中使用Startup而不是Uri选项。此时,如果我们执行,窗口将数据网格显示为单行,六列,全部为空 - 正如预期的那样。
接下来我们添加了一个新模块并定义了dtReplayWinLoss作为Public New DataTable。然后,我们创建了一个公共函数来填充数据表。使用调试器检查数据表显示数据是正确的并且符合预期。 populate函数从Application.xaml.vb中的Application_Startup Sub调用,并在我们执行mainWindow.Show()之前调用。
下一步是绑定datagrid和数据表。我们在xaml中尝试了它,但它似乎没有用,我怀疑有某种类型的引用问题。
我们在调用populate函数后尝试进行绑定,但dataGrid1是无法访问。所以我们将绑定代码放在MainWindow.xaml.vb的MainWindow构造函数中,它似乎可以工作。这是代码:
dataGrid1.ItemsSource = dtReplayWinLoss.DefaultView
运行程序时datagrid现在显示正确的行数和列数,而不仅仅是单行。唯一的问题是任何单元格中都没有显示数据。任何人都可以帮我吗?
顺便说一句,这是代码片段,以防你需要它们:
MainWindow.xaml
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
MainWindow.xaml.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
Application.xaml.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
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
如有任何建议将不胜感激,感谢您的时间。
问候,
Larry
Any suggestions would be appreciated and thanks for your time.
Regards,
Larry
推荐答案
这篇关于Datagrid不显示数据表值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!