AllowColumnReorder used programmatically

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • VR

    #1

    AllowColumnReorder used programmatically

    Please, help.

    Is there any way to take advantage of re-ordering columns
    in ListView control programmaticall y when
    AllowColumnReor der is set to true?

    I have a ListView with 3 columns, LabelEdit is set to
    true. That allows user to edit 0th column. I want the user
    to be able to edit the 1st (or the middle) column. I
    figured my item (0th) column should be somehow switched
    with the 1st sub-item's column.

    It's pretty easy to do by setting AllowColumnReor der to
    true and then, manually re-orderding columns by drag-and-
    dropping them.

    Is there any way to do it programmaticall y without user
    intervention?

    Thanks.
    VR
  • Parker Zhang [MSFT]

    #2
    RE: AllowColumnReor der used programmaticall y

    Hi,

    Sending the lvm_setcolumnor derarray message to the listview along with
    an array that specifies the left to right order of the columns will allow
    you to
    programatically change the order of the columns in the listview. The
    ListView view
    should be set to Details

    Here is VB.NET code. I think you can easily convert it to C#.

    'Sendmessage declare
    Const LVM_FIRST As Integer = &H1000
    Const LVM_SETCOLUMNOR DERARRAY = LVM_FIRST + 58

    <DllImport("use r32.dll", EntryPoint:="Se ndMessageA")> _
    Private Shared Function ListViewColumnO rder(ByVal hwnd As Integer,
    ByVal Msg As
    Integer, ByVal wParam As Integer, ByRef lParam As Integer) As Integer
    End Function


    'Sub to change order of columns

    Public Sub SetColumnOrder( ByVal ListViewToReord er As ListView, ByVal
    SortOrder As Integer())
    If SortOrder.Lengt h < 1 Then Exit Sub
    If SortOrder.Lengt h > ListViewToReord er.Columns.Coun t Then Exit
    Sub
    Dim liSetColumnOrde rArray As Integer = LVM_SETCOLUMNOR DERARRAY
    Dim liSuccess As Integer
    liSuccess = ListViewColumnO rder(ListView1. Handle.ToInt32,
    liSetColumnOrde rArray, SortOrder.Lengt h, SortOrder(0))
    If Not liSuccess.Equal s(0) Then
    ListView1.Refre sh()
    End If
    End Sub


    'Code to call sub - pass desired order of columns in an array
    Dim sortorder() As Integer
    ReDim sortorder(ListV iew1.Columns.Co unt - 1)
    sortorder(0) = 2 'assumes 3 columns
    sortorder(1) = 1
    sortorder(2) = 0
    SetColumnOrder( ListView1, sortorder)


    Below is a sample that illustrates how this could be done. It uses a
    Windows
    Application project with a ListView and a Commnad button added.

    Imports System.Runtime. InteropServices
    Public Class Form1
    Inherits System.Windows. Forms.Form

    Const LVM_FIRST As Integer = &H1000
    Const LVM_SETCOLUMNOR DERARRAY = LVM_FIRST + 58

    <DllImport("use r32.dll", EntryPoint:="Se ndMessageA")> _
    Private Shared Function ListViewColumnO rder(ByVal hwnd As Integer,
    ByVal Msg As
    Integer, ByVal wParam As Integer, ByRef lParam As Integer) As Integer
    End Function

    Dim FirstTime As Boolean = True
    Dim arrData(2, 2) As String


    Windows Form Designer generated code

    Private Sub Form1_Load(ByVa l sender As System.Object, _
    ByVal e As System.EventArg s) Handles MyBase.Load

    'Fill the array with some data
    arrData(0, 0) = "Charlotte"
    arrData(0, 1) = "NC"
    arrData(0, 2) = "(980) 555-5555"
    arrData(1, 0) = "Dallas"
    arrData(1, 1) = "TX"
    arrData(1, 2) = "(917) 333-3333"
    arrData(2, 0) = "Redmond"
    arrData(2, 1) = "WA"
    arrData(2, 2) = "(425) 888-8888"

    SetupListView()

    End Sub

    Sub SetupListView()
    Dim lv As ListView = ListView1
    Dim lvitem As ListViewItem
    Dim i As Long
    Dim ch As ColumnHeader
    Dim lvsitem As ListViewItem.Li stViewSubItem
    Dim lvsitem2 As ListViewItem.Li stViewSubItem
    Dim intLVItemCounte r As Integer
    Dim intSubItemCount er As Integer

    'Clear the Listview, enable GridLines and FullRowSelect
    lv.Clear()
    lv.GridLines = True
    lv.FullRowSelec t = True
    lv.View = View.Details
    Dim imageListSmall As New ImageList()

    For intLVItemCounte r = 0 To arrData.GetUppe rBound(0)
    'Create a new ListViewItem, and set its Text to
    'the appropriate value from the array
    lvitem = New ListViewItem()
    lvitem.Text = arrData(intLVIt emCounter, 0)

    'Loop through the array elements of the first dimension
    'to create SubItems.
    For intSubItemCount er = 1 To arrData.GetUppe rBound(1)
    'Add a SubItem with the text from the appropriate location
    in the
    array
    lvitem.SubItems .Add(arrData(in tLVItemCounter,
    intSubItemCount er))
    Next

    'Add the ListViewItem to the control
    lv.Items.Add(lv item)

    Next

    'Setup imagelist
    imageListSmall. Images.Add(Bitm ap.FromFile("C: \Program
    Files\Microsoft
    Visual Studio .NET\Common7\Gr aphics\icons\Tr affic\trffc05.i co"))
    imageListSmall. Images.Add(Bitm ap.FromFile("C: \Program
    Files\Microsoft
    Visual Studio .NET\Common7\Gr aphics\icons\Tr affic\trffc06.i co"))
    imageListSmall. Images.Add(Bitm ap.FromFile("C: \Program
    Files\Microsoft
    Visual Studio .NET\Common7\Gr aphics\icons\Tr affic\trffc07.i co"))
    lv.SmallImageLi st = imageListSmall

    For i = 0 To lv.Items.Count - 1
    lv.Items(i).Ima geIndex = i
    Next i

    'Add columns to the control
    AddColumns("Cit y")
    AddColumns("Sta te")
    AddColumns("Tel ephone")

    For Each ch In lv.Columns
    ch.Width = -2
    Next

    End Sub

    Sub AddColumns(ByVa l Text As String)
    Dim lv As ListView = ListView1
    Dim ch As ColumnHeader
    ch = New ColumnHeader()
    ch.Text = Text
    lv.Columns.Add( ch)
    End Sub


    Private Sub Button1_Click(B yVal sender As System.Object, ByVal e As
    System.EventArg s) Handles Button1.Click

    Static c As Integer
    ListView1.Allow ColumnReorder = True
    ListView1.Label Edit = True
    Dim sortorder() As Integer
    ReDim sortorder(ListV iew1.Columns.Co unt - 1)
    If FirstTime Then
    c = 2
    FirstTime = False
    End If
    If c > 2 Then c = 0
    sortorder(0) = c
    c += 1
    If c > 2 Then c = 0
    sortorder(1) = c
    c += 1
    If c > 2 Then c = 0
    sortorder(2) = c

    SetColumnOrder( ListView1, sortorder)

    End Sub


    Public Sub SetColumnOrder( ByVal ListViewToReord er As ListView, ByVal
    SortOrder
    As Integer())
    If SortOrder.Lengt h < 1 Then Exit Sub
    If SortOrder.Lengt h > ListViewToReord er.Columns.Coun t Then Exit Sub
    Dim liSetColumnOrde rArray As Integer = LVM_SETCOLUMNOR DERARRAY
    Dim liSuccess As Integer
    liSuccess = ListViewColumnO rder(ListView1. Handle.ToInt32,
    liSetColumnOrde rArray, SortOrder.Lengt h, SortOrder(0))
    If Not liSuccess.Equal s(0) Then
    ListView1.Refre sh()
    End If
    End Sub




    End Class


    Comment

    • VR

      #3
      RE: AllowColumnReor der used programmaticall y

      Thanks for your help. It works flawlessly.

      VR
      [color=blue]
      >-----Original Message-----
      >Hi,
      >
      >Sending the lvm_setcolumnor derarray message to the[/color]
      listview along with[color=blue]
      >an array that specifies the left to right order of the[/color]
      columns will allow[color=blue]
      >you to
      >programaticall y change the order of the columns in the[/color]
      listview. The[color=blue]
      >ListView view
      >should be set to Details
      >
      >Here is VB.NET code. I think you can easily convert it to[/color]
      C#.[color=blue]
      >
      >'Sendmessage declare
      > Const LVM_FIRST As Integer = &H1000
      > Const LVM_SETCOLUMNOR DERARRAY = LVM_FIRST + 58
      >
      > <DllImport("use r32.dll", EntryPoint:="Se ndMessageA")>[/color]
      _[color=blue]
      > Private Shared Function ListViewColumnO rder(ByVal[/color]
      hwnd As Integer,[color=blue]
      >ByVal Msg As
      >Integer, ByVal wParam As Integer, ByRef lParam As[/color]
      Integer) As Integer[color=blue]
      > End Function
      >
      >
      >'Sub to change order of columns
      >
      > Public Sub SetColumnOrder( ByVal ListViewToReord er As[/color]
      ListView, ByVal[color=blue]
      >SortOrder As Integer())
      > If SortOrder.Lengt h < 1 Then Exit Sub
      > If SortOrder.Lengt h >[/color]
      ListViewToReord er.Columns.Coun t Then Exit[color=blue]
      >Sub
      > Dim liSetColumnOrde rArray As Integer =[/color]
      LVM_SETCOLUMNOR DERARRAY[color=blue]
      > Dim liSuccess As Integer
      > liSuccess = ListViewColumnO rder[/color]
      (ListView1.Hand le.ToInt32,[color=blue]
      >liSetColumnOrd erArray, SortOrder.Lengt h, SortOrder(0))
      > If Not liSuccess.Equal s(0) Then
      > ListView1.Refre sh()
      > End If
      > End Sub
      >
      >
      >'Code to call sub - pass desired order of columns in an[/color]
      array[color=blue]
      > Dim sortorder() As Integer
      > ReDim sortorder(ListV iew1.Columns.Co unt - 1)
      > sortorder(0) = 2 'assumes 3 columns
      > sortorder(1) = 1
      > sortorder(2) = 0
      > SetColumnOrder( ListView1, sortorder)
      >
      >
      >Below is a sample that illustrates how this could be[/color]
      done. It uses a[color=blue]
      >Windows
      >Application project with a ListView and a Commnad button[/color]
      added.[color=blue]
      >
      >Imports System.Runtime. InteropServices
      >Public Class Form1
      > Inherits System.Windows. Forms.Form
      >
      > Const LVM_FIRST As Integer = &H1000
      > Const LVM_SETCOLUMNOR DERARRAY = LVM_FIRST + 58
      >
      > <DllImport("use r32.dll", EntryPoint:="Se ndMessageA")>[/color]
      _[color=blue]
      > Private Shared Function ListViewColumnO rder(ByVal[/color]
      hwnd As Integer,[color=blue]
      >ByVal Msg As
      >Integer, ByVal wParam As Integer, ByRef lParam As[/color]
      Integer) As Integer[color=blue]
      > End Function
      >
      > Dim FirstTime As Boolean = True
      > Dim arrData(2, 2) As String
      >
      >
      >Windows Form Designer generated code
      >
      >Private Sub Form1_Load(ByVa l sender As System.Object, _
      > ByVal e As System.EventArg s)[/color]
      Handles MyBase.Load[color=blue]
      >
      > 'Fill the array with some data
      > arrData(0, 0) = "Charlotte"
      > arrData(0, 1) = "NC"
      > arrData(0, 2) = "(980) 555-5555"
      > arrData(1, 0) = "Dallas"
      > arrData(1, 1) = "TX"
      > arrData(1, 2) = "(917) 333-3333"
      > arrData(2, 0) = "Redmond"
      > arrData(2, 1) = "WA"
      > arrData(2, 2) = "(425) 888-8888"
      >
      > SetupListView()
      >
      > End Sub
      >
      > Sub SetupListView()
      > Dim lv As ListView = ListView1
      > Dim lvitem As ListViewItem
      > Dim i As Long
      > Dim ch As ColumnHeader
      > Dim lvsitem As ListViewItem.Li stViewSubItem
      > Dim lvsitem2 As ListViewItem.Li stViewSubItem
      > Dim intLVItemCounte r As Integer
      > Dim intSubItemCount er As Integer
      >
      > 'Clear the Listview, enable GridLines and[/color]
      FullRowSelect[color=blue]
      > lv.Clear()
      > lv.GridLines = True
      > lv.FullRowSelec t = True
      > lv.View = View.Details
      > Dim imageListSmall As New ImageList()
      >
      > For intLVItemCounte r = 0 To arrData.GetUppe rBound[/color]
      (0)[color=blue]
      > 'Create a new ListViewItem, and set its Text[/color]
      to[color=blue]
      > 'the appropriate value from the array
      > lvitem = New ListViewItem()
      > lvitem.Text = arrData(intLVIt emCounter, 0)
      >
      > 'Loop through the array elements of the first[/color]
      dimension[color=blue]
      > 'to create SubItems.
      > For intSubItemCount er = 1 To[/color]
      arrData.GetUppe rBound(1)[color=blue]
      > 'Add a SubItem with the text from the[/color]
      appropriate location[color=blue]
      >in the
      >array
      > lvitem.SubItems .Add(arrData[/color]
      (intLVItemCount er,[color=blue]
      >intSubItemCoun ter))
      > Next
      >
      > 'Add the ListViewItem to the control
      > lv.Items.Add(lv item)
      >
      > Next
      >
      > 'Setup imagelist
      > imageListSmall. Images.Add(Bitm ap.FromFile[/color]
      ("C:\Program[color=blue]
      >Files\Microsof t
      >Visual Studio .NET\Common7[/color]
      \Graphics\icons \Traffic\trffc0 5.ico"))[color=blue]
      > imageListSmall. Images.Add(Bitm ap.FromFile[/color]
      ("C:\Program[color=blue]
      >Files\Microsof t
      >Visual Studio .NET\Common7[/color]
      \Graphics\icons \Traffic\trffc0 6.ico"))[color=blue]
      > imageListSmall. Images.Add(Bitm ap.FromFile[/color]
      ("C:\Program[color=blue]
      >Files\Microsof t
      >Visual Studio .NET\Common7[/color]
      \Graphics\icons \Traffic\trffc0 7.ico"))[color=blue]
      > lv.SmallImageLi st = imageListSmall
      >
      > For i = 0 To lv.Items.Count - 1
      > lv.Items(i).Ima geIndex = i
      > Next i
      >
      > 'Add columns to the control
      > AddColumns("Cit y")
      > AddColumns("Sta te")
      > AddColumns("Tel ephone")
      >
      > For Each ch In lv.Columns
      > ch.Width = -2
      > Next
      >
      > End Sub
      >
      > Sub AddColumns(ByVa l Text As String)
      > Dim lv As ListView = ListView1
      > Dim ch As ColumnHeader
      > ch = New ColumnHeader()
      > ch.Text = Text
      > lv.Columns.Add( ch)
      > End Sub
      >
      >
      > Private Sub Button1_Click(B yVal sender As[/color]
      System.Object, ByVal e As[color=blue]
      >System.EventAr gs) Handles Button1.Click
      >
      > Static c As Integer
      > ListView1.Allow ColumnReorder = True
      > ListView1.Label Edit = True
      > Dim sortorder() As Integer
      > ReDim sortorder(ListV iew1.Columns.Co unt - 1)
      > If FirstTime Then
      > c = 2
      > FirstTime = False
      > End If
      > If c > 2 Then c = 0
      > sortorder(0) = c
      > c += 1
      > If c > 2 Then c = 0
      > sortorder(1) = c
      > c += 1
      > If c > 2 Then c = 0
      > sortorder(2) = c
      >
      > SetColumnOrder( ListView1, sortorder)
      >
      > End Sub
      >
      >
      > Public Sub SetColumnOrder( ByVal ListViewToReord er As[/color]
      ListView, ByVal[color=blue]
      >SortOrder
      >As Integer())
      > If SortOrder.Lengt h < 1 Then Exit Sub
      > If SortOrder.Lengt h >[/color]
      ListViewToReord er.Columns.Coun t Then Exit Sub[color=blue]
      > Dim liSetColumnOrde rArray As Integer =[/color]
      LVM_SETCOLUMNOR DERARRAY[color=blue]
      > Dim liSuccess As Integer
      > liSuccess = ListViewColumnO rder[/color]
      (ListView1.Hand le.ToInt32,[color=blue]
      >liSetColumnOrd erArray, SortOrder.Lengt h, SortOrder(0))
      > If Not liSuccess.Equal s(0) Then
      > ListView1.Refre sh()
      > End If
      > End Sub
      >
      >
      >
      >
      >End Class
      >
      >
      >.
      >[/color]

      Comment

      Working...