Vexorei Vexorei - 1 year ago 138
C# Question

C# Listview column tag

I need to get the sender tag from the colums in a ListView, how can I do this?

I mean something like this:

private void listView1_ColumnClick(object sender, ColumnClickEventArgs e)
{
if((string)(sender as ColumnHeader).Tag == "...")
{
/*mycode*/
}
}

Answer Source

The sender of the event is ListView. You should use e.Column which is the index of clicked column and find the column object, then get the value from tag

private void listView1_ColumnClick(object sender, ColumnClickEventArgs e)
{
    var column = listView1.Columns[e.Column];
    var tag = column.Tag as string;
    if(tag == "something")
    {
        //...
    }
}