如何在vb.net中对数据网格视图的3列进行排序

huangapple go评论73阅读模式
英文:

How to sort 3 columns of datagridview in vb.net

问题

请参考下面的翻译结果:

Sort information in the Size column :

在尺码列中排序:

Desired Result

期望结果

ProductCode Remark ColorCode Size Qty
TEST 3000 TEST BHTF10 S 12
TEST 3000 TEST BHTF10 M 65
TEST 3000 TEST BHTF10 5L 25
TEST 2000 TEST BHTF15 S 55
TEST 1000 TEST BHTF15 6L 10
英文:

Below I want to sort for the columns "ProductCode", "ColorCode" and "Size". Please Guide.

Sort information in the Size column :

Size Number sequence
XS 1
S 2
M 3
L 4
XL 5
XXL 6
2L 7
3L 8
4L 9
5L 10
6L 11
Private Sub Form3_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Dim table1 As New DataTable("Players")
    table1.Columns.Add(New DataColumn("ProductCode", GetType(String)))
    table1.Columns.Add(New DataColumn("Remark", GetType(String)))
    table1.Columns.Add(New DataColumn("ColorCode", GetType(String)))
    table1.Columns.Add(New DataColumn("Size", GetType(String)))
    table1.Columns.Add(New DataColumn("Qty", GetType(Integer)))
    table1.Rows.Add("TEST 3000", "TEST", "BHTF10", "5L", 25)
    table1.Rows.Add("TEST 2000", "TEST", "BHTF15", "S", 55)
    table1.Rows.Add("TEST 3000", "TEST", "BHTF10", "M", 65)
    table1.Rows.Add("TEST 1000", "TEST", "BHTF25", "6L", 10)
    table1.Rows.Add("TEST 3000", "TEST", "BHTF10", "S", 12)
    DataGridView1.DataSource = table1
End Sub

Desired Result

ProductCode Remark ColorCode Size Qty
TEST 3000 TEST BHTF10 S 12
TEST 3000 TEST BHTF10 M 65
TEST 3000 TEST BHTF10 5L 25
TEST 2000 TEST BHTF15 S 55
TEST 1000 TEST BHTF15 6L 10

答案1

得分: 0

要在VB.NET中对一个DataGridView的三列进行排序,您可以使用DataGridView控件的Sort方法。Sort方法允许您指定要排序的列以及排序方向。以下是如何操作的逐步指南:

假设您有一个名为dataGridView1DataGridView,其中包含三列分别命名为Column1Column2Column3,以下是如何对它们进行排序的方法:

Private Sub SortDataGridView()
    ' 对第一列进行升序排序
    dataGridView1.Sort(dataGridView1.Columns("Column1"), ListSortDirection.Ascending)

    ' 对第二列进行升序排序
    dataGridView1.Sort(dataGridView1.Columns("Column2"), ListSortDirection.Ascending)

    ' 对第三列进行降序排序
    dataGridView1.Sort(dataGridView1.Columns("Column3"), ListSortDirection.Descending)
End Sub
英文:

To sort three columns of a DataGridView in VB.NET, you can use the Sort method of the DataGridView control. The Sort method allows you to specify the column to sort and the sort direction. Here's a step-by-step guide on how to do it:

Assuming you have a DataGridView named dataGridView1 with three columns named Column1, Column2, and Column3, here's how you can sort them:

Private Sub SortDataGridView()
    ' Sort the first column in ascending order
    dataGridView1.Sort(dataGridView1.Columns("Column1"), ListSortDirection.Ascending)

    ' Sort the second column in ascending order
    dataGridView1.Sort(dataGridView1.Columns("Column2"), ListSortDirection.Ascending)

    ' Sort the third column in descending order
    dataGridView1.Sort(dataGridView1.Columns("Column3"), ListSortDirection.Descending)
End Sub

答案2

得分: 0

因此,考虑到您对尺寸有特定的排序要求,而不完全知道如何获取它,我假设它存储在某个表中。为了简化排序,您可以将其添加到表中,然后隐藏DataGridView中的该列。

我已经稍微更改了构建表格的代码。

Dim table1 As New DataTable("Players")
table1.Columns.Add(New DataColumn("ProductCode", GetType(String)))
table1.Columns.Add(New DataColumn("Remark", GetType(String)))
table1.Columns.Add(New DataColumn("ColorCode", GetType(String)))
table1.Columns.Add(New DataColumn("Size", GetType(String)))
table1.Columns.Add(New DataColumn("Qty", GetType(Integer)))
table1.Columns.Add(New DataColumn("SizePriority", GetType(Integer)))
table1.Rows.Add("TEST 3000", "TEST", "BHTF10", "5L", 25, 10)
table1.Rows.Add("TEST 2000", "TEST", "BHTF15", "S", 55, 2)
table1.Rows.Add("TEST 3000", "TEST", "BHTF10", "M", 65, 3)
table1.Rows.Add("TEST 1000", "TEST", "BHTF25", "6L", 10, 11)
table1.Rows.Add("TEST 3000", "TEST", "BHTF10", "S", 12, 2)

您会注意到我添加了SizePriority列,并设置了值,以便按照您在问题顶部指定的尺寸排序属性进行排序。

接下来要做的事情是应用排序。使用BindingSource是jmcilhinney在他的评论中建议的理想方法,但由于您似乎不愿意接受他的建议,我使用了一个DataView(我会将DataView描述为位于DataTable之上的可视层)。使用这个DataView,您可以使用与Sql Order by子句相同的语法应用排序属性。请注意,在您期望的输出中,产品代码是按降序排序的,因此我添加了Desc关键字来进行排序。

Dim table1View As New DataView(table1)
table1View.Sort = "ProductCode Desc, ColorCode, SizePriority"
For Each row As DataRowView In table1View
    Console.WriteLine($"{row(0)}, {row(1)}, {row(2)}, {row(3)}, {row(4)}")
Next

这将给我以下输出:

产品代码 备注 颜色代码 尺寸 数量
TEST 3000 TEST BHTF10 S 12
TEST 3000 TEST BHTF10 M 65
TEST 3000 TEST BHTF10 5L 25
TEST 2000 TEST BHTF15 S 55
TEST 1000 TEST BHTF25 6L 10
英文:

So being as you've got a specific order for the size, without knowing completely how you've can derive that I'm assuming that it's stored in a table somewhere. For simplicity's sake to facilitate the ordering you can add that to table, then hide the column in the DataGridView

I've changed the code for building the table slightly here.

Dim table1 As New DataTable("Players")
table1.Columns.Add(New DataColumn("ProductCode", GetType(String)))
table1.Columns.Add(New DataColumn("Remark", GetType(String)))
table1.Columns.Add(New DataColumn("ColorCode", GetType(String)))
table1.Columns.Add(New DataColumn("Size", GetType(String)))
table1.Columns.Add(New DataColumn("Qty", GetType(Integer)))
table1.Columns.Add(New DataColumn("SizePriority", GetType(Integer)))
table1.Rows.Add("TEST 3000", "TEST", "BHTF10", "5L", 25, 10)
table1.Rows.Add("TEST 2000", "TEST", "BHTF15", "S", 55, 2)
table1.Rows.Add("TEST 3000", "TEST", "BHTF10", "M", 65, 3)
table1.Rows.Add("TEST 1000", "TEST", "BHTF25", "6L", 10, 11)
table1.Rows.Add("TEST 3000", "TEST", "BHTF10", "S", 12, 2)

You will notice the SizePriority column I've added and set the value to the ordering attributes for the sizes you specified at the top of your question.

Next thing to do is apply the ordering. Using a BindingSource as was recommended by jmcilhinney in his comments would 100% be the ideal way to do this but since you seem resistant to taking his advice, I'm using a DataView (I would describe a DataView as the Visual Layer that sits over top of DataTables). Using this you would apply the sort property using the same syntax as an Sql Order by clause. Notice in your desired output, you had the Product code ordered descending, so added the Desc keyword to the sort.

Dim table1View As New DataView(table1)
table1View.Sort = "ProductCode Desc, ColorCode, SizePriority"
For Each row As DataRowView In table1View
    Console.WriteLine($"{row(0)}, {row(1)}, {row(2)}, {row(3)}, {row(4)}")
Next

Which gives me the following output

Product Code Remark ColorCode Size Qty
TEST 3000 TEST BHTF10 S 12
TEST 3000 TEST BHTF10 M 65
TEST 3000 TEST BHTF10 5L 25
TEST 2000 TEST BHTF15 S 55
TEST 1000 TEST BHTF25 6L 10

huangapple
  • 本文由 发表于 2023年7月31日 21:27:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/76804108.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定