Please, help.
Is there any way to take advantage of re-ordering columns
in ListView control programmatically when
AllowColumnReorder 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 AllowColumnReorder to
true and then, manually re-orderding columns by drag-and-
dropping them.
Is there any way to do it programmatically without user
intervention?
Thanks.
VR 2 4878
Hi,
Sending the lvm_setcolumnorderarray 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_SETCOLUMNORDERARRAY = LVM_FIRST + 58
<DllImport("user32.dll", EntryPoint:="SendMessageA")> _
Private Shared Function ListViewColumnOrder(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 ListViewToReorder As ListView, ByVal
SortOrder As Integer())
If SortOrder.Length < 1 Then Exit Sub
If SortOrder.Length > ListViewToReorder.Columns.Count Then Exit
Sub
Dim liSetColumnOrderArray As Integer = LVM_SETCOLUMNORDERARRAY
Dim liSuccess As Integer
liSuccess = ListViewColumnOrder(ListView1.Handle.ToInt32,
liSetColumnOrderArray, SortOrder.Length, SortOrder(0))
If Not liSuccess.Equals(0) Then
ListView1.Refresh()
End If
End Sub
'Code to call sub - pass desired order of columns in an array
Dim sortorder() As Integer
ReDim sortorder(ListView1.Columns.Count - 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_SETCOLUMNORDERARRAY = LVM_FIRST + 58
<DllImport("user32.dll", EntryPoint:="SendMessageA")> _
Private Shared Function ListViewColumnOrder(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(ByVal sender As System.Object, _
ByVal e As System.EventArgs) 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.ListViewSubItem
Dim lvsitem2 As ListViewItem.ListViewSubItem
Dim intLVItemCounter As Integer
Dim intSubItemCounter As Integer
'Clear the Listview, enable GridLines and FullRowSelect
lv.Clear()
lv.GridLines = True
lv.FullRowSelect = True
lv.View = View.Details
Dim imageListSmall As New ImageList()
For intLVItemCounter = 0 To arrData.GetUpperBound(0)
'Create a new ListViewItem, and set its Text to
'the appropriate value from the array
lvitem = New ListViewItem()
lvitem.Text = arrData(intLVItemCounter, 0)
'Loop through the array elements of the first dimension
'to create SubItems.
For intSubItemCounter = 1 To arrData.GetUpperBound(1)
'Add a SubItem with the text from the appropriate location
in the
array
lvitem.SubItems.Add(arrData(intLVItemCounter,
intSubItemCounter))
Next
'Add the ListViewItem to the control
lv.Items.Add(lvitem)
Next
'Setup imagelist
imageListSmall.Images.Add(Bitmap.FromFile("C:\Prog ram
Files\Microsoft
Visual Studio .NET\Common7\Graphics\icons\Traffic\trffc05.ico"))
imageListSmall.Images.Add(Bitmap.FromFile("C:\Prog ram
Files\Microsoft
Visual Studio .NET\Common7\Graphics\icons\Traffic\trffc06.ico"))
imageListSmall.Images.Add(Bitmap.FromFile("C:\Prog ram
Files\Microsoft
Visual Studio .NET\Common7\Graphics\icons\Traffic\trffc07.ico"))
lv.SmallImageList = imageListSmall
For i = 0 To lv.Items.Count - 1
lv.Items(i).ImageIndex = i
Next i
'Add columns to the control
AddColumns("City")
AddColumns("State")
AddColumns("Telephone")
For Each ch In lv.Columns
ch.Width = -2
Next
End Sub
Sub AddColumns(ByVal 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(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Static c As Integer
ListView1.AllowColumnReorder = True
ListView1.LabelEdit = True
Dim sortorder() As Integer
ReDim sortorder(ListView1.Columns.Count - 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 ListViewToReorder As ListView, ByVal
SortOrder
As Integer())
If SortOrder.Length < 1 Then Exit Sub
If SortOrder.Length > ListViewToReorder.Columns.Count Then Exit Sub
Dim liSetColumnOrderArray As Integer = LVM_SETCOLUMNORDERARRAY
Dim liSuccess As Integer
liSuccess = ListViewColumnOrder(ListView1.Handle.ToInt32,
liSetColumnOrderArray, SortOrder.Length, SortOrder(0))
If Not liSuccess.Equals(0) Then
ListView1.Refresh()
End If
End Sub
End Class
Thanks for your help. It works flawlessly.
VR -----Original Message----- Hi,
Sending the lvm_setcolumnorderarray message to the
listview along withan array that specifies the left to right order of the
columns will allowyou to programatically 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_SETCOLUMNORDERARRAY = LVM_FIRST + 58
<DllImport("user32.dll", EntryPoint:="SendMessageA")>
_ Private Shared Function ListViewColumnOrder(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 ListViewToReorder As
ListView, ByValSortOrder As Integer()) If SortOrder.Length < 1 Then Exit Sub If SortOrder.Length >
ListViewToReorder.Columns.Count Then ExitSub Dim liSetColumnOrderArray As Integer =
LVM_SETCOLUMNORDERARRAY Dim liSuccess As Integer liSuccess = ListViewColumnOrder
(ListView1.Handle.ToInt32,liSetColumnOrderArray, SortOrder.Length, SortOrder(0)) If Not liSuccess.Equals(0) Then ListView1.Refresh() End If End Sub
'Code to call sub - pass desired order of columns in an
array Dim sortorder() As Integer ReDim sortorder(ListView1.Columns.Count - 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_SETCOLUMNORDERARRAY = LVM_FIRST + 58
<DllImport("user32.dll", EntryPoint:="SendMessageA")>
_ Private Shared Function ListViewColumnOrder(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(ByVal sender As System.Object, _ ByVal e As System.EventArgs)
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.ListViewSubItem Dim lvsitem2 As ListViewItem.ListViewSubItem Dim intLVItemCounter As Integer Dim intSubItemCounter As Integer
'Clear the Listview, enable GridLines and
FullRowSelect lv.Clear() lv.GridLines = True lv.FullRowSelect = True lv.View = View.Details Dim imageListSmall As New ImageList()
For intLVItemCounter = 0 To arrData.GetUpperBound
(0) 'Create a new ListViewItem, and set its Text
to 'the appropriate value from the array lvitem = New ListViewItem() lvitem.Text = arrData(intLVItemCounter, 0)
'Loop through the array elements of the first
dimension 'to create SubItems. For intSubItemCounter = 1 To
arrData.GetUpperBound(1) 'Add a SubItem with the text from the
appropriate locationin the array lvitem.SubItems.Add(arrData
(intLVItemCounter,intSubItemCounter)) Next
'Add the ListViewItem to the control lv.Items.Add(lvitem)
Next
'Setup imagelist imageListSmall.Images.Add(Bitmap.FromFile
("C:\ProgramFiles\Microsoft Visual Studio .NET\Common7
\Graphics\icons\Traffic\trffc05.ico")) imageListSmall.Images.Add(Bitmap.FromFile
("C:\ProgramFiles\Microsoft Visual Studio .NET\Common7
\Graphics\icons\Traffic\trffc06.ico")) imageListSmall.Images.Add(Bitmap.FromFile
("C:\ProgramFiles\Microsoft Visual Studio .NET\Common7
\Graphics\icons\Traffic\trffc07.ico")) lv.SmallImageList = imageListSmall
For i = 0 To lv.Items.Count - 1 lv.Items(i).ImageIndex = i Next i
'Add columns to the control AddColumns("City") AddColumns("State") AddColumns("Telephone")
For Each ch In lv.Columns ch.Width = -2 Next
End Sub
Sub AddColumns(ByVal 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(ByVal sender As
System.Object, ByVal e AsSystem.EventArgs) Handles Button1.Click
Static c As Integer ListView1.AllowColumnReorder = True ListView1.LabelEdit = True Dim sortorder() As Integer ReDim sortorder(ListView1.Columns.Count - 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 ListViewToReorder As
ListView, ByValSortOrder As Integer()) If SortOrder.Length < 1 Then Exit Sub If SortOrder.Length >
ListViewToReorder.Columns.Count Then Exit Sub Dim liSetColumnOrderArray As Integer =
LVM_SETCOLUMNORDERARRAY Dim liSuccess As Integer liSuccess = ListViewColumnOrder
(ListView1.Handle.ToInt32,liSetColumnOrderArray, SortOrder.Length, SortOrder(0)) If Not liSuccess.Equals(0) Then ListView1.Refresh() End If End Sub
End Class
. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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...
|
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 =...
|
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...
|
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...
|
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...
|
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???...
|
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...
|
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...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
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,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
| |