列表视图中的多行文本

时间:2014-02-28 19:06:09

标签: c# winforms listview

我尝试将包含ListView的winform作为详细信息(ListView1.View =“Details”) 这个ListView有2个SubItems,我需要Wrap String并把它放到SubItem。

我不能使用由其他Like TableXP或...

创建的任何组件或用户控件

我使用此代码:

lstShares.Columns.Add("Share Name",100);
lstShares.Columns.Add("Path",300);
lstShares.View = View.Details;
ManagementObjectSearcher shares = new ManagementObjectSearcher("Select * from Win32_Share");
foreach (ManagementObject share in shares.Get())
{
    lstShares.Items.Add(new ListViewItem(new String[] { share["Name"].ToString(), share["Path"].ToString() + "\n" + "AAAA" }));
}

如果我使用“\ n”或Environment.NewLine,则不会发生任何变化,如下图所示 enter image description here

有人有想法吗? TNX。

3 个答案:

答案 0 :(得分:6)

请考虑使用DataGridView控件。它支持包装:

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

结果:

enter image description here

答案 1 :(得分:0)

你可以实现类似的东西

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

答案 2 :(得分:-2)

您可以使用'\ n'字符包装到listitem文本的下一行,例如:

listView1.Items.Add("apples\noranges\nbananas");

listView1.Items.Add("kiwis\ngrapefruits\nwatermelons");