在多列上排序kendo网格

时间:2015-07-15 18:08:06

标签: kendo-grid kendo-asp.net-mvc kendo-sortable

我有一个剑道网格。当页面加载时,默认情况下我想按列1 然后按 column2按降序对网格进行排序。

问题: 按预期排序,但排序箭头显示在最后排序的列上。因此,在下面的情况下,当页面加载时,排序箭头位于" DueDate"而不是" DownloadDate"

Imports System.Windows.Forms.DataVisualization.Charting
Imports System.IO

... ... ...

Dim myDBCS As String = Main.DBCS    'create local copy of database connection string
Dim myAPPFOLDER As String = Main.APPFOLDER  'local copy of scratchpad folder
Dim ReportStart, ReportEnd As DateTime  'reporting dates, persistent
Dim dblMin, dblMax As Double    'minimum and maximum values for y axis settings
Dim sql As String   'holds sql for construction & execute
Dim Chart1 As New Chart 'rate1 chart
Dim Chart2 As New Chart 'subs chart

... ... ...

Public Sub PTRSubs()
'/////////////////////////////////
'Build & execute SQL Query, Parse results, build chart
'/////////////////////////////////

'build sql
Dim cols As String = ""
sql = "SELECT DateTime, Gosnell, Lepanto, Manila, Reginold " _
  & "FROM pt_demand WHERE DateTime BETWEEN '" & ReportStart.ToString & "' AND '" & ReportEnd.ToString _
  & "' ORDER BY DateTime ASC;"

'/////////////////////////////////
'Setup Chart2
'if we're talking more than 1 day's worth of data, change the x axis type to datetime, otherwise, leave as time
Dim x As Boolean
If DateDiff(DateInterval.Day, ReportStart, ReportEnd) > 1 Then
    x = True
Else
    x = False
End If

Chart2.Size = New System.Drawing.Size(1280, 575)
Chart2.Series.Clear()
Chart2.ChartAreas.Clear()

Chart2.ChartAreas.Add("Subs")

'/////////////////////////////////
'define series parameters
Chart2.Series.Add("Gosnell")
Chart2.Series("Gosnell").ChartArea = "Subs"
Chart2.Series("Gosnell").Color = Color.Gray
Chart2.Series("Gosnell").ChartType = DataVisualization.Charting.SeriesChartType.FastLine
Chart2.Series("Gosnell").XValueType = DataVisualization.Charting.ChartValueType.Time
If x = True Then
    Chart2.Series("Gosnell").XValueType = DataVisualization.Charting.ChartValueType.DateTime
Else
    Chart2.Series("Gosnell").XValueType = DataVisualization.Charting.ChartValueType.Time
End If

Chart2.Series.Add("Lepanto")
Chart2.Series("Lepanto").ChartArea = "Subs"
Chart2.Series("Lepanto").Color = Color.Purple
Chart2.Series("Lepanto").ChartType = DataVisualization.Charting.SeriesChartType.FastLine
Chart2.Series("Lepanto").XValueType = DataVisualization.Charting.ChartValueType.Time
If x = True Then
    Chart2.Series("Lepanto").XValueType = DataVisualization.Charting.ChartValueType.DateTime
Else
    Chart2.Series("Lepanto").XValueType = DataVisualization.Charting.ChartValueType.Time
End If

Chart2.Series.Add("Manila")
Chart2.Series("Manila").ChartArea = "Subs"
Chart2.Series("Manila").Color = Color.Red
Chart2.Series("Manila").ChartType = DataVisualization.Charting.SeriesChartType.FastLine
Chart2.Series("Manila").XValueType = DataVisualization.Charting.ChartValueType.Time
If x = True Then
    Chart2.Series("Manila").XValueType = DataVisualization.Charting.ChartValueType.DateTime
Else
    Chart2.Series("Manila").XValueType = DataVisualization.Charting.ChartValueType.Time
End If

Chart2.Series.Add("Reginold")
Chart2.Series("Reginold").ChartArea = "Subs"
Chart2.Series("Reginold").Color = Color.Orange
Chart2.Series("Reginold").ChartType = DataVisualization.Charting.SeriesChartType.FastLine
Chart2.Series("Reginold").XValueType = DataVisualization.Charting.ChartValueType.Time
If x = True Then
    Chart2.Series("Reginold").XValueType = DataVisualization.Charting.ChartValueType.DateTime
Else
    Chart2.Series("Reginold").XValueType = DataVisualization.Charting.ChartValueType.Time
End If

'/////////////////////////////////
'Connect to database, execute query
On Error GoTo Err_Control
Dim ThisRecKey As Guid
Dim strGetDemandsSQL As String = sql
Using sConnection As New SqlConnection(myDBCS)
    sConnection.Open()
    Dim GetRecKeySQLCommand As New SqlCommand(strGetDemandsSQL, sConnection)
    'Get the Rec_Key
    Dim reader As SqlDataReader = GetRecKeySQLCommand.ExecuteReader()
    Dim ThisDateTime As Date

    '/////////////////////////////////
    'build series datapoints
    While reader.Read()
        'Add Points
        Debug.Print(reader(0) & "    " & reader(1) & "    " & reader(2) & "    " & reader(3) & "    " & reader(4))
        ThisDateTime = reader(0)

        Chart2.Series("Gosnell").Points.AddXY(ThisDateTime.ToOADate(), CInt(reader(1)))
        Chart2.Series("Lepanto").Points.AddXY(ThisDateTime.ToOADate(), CInt(reader(2)))
        Chart2.Series("Manila").Points.AddXY(ThisDateTime.ToOADate(), CInt(reader(3)))
        Chart2.Series("Reginold").Points.AddXY(ThisDateTime.ToOADate(), CInt(reader(4)))

    End While
    reader.Close()
    sConnection.Close()
End Using

dblMin = 0
dblMax = 0

SetMinMax(dblMin, dblMax, "Gosnell")
If dblMax = 0 Then          'if axis min/max is not already set, then set both min and max based on these values them based on these values
    Chart2.ChartAreas("Subs").AxisY.Minimum = dblMin
    Chart2.ChartAreas("Subs").AxisY.Maximum = dblMax

Else
    If dblMin < Chart2.ChartAreas("Subs").AxisY.Minimum Then Chart2.ChartAreas("Subs").AxisY.Minimum = dblMin 'only reset if value is lower than current setting
    If dblMax > Chart2.ChartAreas("Subs").AxisY.Maximum Then Chart2.ChartAreas("Subs").AxisY.Maximum = dblMax 'only reset if value is higher than current setting

End If

SetMinMax(dblMin, dblMax, "Lepanto")
If dblMax = 0 Then          'if axis min/max is not already set, then set both min and max based on these values them based on these values
    Chart2.ChartAreas("Subs").AxisY.Minimum = dblMin
    Chart2.ChartAreas("Subs").AxisY.Maximum = dblMax

Else
    If dblMin < Chart2.ChartAreas("Subs").AxisY.Minimum Then Chart2.ChartAreas("Subs").AxisY.Minimum = dblMin 'only reset if value is lower than current setting
    If dblMax > Chart2.ChartAreas("Subs").AxisY.Maximum Then Chart2.ChartAreas("Subs").AxisY.Maximum = dblMax 'only reset if value is higher than current setting

End If

SetMinMax(dblMin, dblMax, "Manila")
If dblMax = 0 Then          'if axis min/max is not already set, then set both min and max based on these values them based on these values
    Chart2.ChartAreas("Subs").AxisY.Minimum = dblMin
    Chart2.ChartAreas("Subs").AxisY.Maximum = dblMax

Else
    If dblMin < Chart2.ChartAreas("Subs").AxisY.Minimum Then Chart2.ChartAreas("Subs").AxisY.Minimum = dblMin 'only reset if value is lower than current setting
    If dblMax > Chart2.ChartAreas("Subs").AxisY.Maximum Then Chart2.ChartAreas("Subs").AxisY.Maximum = dblMax 'only reset if value is higher than current setting

End If

SetMinMax(dblMin, dblMax, "Reginold")
If dblMax = 0 Then          'if axis min/max is not already set, then set both min and max based on these values them based on these values
    Chart2.ChartAreas("Subs").AxisY.Minimum = dblMin
    Chart2.ChartAreas("Subs").AxisY.Maximum = dblMax

Else
    If dblMin < Chart2.ChartAreas("Subs").AxisY.Minimum Then Chart2.ChartAreas("Subs").AxisY.Minimum = dblMin 'only reset if value is lower than current setting
    If dblMax > Chart2.ChartAreas("Subs").AxisY.Maximum Then Chart2.ChartAreas("Subs").AxisY.Maximum = dblMax 'only reset if value is higher than current setting

End If

'/////////////////////////////////
'set axis title
Chart2.ChartAreas("Subs").AxisX.Title = "Report window: " & Format(ReportStart, "yyyy/MM/dd HH:mm") & " to " & Format(ReportEnd, "yyyy/MM/dd HH:mm")

DumpChartToImage(Chart2, "Chart2")

Exit_Here:
Exit Sub

Err_Control:
Select Case Err.Number
    Case Else
        MsgBox("PTRSubs" & vbCrLf _
         & "Error Number: " & Err.Number.ToString & vbCrLf _
         & "Description:  " & Err.Description.ToString & vbCrLf _
         & "Source:  " & Err.Source, MsgBoxStyle.OkOnly, "errcontrol")
        Resume Exit_Here
End Select
End Sub

Private Sub SetMinMax(ByRef Min As Double, ByRef Max As Double, FieldName As String)
'/////////////////////////////////
'Get min and Max values of data from selected set
'/////////////////////////////////

On Error GoTo Err_Control

Dim strGetMaxSQL As String = "Select MAX(" & FieldName & ") FROM pt_demand WHERE DateTime Between '" & ReportStart.ToString & "' AND '" & ReportEnd.ToString & "'"
Dim strGetMinSQL As String = "Select MIN(" & FieldName & ") FROM pt_demand WHERE DateTime Between '" & ReportStart.ToString & "' AND '" & ReportEnd.ToString & "'"
Using sConnection As New SqlConnection(myDBCS)
    sConnection.Open()
    'MAX
    Dim GetMaxSQLCommand As New SqlCommand(strGetMaxSQL, sConnection)
    Dim readerMax As SqlDataReader = GetMaxSQLCommand.ExecuteReader()

    While readerMax.Read()
        Max = CDbl(CInt(readerMax(0) + (0.01 * readerMax(0))))
    End While
    readerMax.Close()
    'MIN
    Dim GetMinSQLCommand As New SqlCommand(strGetMinSQL, sConnection)
    'Get the Rec_Key
    Dim readerMin As SqlDataReader = GetMinSQLCommand.ExecuteReader()

    While readerMin.Read()
        Min = CDbl(CInt(readerMin(0) - (0.01 * readerMin(0))))
    End While
    readerMin.Close()

    sConnection.Close()
End Using

Exit_Here:
Exit Sub

Err_Control:
Select Case Err.Number
    Case Else
        Resume Exit_Here
End Select
End Sub

Private Sub DumpChartToImage(ByRef mychart As Chart, ByRef file As String)
'//////////////////////
'Save chart to file on disk, first wipe old copy if already exists
'//////////////////////
Dim filename As String = myAPPFOLDER & file & ".png"
If System.IO.File.Exists(filename) Then
    'The file exists, delete it first
    System.IO.File.Delete(filename)
    'recreate the file
    mychart.SaveImage(filename, System.Drawing.Imaging.ImageFormat.Png)

Else
    'the file doesn't exist, create it
    mychart.SaveImage(filename, System.Drawing.Imaging.ImageFormat.Png)

End If

End Sub

3 个答案:

答案 0 :(得分:10)

您当前添加要排序的列的方式基本上会覆盖上一列,并且只会考虑您编写的最后一列(在本例中为DueDate)。发生这种情况是因为您的.Sort()被写为单个语句。

为了让您的排序正常工作,您应该将.Sort()更改为:

.Sort(x =>
{
    x.Add(y=>y.DownloadDate).Descending());
    x.Add(y=>y.DueDate).Descending());
} 

答案 1 :(得分:0)

如果要让用户同时按多列和/或取消排序进行排序,请进行以下更改:

自: .Sortable(x =&gt; x.AllowUnsort(false))

于: .Sortable(x =&gt; x     .SortMode(GridSortMode.MultipleColumn)     .AllowUnsort(真) )

答案 2 :(得分:0)

建议的答案中存在语法错误。排序语句应为:

.Sort(x =>
{
    x.Add(y=>y.DownloadDate).Descending();
    x.Add(y=>y.DueDate).Descending();
})