简体   繁体   中英

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. 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.

只需使用:

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

确定了。

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

You can use the String.Split method:

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

Then you can use the returned value as the DataSource :

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

Use following code:

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

Hope its helpful.

Use String.Split() method;

Returns a string array that contains the substrings in this instance that are delimited by elements of a specified Unicode character array.

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

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

Here is a 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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