VB按颜色排序 [英] VB sort by color

查看:81
本文介绍了VB按颜色排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我需要获取重复的记录到DataGridView的顶部.我想出了如何给这些颜色上色,但对如何进行分类一无所知.任何帮助和建议将不胜感激.谢谢


'我如何为这些着色?

       昏暗的dt作为新的数据表
        dt = TryCast(DataGridView1.DataSource,DataTable)

        Dim i As Integer
        Dim j As Integer

       对于i = 0到DataGridView1.Rows.Count-2
           对于j = i + 1到DataGridView1.Rows.Count-2

               如果(DataGridView1.Rows(i).Cells(8).Value = DataGridView1.Rows(j).Cells(8).Value)然后
                    Me.Label8.Text ="1"
                    DataGridView1.Rows(i).Cells(8).Style.BackColor = Color.LightPink
                    DataGridView1.Rows(j).Cells(8).Style.BackColor = Color.LightPink
               其他Me.Label8.Text ="0"
                   如果结束

           下一个
       下一个

理想情况下,我很想使用: 

 

我 .DataGridView1.SortMode = DataGridViewSortMode.可能是格式,但我无法使用...

或如果我在下面添加代码,则无法正常工作,因为'e'出现错误(未处理的异常)





我在下面尝试了代码,但是如果我删除行,则会在其下出现另一个行

   私有子Button10_Click(作为对象发送者,作为EventArgs发送者)处理Button10.Click
        Dim jui As Integer


       对于jui = 0到DataGridView1.RowCount-1

            'DataGridView1.Rows(jui).Visible = True
           如果DataGridView1.Rows(jui).Cells(8).Style.BackColor = Color.Aqua然后
                DataGridView1.Rows(jui).Visible = False
            ElseIf DataGridView1.Rows(jui).Cells(8).Style.BackColor = Color.Aqua = False然后
                DataGridView1.Rows(jui).Visible = True
           如果结束


       下一个

   结束子

解决方案

不能使用"Me.DataGridView1.SortMode = DataGridViewSortMode"不存在.

也不对列单元的backcolor或forecolor或color属性进行DataGridView排序.您将不得不编写代码以某种方式执行该操作.

这些属性可用;

DataGridView .SortedColumn属性

也可以看到

如何:设置列的排序方式在Windows窗体DataGridView控件中

DataGridViewColumn.SortMode
在Windows窗体DataGridView控件中排序数据
Windows窗体DataGridView控件中的列排序模式
如何:在Windows窗体DataGridView控件中自定义排序

代码块通常用于显示代码,这样,当您将代码粘贴到下面的代码块中时,该代码将变得清晰可见,而没有所有未注释掉的非代码文本出现.我想通过不注释掉所有非代码 在您的帖子代码块显示时,向代码块发送任何对代码块有效的文本.

但是,如果您在第8列(我想实际上是索引8,第9列)上设置了列排序,然后在DataGridView的一行中选择第8列(我想对cell(8)假设是9),则该列将进行排序.如果将其设置为升序或降序,则DataGridView将排序 在该列上.我通常将所有列设置为排序,因此,如果我选择一个列,则DataGridView将对该列进行排序.

昏暗的dt作为新的数据表
         dt = TryCast(DataGridView1.DataSource,DataTable)

         昏暗的整数
         Dim j作为整数

         对于i = 0到DataGridView1.Rows.Count-2
             对于j = i + 1到DataGridView1.Rows.Count-2

                 如果(DataGridView1.Rows(i).Cells(8).Value = DataGridView1.Rows(j).Cells(8).Value)然后
                     Me.Label8.Text ="1";
                     DataGridView1.Rows(i).Cells(8).Style.BackColor = Color.LightPink
                     DataGridView1.Rows(j).Cells(8).Style.BackColor = Color.LightPink
                 其他Me.Label8.Text ="0";
                     万一

             下一个
         下一个


静态arrCol()为Color = {Color.LightPink,Color.HotPink}',Color.Red,Color.Moccasin,Color.LightGreen,Color.White

        昏暗的dgv作为DataGridView =发送者
        昏暗的col1index为整数= Array.IndexOf(arrCol,dgv.Rows(e.RowIndex1).DefaultCellStyle.BackColor)
        Dim col2index As Integer = Array.IndexOf(arrCol,dgv.Rows(e.RowIndex2).DefaultCellStyle.BackColor)
        如果col1index> -1或col2index> -1然后
            如果col1index = -1,则col1index = arrCol.Length
            如果col2index = -1,则col2index = arrCol.Length
            e.SortResult = Math.Sign(col1index-col2index)
            e.Handled = col1index<> col2index
        别的
            e.Handled = False
        如果结束


Hi,

I need to get duplicate records to top of DataGridView. I figured out how to colour these, but could not get my head around on how to sort. Any help and  advice will be much appreciated. Thanks.


'HOW I did color these

        Dim dt As New DataTable
        dt = TryCast(DataGridView1.DataSource, DataTable)

        Dim i As Integer
        Dim j As Integer

        For i = 0 To DataGridView1.Rows.Count - 2
            For j = i + 1 To DataGridView1.Rows.Count - 2

                If (DataGridView1.Rows(i).Cells(8).Value = DataGridView1.Rows(j).Cells(8).Value) Then
                    Me.Label8.Text = "1"
                    DataGridView1.Rows(i).Cells(8).Style.BackColor = Color.LightPink
                    DataGridView1.Rows(j).Cells(8).Style.BackColor = Color.LightPink
                Else Me.Label8.Text = "0"
                    End If

            Next
        Next

Ideally I would love to use : 

Me.DataGridView1.SortMode = DataGridViewSortMode. possibly Format, but I can't get it work...

Or if I add code below, it is not working, as 'e' comes as an error (unhandled exception)


   Static arrCol() As Color = {Color.LightPink, Color.HotPink} ' , Color.Red, Color.Moccasin, Color.LightGreen, Color.White

        Dim dgv As DataGridView = sender
        Dim col1index As Integer = Array.IndexOf(arrCol, dgv.Rows(e.RowIndex1).DefaultCellStyle.BackColor)
        Dim col2index As Integer = Array.IndexOf(arrCol, dgv.Rows(e.RowIndex2).DefaultCellStyle.BackColor)
        If col1index > -1 Or col2index > -1 Then
            If col1index = -1 Then col1index = arrCol.Length
            If col2index = -1 Then col2index = arrCol.Length
            e.SortResult = Math.Sign(col1index - col2index)
            e.Handled = col1index <> col2index
        Else
            e.Handled = False
        End If
 




I tried code below, but if I delete row there anothedr row appears under it

    Private Sub Button10_Click(sender As Object, e As EventArgs) Handles Button10.Click
        Dim jui As Integer


        For jui = 0 To DataGridView1.RowCount - 1

            'DataGridView1.Rows(jui).Visible = True
            If DataGridView1.Rows(jui).Cells(8).Style.BackColor = Color.Aqua Then
                DataGridView1.Rows(jui).Visible = False
            ElseIf DataGridView1.Rows(jui).Cells(8).Style.BackColor = Color.Aqua = False Then
                DataGridView1.Rows(jui).Visible = True
            End If


        Next

    End Sub

解决方案

Well you can't use something "Me.DataGridView1.SortMode = DataGridViewSortMode" which does not exist.

Nor does a DataGridView sort on the columns cells backcolor or forecolor or a color property. You would have to write code to perform that somehow.

These properties are available;

DataGridView.SortOrder Property (poor code example IMO)

DataGridView.SortedColumn Property

Also see

How to: Set the Sort Modes for Columns in the Windows Forms DataGridView Control

DataGridViewColumn.SortMode
Sorting Data in the Windows Forms DataGridView Control
Column Sort Modes in the Windows Forms DataGridView Control
How to: Customize Sorting in the Windows Forms DataGridView Control

And a Code Block is for displaying code typically such that the code will then appear legible as your code pasted into the below code block, without all the non-code text that is not commented out, looks. I suppose by not commenting out all the non-code text whatever works for a code block malfunctions as your posts code block shows.

However if you set column sorting on column 8 (index 8, column 9 really I suppose) and then select column 8 (9 I suppose for cell(8)) in a row on the DataGridView the column will sort. If it is set for Ascending or Descending the DataGridView will sort on that column. I usuall set all Columns to sort so if I select a Column then the DataGridView will sort on that column.

       Dim dt As New DataTable
         dt = TryCast(DataGridView1.DataSource, DataTable)

         Dim i As Integer
         Dim j As Integer

         For i = 0 To DataGridView1.Rows.Count - 2
             For j = i + 1 To DataGridView1.Rows.Count - 2

                 If (DataGridView1.Rows(i).Cells(8).Value = DataGridView1.Rows(j).Cells(8).Value) Then
                     Me.Label8.Text = "1"
                     DataGridView1.Rows(i).Cells(8).Style.BackColor = Color.LightPink
                     DataGridView1.Rows(j).Cells(8).Style.BackColor = Color.LightPink
                 Else Me.Label8.Text = "0"
                     End If

             Next
         Next


Static arrCol() As Color = {Color.LightPink, Color.HotPink} ' , Color.Red, Color.Moccasin, Color.LightGreen, Color.White

        Dim dgv As DataGridView = sender
        Dim col1index As Integer = Array.IndexOf(arrCol, dgv.Rows(e.RowIndex1).DefaultCellStyle.BackColor)
        Dim col2index As Integer = Array.IndexOf(arrCol, dgv.Rows(e.RowIndex2).DefaultCellStyle.BackColor)
        If col1index > -1 Or col2index > -1 Then
            If col1index = -1 Then col1index = arrCol.Length
            If col2index = -1 Then col2index = arrCol.Length
            e.SortResult = Math.Sign(col1index - col2index)
            e.Handled = col1index <> col2index
        Else
            e.Handled = False
        End If


这篇关于VB按颜色排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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