473,548 Members | 2,683 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

AllowColumnReor der used programmaticall y

VR
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
Nov 15 '05 #1
2 4888
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
Nov 15 '05 #2
VR
Thanks for your help. It works flawlessly.

VR
-----Original Message-----
Hi,

Sending the lvm_setcolumnor derarray message to the listview along withan array that specifies the left to right order of the columns will allowyou to
programaticall y change the order of the columns in the listview. TheListView 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, ByValSortOrder As Integer())
If SortOrder.Lengt h < 1 Then Exit Sub
If SortOrder.Lengt h > ListViewToReord er.Columns.Coun t Then ExitSub
Dim liSetColumnOrde rArray As Integer = LVM_SETCOLUMNOR DERARRAY Dim liSuccess As Integer
liSuccess = ListViewColumnO rder (ListView1.Hand le.ToInt32,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 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 aWindows
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 locationin the
array
lvitem.SubItems .Add(arrData (intLVItemCount er,intSubItemCoun ter))
Next

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

Next

'Setup imagelist
imageListSmall. Images.Add(Bitm ap.FromFile ("C:\ProgramFiles\Microsof t
Visual Studio .NET\Common7 \Graphics\icons \Traffic\trffc0 5.ico")) imageListSmall. Images.Add(Bitm ap.FromFile ("C:\ProgramFiles\Microsof t
Visual Studio .NET\Common7 \Graphics\icons \Traffic\trffc0 6.ico")) imageListSmall. Images.Add(Bitm ap.FromFile ("C:\ProgramFiles\Microsof t
Visual Studio .NET\Common7 \Graphics\icons \Traffic\trffc0 7.ico")) 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 AsSystem.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 ListView, ByValSortOrder
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.Hand le.ToInt32,liSetColumnOrd erArray, SortOrder.Lengt h, SortOrder(0))
If Not liSuccess.Equal s(0) Then
ListView1.Refre sh()
End If
End Sub


End Class
.

Nov 15 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
2345
by: Mike Bennett | last post by:
Does the .NET framework (using VB.NET) support the ability to programmatically STOP a device prior to its removal from the system? I have need to move data from a computer on one network to a computer on a different network. For various reasons, I am not allowed to join these networks. Additionally, neither computer is allowed to disconnect...
3
3702
by: R Reyes | last post by:
Just wondering if anyone knows the pros/cons between creating a database programmatically vs using the application's tool windows/features that come with SQLServer, Access, Oracle, etc... Is it the same? Does it really matter? I have created databases with .NET programmatically and it works fine, but why would anyone want to do all this...
1
9967
by: John Criswell | last post by:
I have created a radio button list programmatically. C# SqlConnection conn = new SqlConnection("data source=localhost; integrated security=true; initial catalog=pubs"); SqlCommand cmdAuthors = new SqlCommand("select * from Authors", conn); SqlDataReader dr; dr = cmdAuthors.ExecuteReader(); RadioButtonList rblAuthors = new...
5
6785
by: Carlo Marchesoni | last post by:
From an aspx page (A.aspx) I open another one (B.aspx - for table lookup). When the user selects an entry in B.aspx I would like to force a button's event in A.aspx to be fired. I guess the only way is using javascript - does anybody have a sample for this ? Thanks
4
3073
by: BentleyInc | last post by:
I'm trying to find a way to add a whildcard application mapping to aspnet_isapi.dll in IIS programmatically.... been looking into IIS administrator reference but didn't find the right function to use. The equivalent GUI steps would be, open IIS, select my application->properties, app configuration, in whildcard application mapping type the...
6
11532
by: Matt Frame | last post by:
I have a client that has asked us to get a digital signature certificate and start digitally signing all files we pass between each other. I have heard of the subject and know about the certs but I have no idea how to do something like this with VB.Net. Has anyone done something like this or know where I can find out information how to...
3
2224
by: J M | last post by:
When you set the Listview's property AllowColumnReorder to True, users can reorder the columns (quite logical I guess)... Bus how on earth can I obtain from code what order these columns are in??? I want to save these settings (to the registry or to a file) so that the program can recall which order of columns the user prefers.... Thanks
5
15280
by: Brian McClellan | last post by:
Just wondering if anyone has a simple example of creating a gridview completely programmatically, i'm not doing anything terribly sophisticated. When creating the gridview declaratively evertying works fine, however programmatically, while the grid will display data that exsists in the database, any operation on the data (...
3
2629
by: Ken Fine | last post by:
I'm interested in programmatically manipulating groups of ASP.NET controls by type. Can someone suggest code for the following? Loop through, say, all label controls on a page, and assigning a CssClass to them, or programmatically making the Visible/not Visible. If applied to a containing page, such a function would traverse all user...
0
7512
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7707
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7466
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7803
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
3495
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3475
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1926
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1051
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
751
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.