NateShoffner
NateShoffner

Reputation: 16839

C# - Programatically click a ListView Column

Is there a way to programatically click on a ListView column just as if you would normally click on it?

Upvotes: 0

Views: 6488

Answers (3)

NateShoffner
NateShoffner

Reputation: 16839

Nevermind, just figured it out using this page.

        ColumnClickEventArgs args = new ColumnClickEventArgs(0);
        listView1_ColumnClick(this, args);

Upvotes: 0

dotalchemy
dotalchemy

Reputation: 2467

Can't you just raise the ColumnClick event and pass the column number in the ColumnClickEventArgs?

http://msdn.microsoft.com/en-us/library/system.windows.forms.listview.columnclick.aspx

http://msdn.microsoft.com/en-us/library/system.windows.forms.columnclickeventargs.aspx

Example... erm... not really, as I'm a good few hundred miles away from the nearest VS install and I'm not great at remembering syntax without it!

Sort of pseudocode attempt though...

//initialize your event arguments
    ColumnClickEventArgs eArgs = new ColumnClickEventArgs(columnindex);

//declare your method
private void myColumnClick(object o, ColumnClickEventArgs e)
{
//do your stuff
}

//call your method to do stuff
myColumnClick(ListView1, eArgs);

I mean, really all you're doing is actually passing the column number which was clicked along with the listview object through as if you were creating an event handler - you're just not actually creating an event handler, nor are you using the OnColumnClick event to fire it.

This might get shot down by someone, but AFAIK it should work...

Upvotes: 4

Bradley Smith
Bradley Smith

Reputation: 13611

The ListView control does absolutely nothing when you click on a column header; the only action taken would be performed in your own event handler anyway, so I would suggest simply calling your event handler (or factoring the common code out into a separate method, as would be best practice, and calling that whenever you want to simulate the clicking of a column header).

Upvotes: 1

Related Questions