开发者

ListView ColumnHeader.Name is empty string

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 ColumnHead开发者_Python百科er.Name is an empty string. Am I doing something wrong or is this a framework bug?


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.


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;
    }


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜