Datagrid不显示数据表值 [英] Datagrid not showing datatable values

查看:69
本文介绍了Datagrid不显示数据表值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





刚开始使用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

推荐答案

感谢Anh的输入。今天早上我解决了这个问题,解决方案非常简单,有点让我觉得自己像个白痴。我原本试图在xaml中进行绑定,但无论出于什么原因它都无效。所以我删除了xaml中的所有绑定,包括datagridtextcolumns中的绑定,然后添加了



dataGrid1.ItemsSource = dtReplayWinLoss.DefaultView



代码到MainWindow.xaml.vb。然后,网格网格显示正确的列数和行数,但没有数据。将Binding放回datagridtextcolumn就可以了。为什么我没有立刻看到它超出我。



问候,

拉里
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


我遇到了同样的问题并找到了解决方案:



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



请查看并希望您的问题得到解决。



Trong Anh
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


这篇关于Datagrid不显示数据表值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆