简体   繁体   English

在数据列表或列表视图中显示逗号分隔的字符串?

[英]Show comma-separated string in datalist or listview?

I have string which has data in comma-separated format. 我有包含以逗号分隔格式的数据的字符串。 like below. 像下面

xyz,abc,lmnk

Now I need to show these items in one of the column in listview or datalist but like below (vertically). 现在,我需要在列表视图或数据列表的某一列中显示这些项目,但必须如下所示(垂直)。

xyz
abc
lmnk

How to do? 怎么做? Not able to figure out. 无法弄清楚。

I have other columns to show in listview and my listview in bind with custom object. 我还有其他列要显示在listview中,而我的listview要与自定义对象绑定。 Like below. 像下面。

List<Product> p =GetProductlist();
        lvProduct.DataSource = p;

And now in my listview i am showing the content of list like 现在在我的列表视图中,我正在显示列表的内容,例如

<a> '<%#DataBinder.Eval(Container.DataItem,"ProductName")%>'</a>
                           <a> '<%#DataBinder.Eval(Container.DataItem,"ProductIntegraidents")%>'</a>

and here the ProuctInegraident contains the comma seprated text. 此处的ProuctInegraident包含逗号分隔的文本。

只需使用:

myListView.DataSource = myString.Split(',');

确定了。

<%# Convert.ToString(Eval("ProductIntegraidents")).Replace(",","<br/>") %

You can use the String.Split method: 您可以使用String.Split方法:

var str = "xyz,abc,lmnk";
var items = str.Split(new char[] { ',' });

Then you can use the returned value as the DataSource : 然后,您可以将返回值用作DataSource

var listBox = new ListBox();
listBox.DataSource = items;

Use following code: 使用以下代码:

ListView.Items.Add(myString.Split(',')); 

Hope its helpful. 希望对您有所帮助。

Use String.Split() method; 使用String.Split()方法;

Returns a string array that contains the substrings in this instance that are delimited by elements of a specified Unicode character array. 返回一个字符串数组,其中包含此实例中的子字符串,这些子字符串由指定的Unicode字符数组的元素定界。

var s = "xyz,abc,lmnk";
var i = s.Split(new char[] { ',' });

foreach(var z in i)
   Console.WriteLine(z);

Here is a DEMO . 这是一个DEMO

String str = "xyz,abc,lmnk";
String[] Splitted = str.Split(',');
foreach (var word in Splitted)
{
    if (!String.IsNullOrEmpty(word))
        listView1.Items.Add(word);
}

Something like that, use an ListViewItem if you want more 'control', for example adding subitems etc. 像这样的东西,如果您想要更多的“控制”,请使用ListViewItem,例如添加子项等。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM