2014-01-31 107 views
0

我正在创建一个程序,需要在Log_Type = I中过滤最小log_time,在Log_Type = O中过滤max logtime。需要在reportviewer中显示。谢谢你们,我不知道如何在最小和最大vb.net的最小值和最大值

enter image description here

enter image description here

+0

什么的ReportViewer的数据源? –

+0

数据集sir mysql数据库。 –

回答

2

开始让你在使用一个DataSet作为DataSource针对从MySql -database充满了ReportViewer。然后,您应该首先筛选DataSet或通过sql进行筛选。

例如(通过Linq-To-DataSet):

Dim groups = From row In ds.Tables(0) 
      Let id = row.Field(Of Int32)("ID") 
      Let empId = row.Field(Of String)("EMP_ID").Trim() 
      Let logType = row.Field(Of String)("LOG_TYPE").Trim() 
      Let logDate = row.Field(Of Date)("LOG_DATE") 
      Let logTime = row.Field(Of TimeSpan)("LOG_TIME") 
      Let creditDate = row.Field(Of Date)("CREDIT_DATE") 
      Select data = New With {id, empId, logType, logDate, logTime, creditDate, row} 
      Group data By data.empId, data.logType Into TypeGroup = Group 

Dim newDataSource As DataTable = ds.Tables(0).Clone() ' empty table with same schema ' 
For Each grp In groups 
    Dim iGroup = From data In grp.TypeGroup 
       Where StringComparer.OrdinalIgnoreCase.Equals("i", data.logType) 
    Dim min = (From data In iGroup Order By data.logTime Ascending).FirstOrDefault() 
    Dim max = (From data In iGroup Order By data.logTime Descending).FirstOrDefault() 
    If min IsNot Nothing Then 
     newDataSource.Rows.Add(min.id, min.empId, min.logType, min.logDate, min.logTime, min.creditDate) 
    End If 
    If max IsNot Nothing AndAlso Not Object.ReferenceEquals(min, max) Then 
     newDataSource.Rows.Add(max.id, max.empId, max.logType, max.logDate, max.logTime, max.creditDate) 
    End If 
    Dim oGroup = From data In grp.TypeGroup 
       Where StringComparer.OrdinalIgnoreCase.Equals("o", data.logType) 
    min = (From data In oGroup Order By data.logTime Ascending).FirstOrDefault() 
    max = (From data In oGroup Order By data.logTime Descending).FirstOrDefault() 
    If min IsNot Nothing Then 
     newDataSource.Rows.Add(min.id, min.empId, min.logType, min.logDate, min.logTime, min.creditDate) 
    End If 
    If max IsNot Nothing AndAlso Not Object.ReferenceEquals(min, max) Then 
     newDataSource.Rows.Add(max.id, max.empId, max.logType, max.logDate, max.logTime, max.creditDate) 
    End If 
Next 

ds.Tables.Clear() 
ds.Tables.Add(newDataSource) 
+0

先生,我宣布ds为数据集,然后我得到了这个错误 - 变量'ds'在它被赋值之前被使用。运行时可能会导致空引用异常。 –

+0

感谢代码sir @Tim。 –

+0

先生另一个错误@tim对象引用未设置为对象的实例。 –