Multiline Text In List View

前端 未结 3 1836
抹茶落季
抹茶落季 2021-01-14 03:03

I try to make a winform contains the ListView as Details (ListView1.View = \"Details\") This ListView has 2 SubItems and i need to Wrap String and put it to SubItem .

<
相关标签:
3条回答
  • 2021-01-14 03:08

    You can implement something like this

    ListView lv = new ListView();
    lv.Columns.Add("Header", 100);
    lv.Columns.Add("Details", 100);
    lv.Dock = DockStyle.Fill;
    lv.Items.Add(new ListViewItem(new string[] { "Sachin", "Some details" }));
    lv.Items.Add(new ListViewItem(new string[] { "Stats", "More details" }));
    lv.View = View.Details;
    Controls.Add(lv);
    
    0 讨论(0)
  • 2021-01-14 03:15

    You can wrap to the next line in a listitem's text using the '\n' character, e.g.:

    listView1.Items.Add("apples\noranges\nbananas");
    
    listView1.Items.Add("kiwis\ngrapefruits\nwatermelons");
    
    0 讨论(0)
  • 2021-01-14 03:32

    Consider using the DataGridView control instead. It supports wrapping:

    dgv.AutoGenerateColumns = false;
    dgv.RowHeadersVisible = false;
    dgv.MultiSelect = false;
    dgv.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
    dgv.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells;
    dgv.DefaultCellStyle.WrapMode = DataGridViewTriState.True;
    dgv.Columns.Add(new DataGridViewTextBoxColumn() {
      HeaderText = "Share Name",
      ReadOnly = true,
      AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill,
      FillWeight = 25
    });
    dgv.Columns.Add(new DataGridViewTextBoxColumn() {
      HeaderText = "Path",
      ReadOnly = true,
      AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill,
      FillWeight = 75
    });
    var shares = new ManagementObjectSearcher("Select * from Win32_Share");
    foreach (ManagementObject share in shares.Get()) {
      dgv.Rows.Add(new String[] { share["Name"].ToString(),
                                  share["Path"].ToString() + "\n" + "AAAA" });
    }
    

    Result:

    enter image description here

    0 讨论(0)
提交回复
热议问题