Solved

How to AllowColumnReorder programmatically on listview - C# .Net 1.1

Posted on 2008-06-16
1
1,118 Views
Last Modified: 2013-12-17
How to AllowColumnReorder programmatically in C# .Net 1.1???
Here is a working sample in VB.net:
http://bytes.com/forum/thread223912.html

My conversion does not work in C#, it causes a nullreference exception...
What's wrong???
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Runtime.InteropServices;
using System.Diagnostics;
 
namespace WindowsApplication5
{
	/// <summary>
	/// Summary description for Form1.
	/// </summary>
	public class Form1 : System.Windows.Forms.Form
	{
 
		static int LVM_SETCOLUMNORDERARRAY = 0x103A;  //LVM_FIRST + 58;
		static int LVM_GETCOLUMNORDERARRAY = 0x103B;  //LVM_FIRST + 59;
//		[DllImport("user32.dll", EntryPoint="SendMessageA")]
//		static extern int ListViewColumnOrder(Int32 hwnd , Int32 Msg , Int32 wParam , Int32 lParam );
		[DllImport("user32.dll")]
		public static extern bool ListViewColumnOrder(IntPtr hWnd, Int32 msg, Int32 wParam, IntPtr lParam);
		
 
//		[DllImport("user32.dll")]
//		static extern bool SendMessage(IntPtr hWnd, Int32 msg, Int32 wParam,ref LVCOLUMN lParam);
 
		bool FirstTime = true;
 
		private System.Windows.Forms.Button button1;
		private System.Windows.Forms.ListView listView1;
		private System.Windows.Forms.ColumnHeader columnHeader1;
		private System.Windows.Forms.ColumnHeader columnHeader2;
		private System.Windows.Forms.ColumnHeader columnHeader3;
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;
 
		public Form1()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();
 
			//
			// TODO: Add any constructor code after InitializeComponent call
			//
		}
 
		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (components != null) 
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}
 
		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			System.Windows.Forms.ListViewItem listViewItem1 = new System.Windows.Forms.ListViewItem("fsdfdas");
			System.Windows.Forms.ListViewItem listViewItem2 = new System.Windows.Forms.ListViewItem("fsdfdsa");
			System.Windows.Forms.ListViewItem listViewItem3 = new System.Windows.Forms.ListViewItem("erterwtwer");
			this.button1 = new System.Windows.Forms.Button();
			this.listView1 = new System.Windows.Forms.ListView();
			this.columnHeader1 = new System.Windows.Forms.ColumnHeader();
			this.columnHeader2 = new System.Windows.Forms.ColumnHeader();
			this.columnHeader3 = new System.Windows.Forms.ColumnHeader();
			this.SuspendLayout();
			// 
			// button1
			// 
			this.button1.Location = new System.Drawing.Point(72, 248);
			this.button1.Name = "button1";
			this.button1.Size = new System.Drawing.Size(120, 16);
			this.button1.TabIndex = 0;
			this.button1.Text = "button1";
			this.button1.Click += new System.EventHandler(this.button1_Click);
			// 
			// listView1
			// 
			this.listView1.AllowColumnReorder = true;
			this.listView1.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] {
																						this.columnHeader1,
																						this.columnHeader2,
																						this.columnHeader3});
			this.listView1.GridLines = true;
			this.listView1.Items.AddRange(new System.Windows.Forms.ListViewItem[] {
																					  listViewItem1,
																					  listViewItem2,
																					  listViewItem3});
			this.listView1.Location = new System.Drawing.Point(0, 8);
			this.listView1.MultiSelect = false;
			this.listView1.Name = "listView1";
			this.listView1.Size = new System.Drawing.Size(288, 240);
			this.listView1.TabIndex = 1;
			this.listView1.View = System.Windows.Forms.View.Details;
			// 
			// columnHeader2
			// 
			this.columnHeader2.Text = "ColumnHeader2";
			// 
			// columnHeader3
			// 
			this.columnHeader3.Text = "ColumnHeader3";
			// 
			// Form1
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(292, 273);
			this.Controls.Add(this.listView1);
			this.Controls.Add(this.button1);
			this.Name = "Form1";
			this.Text = "Form1";
			this.ResumeLayout(false);
 
		}
		#endregion
 
		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[STAThread]
		static void Main() 
		{
			Application.Run(new Form1());
		}
 
		int c=0;
		Int32 [] sortorder;
		private void button1_Click(object sender, System.EventArgs e)
		{
		
			//sortorder=new int(ListView1.Columns.Count - 1);
 
			if(FirstTime){
				c = 2;
				FirstTime = false;
				sortorder=new Int32[3]; 
			}
			if( c > 2 )c = 0;
			sortorder[0] = c;
			c += 1;
			if( c > 2) c = 0;
			sortorder[1] = c;
			c += 1;
			if( c > 2 )c = 0;
			sortorder[2] = c;
 
			SetColumnOrder(listView1, sortorder);
		
		}
 
		public void SetColumnOrder(ListView ListViewToReorder , int[] SortOrder )
		{
			if( SortOrder.Length < 1 )return;
			if( SortOrder.Length > ListViewToReorder.Columns.Count) return;
			Int32 liSetColumnOrderArray = LVM_SETCOLUMNORDERARRAY;
			Int32 handle=listView1.Handle.ToInt32();
			Int32 slen=SortOrder.Length;
			this.Handle 
			int liSuccess = ListViewColumnOrder(handle, liSetColumnOrderArray, slen, SortOrder[0]);
			if( liSuccess!=0) 
			{
				listView1.Refresh();
			}
		}
 
}
}

Open in new window

0
Comment
Question by:cosie
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 

Accepted Solution

by:
cosie earned 0 total points
ID: 21811379
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
Computer science students often experience many of the same frustrations when going through their engineering courses. This article presents seven tips I found useful when completing a bachelors and masters degree in computing which I believe may he…
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question