开发者

How can one extract/convert information from a Listview (to a DataTable)?

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-lin开发者_如何学编程e, 6 column table to be converted to a 5-line, 6-column data table.

Is there another avenue I should be looking at?


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.


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜