Looping data with a Repeater - asp.net

For an example of what I'm attempting to accomplish, see below. I need to loop through a data source, displaying a value from every datarow in its own tablecell. (table is 3 columns wide)
Normally I'd use a modulus in the code behind to determine if a table row should be ended and a new row started. But I swear I recall reading that the Repeater control has this capability built in.
Pseudocode/table:
Header: <table>
<tr><td>Bind("ProductID")</td><td>Bind("ProductID")</td><td>Bind("ProductID")</td></tr>
Footer: </table>
A simple example of the output is:
1 2 3
4 5 6
7 8 9
So what I'm wondering is there a "built in" way to accomplish this, or should I proceed with a mod check in the code behind.
Thanks
Kevin

The Repeater does not have it the ListView however does.
Edit I was actually thinking of the DataList (new name for the "old" ListView") as Canavar mentions, but the new ListView is so flexible I'd bet you can use it as well.

You can use DataList's RepeatColumns property.

Related

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

Bulk Editing of A GridView in ASP.Net

What is the best way to achieve bulk editing of a grid view in ASP.net? I don't want the user to have to click edit on each individual row to change it. It would be much better if they could make all the changes and then submit them in one go.
What I want is something like
Item1 x x x x
Item2 x x x x
Item3 x x x x
Submit
Where x a check box.
Is a grid view even a good way to go about achieving this?
Try this:
In you gridview create a template column with textboxs next fill all with databinder for get default data from database. put a bottom your page and in this bottom create a for cycle that load all gridview rows and search for template column you created. Fin the textbox a write this to dataset. Next you can use my code i posted in this thread for with one line save all dataset.
BulkEditGridview from RealWorldGrids does just what you want.
Here is a GridView that supports bulk editing: Link
I wonder if Matt Berseth´s post about bulk inserting (in a listview but it might help you) could be of some assistance, try it:
http://mattberseth.com/blog/2008/05/bulk_inserting_data_with_the_l.html
You should be able to set the GridViewRow.RowState to Edit during RowDataBound. Just be sure the RowType is DataRow.

Data Paging with specific layout

I am trying to use data paging (not custom data paging - just normal inefficient paging) for about max 125 records, over 5 pages, 25 records per page. But most samples out there seem to be using a gridview or a datagrid, however I want each record to look like this:
Record 1 Title
Record 1 Description
Record 1 Time: Record 1 Contact:
Record 2 Title
Record 2 Description
Record 2 Time: Record 2 Contact:
etc etc, however with grids they result is like a table with bound fields for each column so I can't display it like that using paging, I thought of using a Repeater however I think I will need to use viewstate for that as it doesn't have paging built in and don't want to use ViewState as I have disabled this - can anyone help?
I think what you are looking for is a DataView or a Repeater
The Repeater control is a basic
templated data-bound list. It has no
built-in layout or styles, so you must
explicitly declare all layout,
formatting, and style tags within the
control's templates.

ASP.NET, object data source 5+ rows returned - force repeater to only show one

Is there a way with ASP.NET to tell a data repeater which is working against a objectDataSource to only show the first row and not any others that are returned in the objectDataSource.
I can't limit the data source to one row as it's echo'ing to a grid above the repeater.
Forgive me if this is a stupid question!
Thanks in advance
C
in your repeater event handler you can use the event object to see what number the item in the collection you are on.
So you can say if that number > 0 or 1 or whatever it is, render, if not, do nothing.
It's been a really long time since I've used .NET, but I know you can lookup the index of the item in the ItemDataBound event.

Display rows in multiple columns in Asp.net Gridview

By default each row of a Gridview maps to each row in a datatable or dataset attached to its datasource. But what if I want to display these rows in multiple columns. For example if it has 10 rows, 5 rows each should be displayed in 2 columns side by side. Also can I do this with the Infragistics grid. Is this possible?
You can use a DataList control instead. It has a RepeatColumns property that you can define the number of columns you want to display.
In .NET Framework 3.5, there is an even better solution, the ListView control. You can find further information about how to use the ListView control here.
If this is a pure coding exercise, then bind to the RowDataBound event of the Gridview. That way, you can do:
e.Row.Cells(2).Text = e.Row.Cells(1).Text
This would place the text from column 1 in column 2 after it has been pulled from the database. You can also dynamically create columns using a similar method.
Re-reading, I think I misunderstand your problem though.
Can't you just put two identical bound columns one after the other?

Resources