Sorting & Grouping in a Repeater - asp.net

In ASP.NET Can we achieve both grouping and sorting in a repeater control.
If possible please suggest me the way of doing it or links ..
I have to implement that in my application
Thank You.

If you mean visual grouping, the way I have done this sort of thing in the past is to add an ItemDataBound event to the repeater which checks the previous item using MyRepeater.Items[e.Item.ItemIndex-1]. If I've determined that a new group has started (i.e., if the previous item started with a different letter than the current item, and I am sorting alphabetically and grouping by letter) then I inject the appropriate HTML markup into a Literal control in the ItemTemplate to create the visual groupings.

Couldn't you use the ListView control instead of a repeater? This has built in support for sorting and a kind of grouping whilst still offering the layout flexibility you get with a Repeater.

Related

Programmatically select/use ItemTemplate or AlternatingItemTemplate

I have a ASP.NET repeater, and i want to make use of it's ItemTemplate and AlternatingItemTemplate slightly different as how they are supposed to be used. Instead of having ASP.NET to switch between the 2 for every odd/even item, i want to choose the right template in the ItemDataBound event.
Does anyone know whether this is possible?
I saw that which template is being applied, comes from which ItemType the row/item has. So if anyone knows how to manipulate that value, that would be great as well.
Turns out that this is really not possible. The only option you have here, is to write your own version that inherits from ASP.NET repeater. Then, of course, you can do/overrule whatever you like to do with your templates.

Data Web Controls

In a repeater control can we achive both sorting and grouping together, If possiple plz guide me the way.
If not, what is the best control to obtain just sorting and grouping.
Thank You
There is no built-in data control with grouping and sorting which works out of the box in .net framework. You need to implement sorting and grouping by yourself. As implemented solutions you can look at devexpress gridview or jquery grid plugin.
If you want to implement sorting in repeater control - look at this.

ASP.NET three-level databinding

I have custom objects I am binding to using ObjectDataSource. I have three-level binding: a DropDownList (Department) that filters the next DropDownList (Category) that filters a GridView (Questions). Each ObjectDataSource binds to the previous control's SelectedValue (except the first one, of course).
Everything works fine only to the next level (Department to Category and Category to Questions). When I change the Department, the Category list gets updated correctly, but the Questions that show up are from the previously selected category.
How can I get this three-level binding to work correctly? I can't figure out whether I am missing something. If I have to, I could implement SelectedIndexChanged on the first list and manually force the update on the grid, but this is not ideal. Thanks for your help!
A bit more info: I don't have a default "select an item" option. That means that when I change the Department, the first Category is automatically selected. I was hoping the binding would be smart enough to trickle that all the way down. It was smart enough that I didn't have to do the if (!IsPostBack) { // Load data }.
I currently have implemented Department_SelectedIndexChanged() and simply done a Questions.DataSource = Questions.DataSource;. That seems to "refresh" everything properly. Is there a better way to do this?
Could you clear the grid instead until the second value is refreshed? Are you looking for an AjAX appraoch, or does this use postbacks?
There isn't an automatic solution that I'm aware of so you would need to do something like you mentioned because how else would the page know to refresh the grid?

Displaying hierarchical data from database in unordered list via Repeater control

I have a database table with some Id, parentId and other data-related fields.
I want to display all this in nested <ul>s (n-levels of nesting) using repeater control.
(I don't want to use any other 3-rd party controls and also no treeviews...)
First, i read the data (once) from database into a collection (List<myObject>) on which I would like to manipulate to get the hierarchical structure.
The problem is how to build repeater's template?
Repeater's template is static, so the only thing that i can prepare is the starting and ending <ul> and </ul>.
How to be with nested uls? how to create them?
Please suggest.
You could nest more repeater controls into the item template, and bind them to a subset of your data.
Alternately, I would suggest programatically building your item template.
You're not going to be able to do this with a standard repeater. You could nest repeaters, but that'd take some programatic tweaking to build your hierarchy. An alternative would be to use the TreeView control. If you don't like the built-in TreeView and you want cleaner markup, Asp.Net MVC sounds like it'd be a better fit. If you want a more middle-ground solution, Asp.Net's CSS Friendly Control Adapters may work for better you.

Why not we use Repeater Control instead of Gridview Control?

I know GridView control comes with lot of built in functionality, which we can achieve from repeater control. GridView control has performance issues. Why don't we use repeater?
You will be thinking why this question, if you can achieve the functionality and performance using repeater use it, but I want to understand why and when we should use repeater and GridView. Can anybody explain me how and when?
GridView supports a tablular style of layout. So works nicely for displaying data that would fit into a table. e.g. report style data
Repeater control is good for a more free style layout. Say for displaying products on an ecommerce website or for displaying entries on a forum or blog.
Like you said, the Repeater can perform certain aspects of the GridView. In this case, you'd want to use a Repeater. However, there are differences between the controls which cannot easily be substituted (or, necessarily, worth the time to implement). You can see a table of differences here. Knowing these differences can make it easier to decide what control to use depending on your needs.
(From Link)
The GridView : it supports paging but
it doesn't provide a flexible layout ,
since its mainly used to display the
data in a table based layout.And If we
looked at data inserting , the
Gridview doesn't have a built in
support for inserting data( since it
doesn't call the insert method of it
underlying data source when you click
on a button with a CommadName set to
"Insert" ).
The Repeater control : you will find
that it provides a flexible layout but
it doesn't support data grouping
,inserting,deleting , updating and
paging through the data .
The GridView is for tabular data only and does a lot of the work for you, like binding data automatically to columns.
The Repeater gives you more control over the result, but you have to do more because nothing gets binded automatically.
I prefer using a Repeater almost every time, but I can see the usefulness of the GridView.

Resources