views:

276

answers:

3

I have a relatively simple Listview that suddenly needs (due to new requirements) to have it's 'layout' extracted to a DataTable so that a common routine can convert it to an Excel spreadsheet for export purposes.

The ItemTemplate is just a series of Table Rows with some text, data-bound labels and textboxes with validators in the cells.

Usually, when trying to pull out a particular value (like what was entered into a text box), I use the ListViewItem's .FindControl method.

For Each objItem As ListViewItem In lvwOptions.Items
    Dim objTextHrsLabor As TextBox = CType(objItem.FindControl("txtHrsOptByLabor"), TextBox)
    decHours = CDec(objTextHrsLabor.Text)
Next

In this case, however, I'm trying to take all the data displayed - all the 'rows and columns' of the table that was created.

Inside the ForEach / Next loop of ListViewItems, I started a ForEach/Next loop of Controls for each instance's controls but I got some really strange results returned (like controls that had a couple of table cells in them).

I get the sense I'm headed in the wrong direction. All I want is for the nicely-formatted 5-line, 6 column table to be converted to a 5-line, 6-column data table.

Is there another avenue I should be looking at?

A: 

I would look at the underlying data source for your ListView.

The data source must be a collection or an IEnumerable and you should be able to iterate through it to build your data table.

If you know that all elements are of the same type then you can use the first element and look at its properties using reflection to determine which columns your table should contain. Then you can add DataRows to your table and fill in the columns using the property names.

This will probably be faster than iterating through the generated html of the ListView.

Rune Grimstad
I was hoping to avoid having to add all the 'labels'. I basically have 3 Listviews, one in each tab of a TabContainer, displaying different parts of the same List(Of MyBusinessObject). The desired goal is to take what they see on the screen and 'export' it to Excel. We have routines to convert a datable to Excel so I was hoping to make a datatable from the 'table' displayed in the ListView.
David
Hmm. You have defined custom headers in your aspx-markup? You will have to examine the control hierarchy or the generated html for those I think. But once you have them you should still be able to step through the data source to get the row data. In the wors case you could step through the Items collection and look at the ListViewDataItem's there, but that will surely be messy.
Rune Grimstad
It's becoming clear that I have to use Labels for the column and row-header text. Then I have to iterate through the .Controls in each ListView.Item. A Case statement branching on the Control.ToString value allows me to DirectCast into Labels and TextBoxes. I can now tell the end-of-row if the Object's type is "Web.UI.Webcontrols.LiteralControl" and, once CAST, scan the LiteralControl.Text property for "</tr>". Ugly, but it looks like it'll work.
David
Oh man! Just wait a year when you have to support localized dates! Or arbitrary precision floating point numbers. :-) Good luck!
Rune Grimstad
A: 

Hey,

I used this approach for exporting a ListView to Excel: http://aspalliance.com/771_CodeSnip_Exporting_GridView_to_Excel.

I know it deals with a GridView, but I adapted it to a ListView (as long as the underlying structure is a table) and it worked fine for me.

HTH.

Brian
Unfortunately, the underlying code is a generic List collection.
David
Works the same way; just bind the list, and that will work too.
Brian
A: 

you can use.. listView1.Items[0].SubItems[0].Text

this will be helpful , really simple and easy . You can extract info right on the basis of index & use anyway you want.

Depinder Bharti