0

I've searched thoroughly before asking, I'd like to find out how to take some text from a listview and pass it into a string. Theres only one column in the listview and only one row. The value in it is an integer.

The closest I came was this:

teststring = ListView1.Items.ToString

but that just filled the string with "System.Collections.Generic.List`1[System.Web.UI.WebControls.ListViewDataItem]"

Update: I'm working under ASP.NET

user2247353
  • 37
  • 1
  • 6
  • Yes I have tried it. Thank you for the suggestion. I commented below what happened. – user2247353 Apr 10 '13 at 07:59
  • @SATSON I am using a linqdatasource to get an integer from an sql table, I didn't code anything. – user2247353 Apr 10 '13 at 08:09
  • it is totally empty until a button press, then there is one integer and nothing else – user2247353 Apr 10 '13 at 09:42
  • listview show anything in run time. then select value and get the index in selection changed and use that index for get value – Sathish Apr 10 '13 at 10:27
  • i'm not sure i follow? – user2247353 Apr 10 '13 at 10:34
  • Try it. because listview.item(0) is index out of range means i think listview does have items so first check which value passing in selection changed event then use that for get value. just try it may be helpful. All is Well – Sathish Apr 10 '13 at 10:43
  • I will try it, but I didn't understand what you meant, but now I think I do. I think you are right. I've been looking at it for a long time now and I agree that there must be no data in the listview. I'm getting data from an SQL table when a button is pressed, and its the same button that tries to read the gridview. I think I need to implement the gridview on another page using the suggestions that have been made here. I will update with results and confirm which Answer worked – user2247353 Apr 10 '13 at 10:59
  • listview. i have tried both though, and they both seem to have nothing in them. – user2247353 Apr 10 '13 at 11:11
  • if you want gridview then try [link](http://stackoverflow.com/questions/3670549/binding-linqdatasource-from-code-behind-to-gridview) and get value using follwing code `GridView1.Rows(0).Cells(0).Text` – Sathish Apr 10 '13 at 11:35
  • After taking the data to another page, doing a linqdatasource and gridview there, the code: labelText = GridView1.Rows(0).Cells(0).Text successfully works. However the code labelText = ListView1.DataMember(1) did not. – user2247353 Apr 10 '13 at 12:43
  • i knew that already. anyway happy coding. check my previus code `listview.items(0).ToString` :) – Sathish Apr 10 '13 at 13:08
  • Thanks alot for your help. I can't mark a right answer because the listview never worked. I'm using gridview instead. – user2247353 Apr 10 '13 at 13:37

2 Answers2

0

Use this,

teststring =  ListView1.Items(ListView1.Items.Count - 1)

Instead of

teststring = ListView1.Items.ToString 

Because your attempt will get the ListViewItemCollection (Non-Technically: group of items), But actually your need is to get a single item from that group. If you need further clarifications, Please refer this.

Update,

 teststring =  Ctype(ListView1.Items(ListView1.Items.Count - 1),Control).text
Rajaprabhu Aravindasamy
  • 66,513
  • 17
  • 101
  • 130
  • Thankyou. The code was underlined in blue and said cannot be converted to string, so I amended it like this: testtext = (ListView1.Items(ListView1.Items.Count - 1)).ToString It now passes this string into testtext "System.Web.UI.WebControls.ListViewDataItem" – user2247353 Apr 10 '13 at 07:57
  • @user2247353 Oh, ok just try my new update, and commnent back if it isworking for you :) – Rajaprabhu Aravindasamy Apr 10 '13 at 08:08
  • It is underlined blue and says 'text' is not a member of 'System.Web.UI.Control'. Tried loading the page anyway but it showed an error with the line highlighted. I am using visual studio 2010 and vb. Do I need to import something for it to work? – user2247353 Apr 10 '13 at 08:13
0

Try this:

teststring = ListView1.Items(0).Text
SysDragon
  • 9,692
  • 15
  • 60
  • 89
  • It gives compilation error 'Text' is not a member of 'System.Web.UI.WebControls.ListViewDataItem'. Its like I'm missing an Import statement or something. I am using visual studio 2010 and vb – user2247353 Apr 10 '13 at 08:51
  • 1
    Nah, this works for standart winforms ListView, but you must be working under ASP.NET without telling us. – SysDragon Apr 10 '13 at 08:55