ListView ColumnHeader.Name is empty string
Asked Answered
D

3

10

I've created a WinForms ListView as Detail view with four columns. I've given each column a name in the designer, however when accessing each ColumnHeader via the ListView.Columns property, I find each ColumnHeader.Name is an empty string. Am I doing something wrong or is this a framework bug?

Darvon answered 7/11, 2010 at 23:55 Comment(0)
G
9

I'm able to recreate the same behaviour. I think it must definitely be a bug as it's implied that the value will be set correctly by the designer.

As a workaround, you could put the name into the Tag property too. (Or set it programmatically in the constructor, but that won't work well if you need to add a column in the designer later on. I would then rather go with not using the designer at all to initialize the columns.)

I found some discussion on this here - looks like this is a known issue, they are also going with the Tag hack.

Groom answered 8/11, 2010 at 0:23 Comment(1)
The "FixColumnNames" function hack is working fine.Surprisingly(?) It's not fixed in .NET 4.0.Stomatic
L
0

here is a code i took it from Here and make some modifications

   private  static List<string> GetColumnNames(ListView list)
    {
        List<ColumnHeader> columns = new List<ColumnHeader>();
        List<string> ColumnsNames = new List<string>();
        foreach (ColumnHeader column in list.Columns)
        {
            if (string.IsNullOrEmpty(column.Name))
            {
                columns.Add(column);
            }
        }

        if (columns.Count == 0)
        {
            // no need to fix names 
            return null;
        }

        Control parent = list.Parent;

        while (parent != null)
        {
            //if listview is public use this:
            //FieldInfo listInfo = parent.GetType().GetField(list.Name, BindingFlags.Public | BindingFlags.Instance);
            //or if listview is private use this
            FieldInfo listInfo = parent.GetType().GetField(list.Name, BindingFlags.NonPublic | BindingFlags.Instance);

            if (listInfo != null)
            {
                // found a member with given name, let's check if it points to the same object 
                if (object.ReferenceEquals(listInfo.GetValue(parent), list))
                {
                    // yes, this member points to our object 
                    break;
                }
            }

            parent = parent.Parent;
        }

        if (parent != null)
        {
           
            foreach (ColumnHeader column in columns)
            {
                FieldInfo columnInfo = null;

                // for all fields 
               
                foreach (FieldInfo field in parent.GetType().GetFields(BindingFlags.NonPublic | BindingFlags.Instance))
                {
                    // ...find the one pointing to column we're looking for 
                    if ((field.FieldType == column.GetType()) && (object.ReferenceEquals(field.GetValue(parent), column)))
                    {
                        // same type, same value -> yes, this is the member we're looking for 
                        columnInfo = field;
                        break;
                    }
                }

                if (columnInfo != null)
                {
                    try
                    {
                      
                            ColumnsNames.Add(columnInfo.Name);
                        
                       // MessageBox.Show(columnInfo.Name);
                        // column.Name = columnInfo.Name;
                    }
                    catch
                    {
                        // No idea why, but we don't want any exceptions... 
                    }
                }
                else
                {
                    //MessageBox.Show("no member found ");
                    // no member found 
                }
            }
        }
        else
        {
            return null;
            // no parent found 
        }
        return ColumnsNames;
    }
Leith answered 17/11, 2022 at 6:55 Comment(0)
E
-1

I had the same problem, but worked around it like this:

foreach (ColumnHeader CN in listView1.Columns)
{
    //I added listbox for this example, but my code uses it
    //to populate an excell spreadsheet

    listBox1.Items.Add(CN.Text.ToString());

}

Hope you can use this in some way.

Ebro answered 9/8, 2012 at 8:1 Comment(1)
Late answere but, OP is looking for Name (elements name its self) and not Text (whats actualy only a caption).Session

© 2022 - 2024 — McMap. All rights reserved.