how to reorder columns in listview?

Thanks

Recommended Answers

All 12 Replies

If you click on the liste view, in the properties menu, you can set "Allow Column Reorder" to true. Thats a good place to start.

yeah but it enables at run time. how to do that using code?

an idea came to my mind but i dont think it is quite smart.
i can remove the items and get the reference of the removed item and then add the item using AddAt method. i will try that.

how to reorder columns in listview?

Thanks

scissors+glue

i am flabergasted :(
you used to be a hardworker when it came to posting some useful code snippets to my questions :(

I will, I promise :P

It is a Friday evening and i'm having a few drinks before I call it a night. I will take a look at this in the morning. Take the night off! :)

ok that is the deal, then i will increase your reputation points by 5 as well as one more solved threads :D

Hey now... you slacked off on that last 10 day one. I think you made it 3 days :(

commented: day 5 +5

ok i add one more now :)

Sorry buddy.. you have me on this. I have never used a ListView and i'm not seeing a way to about this :(

commented: day 6 +5

i found a way, i will post it when i am done. dont worry.

here is the solution :

Form1.cs :

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace ProgrammaticallyReorderListview
{
	public partial class Form1 : Form
	{
		public Form1()
		{
			InitializeComponent();
		}
		ColumnHeader ch1;
		private void Form1_Load(object sender, EventArgs e)
		{
			ListViewItem item1 = listView1.Items.Add("item1");
			item1.SubItems.Add("subitem1");
			item1.SubItems.Add("subitem2");
			ListViewItem item2 = listView1.Items.Add("item2");
			item2.SubItems.Add("subitem1");
			item2.SubItems.Add("subitem2");

			ch1 = listView1.Columns.Add("column1");
			listView1.Columns.Add("column2");
			listView1.Columns.Add("column3");
		}
		private int columnIndex = 1;
		private void button1_Click(object sender, EventArgs e)
		{
			columnIndex = columnIndex % 3;
			listView1.Columns.Remove(ch1);
			listView1.Columns.Insert(columnIndex, ch1);
			columnIndex++;
		}
	}
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.