如何使用以下列创建datagridview:
我试图像这样创建一个dgv但我的问题在于排序。所有字段都以字符串形式出现,我无法完全排序[Date Time]。是否可以使用包含可包含日期时间数据类型或字符串值的列的dgv?
有关我的方案的更多信息: 我有一个带有列表框的表单,由最终用户手动填写。此列表框包含我们的SQL数据库中可能存在/可能不存在的序列号。我调用的第一个函数是检查s / n是否存在并根据其存在返回true或false。如果它存在,那么我将它存储在一个字符串数组中;如果不是,它存储在List(Of String)中。存储后,我使用它们返回tsql代码(SQLstrIn& SQLstrNotIn)。这一小段代码做得非常好:
Dim IsIn As String = ""
Dim IsNotIn As New List(Of String)
For Each line As String In SNList.Items
If SQLData.CheckIfInDb("'" & line & "'") = True Then
IsIn += "'" & line & "',"
Else : IsNotIn.Add("('" & line & "')")
End If
Next
If IsIn.Count > 0 Then
IsIn = "(" & IsIn.Remove(IsIn.Length - 1) & ")"
SQLstrIn = SQLData.SQLtoReturn(IsIn)
Else : SQLstrIn = ""
End If
If IsNotIn.Count > 0 Then
SQLstrNotIn = SQLData.SQLtoReturn(IsNotIn)
Else : SQLstrNotIn = ""
End If
如果s / n存在,那么我使用此函数生成tsql代码:
Public Shared Function SQLtoReturn(ByVal InStr As String) As String
Dim SQLstr As New StringBuilder
SQLstr.Append("SELECT DISTINCT")
SQLstr.Append(" RTRIM(LTrim(ce.PartSerialNumber)) [Part Serial Number], ")
SQLstr.Append(" RTRIM(LTrim(p.PartNumber)) [Part Number], ")
SQLstr.Append(" CONVERT(varchar(10), DateTime, 101) + STUFF(RIGHT(CONVERT(varchar(26), DateTime, 109), 15), 7, 7, ' ') [Date Time], ")
'SQLstr.Append(" ce.DateTime [Date Time], ")
SQLstr.Append(" RTRIM(LTrim(cp.Description)) [Description], ")
SQLstr.Append(" RTRIM(LTrim(cp.Side)) [Side], ")
SQLstr.Append(" RTRIM(LTrim(ce.EventOutcome)) [Event Outcome], ")
SQLstr.Append(" RTRIM(LTrim(ce.EventOutcomeInfo)) [Event Outcome Info] ")
SQLstr.Append("FROM ")
SQLstr.Append(" CollectionEvents AS ce INNER JOIN ")
SQLstr.Append(" CollectionPoints AS cp ON ce.CollectionPointId = cp.CollectionPointId INNER JOIN ")
SQLstr.Append(" Products AS p ON cp.ProductIdValue = p.ProductId ")
SQLstr.Append("WHERE ")
SQLstr.Append(" (ce.PartSerialNumber IN " & InStr & ") ")
Return SQLstr.ToString
End Function
如果s / n不存在,那么我使用这个函数:
Public Shared Function SQLtoReturn(ByVal NotInStr As List(Of String)) As String
Dim SQLstr As New StringBuilder
Dim rtrnString As String = ""
For i As Integer = 0 To NotInStr.Count - 1
SQLstr.Append("SELECT ")
SQLstr.Append(NotInStr.Item(i).ToString & " [Part Serial Number], ") '1
SQLstr.Append("'No Data Found' [Part Number], ") '2
SQLstr.Append("'No Data Found' [Date Time], ") '3
SQLstr.Append("'No Data Found' Description, ") '4
SQLstr.Append("'No Data Found' Side, ") '5
SQLstr.Append("'No Data Found' [Event Outcome], ") '6
SQLstr.Append("'No Data Found' [Event Outcome Info] ") '7
If (i < NotInStr.Count - 1) Then SQLstr.Append("UNION")
SQLstr.Append(vbCrLf)
rtrnString += SQLstr.ToString
Next
Return SQLstr.ToString
End Function
如果两个函数都返回tsql数据,那么它们一起被UNIONed;如果一个不返回tsql代码,那么它不包括在内。这段代码基本上是最后一步:
If SQLstrIn.Length > 0 And SQLstrNotIn.Length > 0 Then
SQLstr = SQLstrIn & vbNewLine & "UNION" & vbNewLine & SQLstrNotIn
ElseIf SQLstrIn.Length > 0 And SQLstrNotIn.Length = 0 Then
SQLstr = SQLstrIn
ElseIf SQLstrIn.Length = 0 And SQLstrNotIn.Length > 0 Then
SQLstr = SQLstrNotIn
End If
Dim dt As DataTable = New DataTable
dt.Columns.Add("Part Serial Number", GetType(String))
dt.Columns.Add("Part Number", GetType(String))
dt.Columns.Add("DateTime", GetType(Object))
dt.Columns.Add("Description", GetType(String))
dt.Columns.Add("Side", GetType(String))
dt.Columns.Add("Event Outcome", GetType(String))
dt.Columns.Add("Event Outcome Info", GetType(String))
Dim objConn As New SqlConnection(DatabaseConnection.FISSQLConnectionString)
objConn.Open()
Dim objCommand As SqlCommand = objConn.CreateCommand
objCommand.CommandText = SQLstr
Dim objReader As SqlDataReader = objCommand.ExecuteReader
If objReader.HasRows Then
While objReader.Read
With objReader
dt.Rows.Add(.GetString(0), .GetString(1), .GetValue(2), .GetString(3), .GetString(4), .GetString(5), .GetString(6))
End With
End While
End If
Dim dataview As DataView = dt.DefaultView
dataview.Sort = "DateTime ASC"
dgvSNEvents.DataSource = dataview.ToTable
我在DateTime字段中使用了GetType(Object),希望它存储日期/时间值或字符串值。最终,我仍然无法按s / n和日期排序。仍在学习最好的方法,所以如果有人有更好的方法,我很乐意看到它们。
日期时间列仍然有点偏。
答案 0 :(得分:2)
DataGridView
的默认行为是可以对列进行排序(即通过单击标题);在任何情况下,您都可以通过影响SortMode
属性来强制执行此行为。关于日期,您可以将Date type
列添加到给定的DataSource
。 DataGridView1
的示例代码:
Dim dt As DataTable = New DataTable
dt.Columns.Add("col", GetType(Date))
dt.Rows.Add(New Date(2000, 2, 1, 10, 10, 0))
dt.Rows.Add(New Date(2002, 5, 10, 10, 10, 0))
dt.Rows.Add(New Date(1995, 5, 10, 10, 11, 10))
DataGridView1.DataSource = dt
DataGridView1.Columns(0).SortMode = DataGridViewColumnSortMode.Automatic 'Just to make sure
DataGridView1
中的行将通过单击第一列的标题进行排序。如果您想要从头开始排序所有记录,可以对DataSource
(dt
)进行排序;示例代码:
Dim dataView As DataView = dt.DefaultView
dataView.Sort = "col asc"
DataGridView1.DataSource = dataView.ToTable() 'DGV sorted ascendently