网格中不可见的数据 [英] Data not visible in Grid

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

问题描述



我正在尝试将代码生成的数据表绑定到WPF中的数据网格.
数据很好,我可以按行中的数字对列进行排序,但是datagrid除了列标题和行中的第一个(AgentName)列数据外,不显示任何数据,行中的其他数据为空屏幕上,但正如我所说,我可以对应该显示的数据进行排序.

我在哪里做错了?

Hi,

I am trying to bind code generated data table to a datagrid in WPF.

The data comes out fine, and I can sort columns by the numbers in the rows, BUT the datagrid doesn''t show any data besides column headers and first (AgentName) column data in the rows, other data in the row is empty on the screen, but as I said, I can sort the data that should appear.

Where am I doing wrong here?

Private Sub gil_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles gil.Click
    If String.IsNullOrEmpty(ReportFromDate.Text) Or String.IsNullOrEmpty(ReportToDate.Text) Then Exit Sub

    Dim fromdate As Date = ReportFromDate.SelectedDate
    Dim todate As Date = ReportToDate.SelectedDate

    Dim months = DateDiff(DateInterval.Month, fromdate, todate)
    Dim years = DateDiff(DateInterval.Year, fromdate, todate)

    Dim a = New DataTable("MonthSliceTable")
    a.Columns.Add("AgentName")

    Dim fromyear = DatePart(DateInterval.Year, fromdate)
    Dim frommonth = DatePart(DateInterval.Month, fromdate)
    Dim toyear = DatePart(DateInterval.Year, todate)
    Dim tomonth = DatePart(DateInterval.Month, todate)

    If fromyear < toyear Then
        Dim yr = toyear - fromyear
        Dim totalmonths = months
        Dim mon = frommonth
        Dim y = DatePart(DateInterval.Year, fromdate)
        For m = 0 To months
            Dim mo As String = "1/" + mon.ToString + "/" + y.ToString
            Dim Name As DataColumn = New DataColumn(mo.ToString)
            Name.AllowDBNull = True
            Name.Caption = mo.ToString
            Name.ColumnName = mo.ToString
            Name.MaxLength = 50
            Name.Unique = False
            a.Columns.Add(Name.ToString)
            If mon = 12 Then
                mon = 0
                y = y + 1
            End If
            mon += 1
        Next

    ElseIf fromyear = toyear Then
        For m = frommonth To tomonth
            Dim y = DatePart(DateInterval.Year, fromdate)
            Dim mo As String = "1/" + m.ToString + "/" + y.ToString
            Dim Name As DataColumn = New DataColumn()
            Name.AllowDBNull = True
            Name.Caption = mo.ToString
            Name.ColumnName = mo.ToString
            Name.Unique = False
            a.Columns.Add(Name.ToString)
        Next

    End If

    Dim moni As String
    Dim moni1 As String
    Dim begmonth As Date
    Dim endmonth As Date
    Dim yprem As Double = 0.0
    Dim agg = 0

    Dim reportcolumns As Integer = a.Columns.Count

    Dim meetingreport = From c In db.AppMeetingViews Select c.AgentName, c.MeetingDate, c.MeetingId, c.MeetingPrem
                Where MeetingDate >= fromdate And MeetingDate <= todate
                Group By AgentName Into Cou = Count(MeetingId), prem = Sum(MeetingPrem) Order By prem Descending, Cou Descending

    Dim AgentCount = meetingreport.Count
    Dim agentlist = From c In meetingreport Select c.AgentName

    Try
        For Each agnt As String In agentlist
            Dim newdatarow As DataRow
            newdatarow = a.NewRow()
            newdatarow.Item(0) = agnt.ToString
            Dim agentnamerow As String = agnt
            Dim meetingbyagent = From c In db.AppMeetingViews Select c.AgentName, c.MeetingDate, c.MeetingId, c.MeetingPrem
                            Where MeetingDate >= fromdate And MeetingDate <= todate And AgentName = agentnamerow
                            Group By AgentName Into Cou = Count(MeetingId), prem = Sum(MeetingPrem) Order By prem Descending, Cou Descending

            For col = 0 To reportcolumns - 1
                If a.Columns.Item(col).ToString <> "AgentName" Then
                    begmonth = a.Columns.Item(col).ToString
                    endmonth = DateAdd(DateInterval.Month, 1, begmonth)
                    endmonth = DateAdd(DateInterval.Day, -1, endmonth)
                    moni = begmonth.ToString
                    moni1 = endmonth.ToString

                    Dim monthslice = From c In db.AppMeetingViews
                                     Select c.AgentName, c.MeetingDate, c.MeetingId, c.MeetingPrem
                                     Where MeetingDate >= begmonth.ToString And MeetingDate <= endmonth.ToString And
                                     AgentName = agentnamerow Group By AgentName Into montttprem = Sum(MeetingPrem), montttmeetings = Count(MeetingId)

                    If monthslice.Count > 0 Then

                        Dim agentlistmonths = From c In monthslice Select c.montttprem

                        If agentlistmonths IsNot Nothing Then
                            Dim prem As Integer = agentlistmonths.Sum
                            If prem = Nothing Then yprem = 0.0 Else yprem = prem
                            newdatarow.Item(col) = yprem
                        Else
                        End If
                    Else
                    End If
                Else
                End If
            Next col
            a.Rows.Add(newdatarow)
        Next agnt

        Me.ReportSource = CType(Me.FindResource("Report"), CollectionViewSource)
        Me.ReportSource.Source = a.DefaultView
        Me.Report = CType(Me.ReportSource.View, BindingListCollectionView)
        ReportDataGrid.Visibility = Windows.Visibility.Visible
        Dim gildsa = ReportDataGrid.Items
    Catch ex As Exception
        MsgBox(ex.Message)
        '   ReportDataGrid.ItemsSource = a.AsDataView
    End Try
End Sub

推荐答案

AFAICT的主要问题是,您正在手动滚动datagrid列等(与Winforms Development一样).我在切换到新的工作方式时遇到了麻烦,但是一旦习惯了,它就会变得简单得多.

您几乎要做的就是获取包含要查看的行的数据集,并将其绑定到XAML中的DataGrid.这里有一篇有关如何执行此操作的文章: WPF DataGrid实用示例 [
The main problem AFAICT, is that you are hand-rolling the datagrid columns etc (As you would with Winforms Development). I had trouble switching to the new way of working, but once you are used to it, it''s much simpler.

Pretty much all you need to do is get the dataset containing the rows you want to see, and bind it to the DataGrid in XAML. There is an article on how to do this here: WPF DataGrid Practical Examples[^]

Hope this helps!


这篇关于网格中不可见的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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