1

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.

Igor Ševo
  • 5,459
  • 3
  • 35
  • 80
CoreLean
  • 2,139
  • 5
  • 24
  • 43
  • Split on the comma, store in a collection, set the datasource of datalist/view to the collection, call databind on datalist/view. – DGibbs Apr 08 '13 at 10:13
  • Here's how to add items to a Listview (first search result): http://stackoverflow.com/questions/9951704/add-item-to-listview-control – jalgames Apr 08 '13 at 10:21
  • var myresult = mycomastring.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries); – JSJ Apr 08 '13 at 10:23
  • Ok i have updated my question. Please guide me now. – CoreLean Apr 08 '13 at 10:27
  • see this http://stackoverflow.com/questions/4872943/how-to-merge-two-lists-using-linq – JSJ Apr 08 '13 at 10:36

6 Answers6

1

Simply use this:

myListView.DataSource = myString.Split(',');
Roy Dictus
  • 32,551
  • 8
  • 60
  • 76
  • I have updated my question. Please guide me on that. I am binding custom list and one property contains comma separated text. – CoreLean Apr 08 '13 at 11:12
1

Ok got the answer.

<%# Convert.ToString(Eval("ProductIntegraidents")).Replace(",","<br/>") %
CoreLean
  • 2,139
  • 5
  • 24
  • 43
0

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;
Erik Schierboom
  • 16,301
  • 10
  • 64
  • 81
0

Use following code:

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

Hope its helpful.

Freelancer
  • 9,008
  • 7
  • 42
  • 81
0

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.

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
0
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.

joell
  • 396
  • 6
  • 17