C# ListView Column Width Auto

后端 未结 10 1138
傲寒
傲寒 2020-11-28 07:04

How can I set the column width of a c# winforms listview control to auto. Something like width = -1 / -2 ?

10条回答
  •  爱一瞬间的悲伤
    2020-11-28 08:03

    This solution will first resize the columns based on column data, if the resized width is smaller than header size, it will resize columns to at least fit the header. This is a pretty ugly solution, but it works.

    lstContacts.AutoResizeColumns(ColumnHeaderAutoResizeStyle.ColumnContent);
    colFirstName.Width = (colFirstName.Width < 60 ? 60 : colFirstName.Width);
    colLastName.Width = (colLastName.Width < 61 ? 61 : colLastName.Width);
    colPhoneNumber.Width = (colPhoneNumber.Width < 81 ? 81 : colPhoneNumber.Width);
    colEmail.Width = (colEmail.Width < 40 ? 40 : colEmail.Width);
    

    lstContacts is the ListView. colFirstName is a column, where 60 is the width required to fit the title. Etc.

提交回复
热议问题