I have a repository that contains all of my LINQ queries for this project that I am working on. I am able to get the LINQ results to a DataTable and bind that to a gridview for displaying the data. Now I need to make the gridview sortable. I have set AllowSorting="true" and I have the OnSort event handled in a routine in the codebehind. The program makes it into the sorting routine just fine.
Is manual sort my only option or is there an easier way to sort the gridview in the OnSort handler?
If you bind the GridView to a DataSourceControl then the sorting is taken care of for you. Otherwise you're stuck with doing it manually.
I ended up solving this problem using this example from MSDN and attaching my own datatable, which was acquired through a LINQ Query in my repository.
Related
I have a GridView and a SqlDataSource. All binding done in aspx, works correctly and Select calls an SP with parameters.
Now I would like to access the bound data programmatically, in stuctured form, not parsing back it from the UI, using cells or .FindControl or other hacks. I also do not want to reselect the data using my SqlDataSource's Select.
I've debugged the code but can not find any structured data neither in my GridView variable neither in my SqlDataSource variable. Maybe I was browsing the wrong properties or a cast was missing.
Thx in advance
I don't think you can do that. When you bind the data from an SQL DataSource to a Gridview, it fetches the data to display and then that's it. It's gone.
When a GridView updates the data, it uses the key field ID and the text in the GridView to do it. There is no other DataTable or other structure behind the scenes.
I've read some articles online where they say you just need to set the "AllowSorting" property to true, while there are others that have code written in the Sorting event. So my question is- To sort a gridviwe in ASP.NET, do we need to write any code in the sorting event or does setting "AllowSorting" to true enable the sorting?
Also, will anything change if I set my datasource programmatically as opposed to setting it directly as a SqlDataSource in the .aspx file itself?
As requested, my comment as answer:
That depends on what you're using as DataSource. If you use a declarative datasource control like SqlDataSource or ObjectDataSource you're done, otherwise you need to write the code yourself.
I've recently accomplished what you're asking by using something along the following lines:
DataTable dt = YourOwnWayOfCreatingADataTable();
dt.DefaultView.Sort = "ColumnNameToSort"; // You can append ASC or DESC to this
gridView1.DataSource = dt;
gridView1.DataBind();
I hope that helps you out. Obviously, this is sorting the results before you display them, but you can sort again by changing the .Sort property and rebinding. If you need something more specific or multiple columns, this may not work.
I dynamically create my gridView from DataTable. Now when I add paging to it, I must Auto Generate Columns. As soon as I do that, the gridview data is duplicated.
1.5K+ records each 150 Columns duplicated doesn't exactly look quite right.
Anyone's got a clue on how to add paging that actually won't mess everything up?
PS: I fire up LoadGridView() event on Button_Click.
This is a very simple example on how it is done
so i know that there are built in datagrid functions to sort for bounded items. However, my datagrid is bound dynamically and I am wondering how I can get this sort function to work. I know I should be coding inside the Sorting event, and probably would have to use some ADO.net since that is what I am working with. Im not sure if any code would help but if you need any please ask. I just want to know which functions (if vs2010 has one) should I use to get column names and such. Thanks!
You can try sorting the DataTable, like so:
DataTable1.DefaultView.Sort = "SomeColumn ASC";
DataGrid1.DataSource = DataTable1.DefaultView.ToTable();
DataGrid1.DataBind();
I have confusion about GridView in ASP.NET.
How does the GridView exactly work?
I mean when we bind data to gridview with 100 records through GridView1.DataBind();
I have set Pageindexchanging Event and I set the Pagesize = "40" and AllowPaging="True" Then....
Now the interesting part is begins What happens when i Click on next page index of GridView is it. Once again go to the database and fetch the data.. or gridview creates its own dataset and fetch data from that dataset or anything different than this...
And one more thing is how the Sorting works in GridView?
The simple answer is yes, the GridView is simply a view placed over a DataGrid, and all the parameters specified affect how the GridView is rendered for the user. Since we're dealing with the stateless web, you will have to rebind the data.
One trick for this is that if your dataset isn't too large, you can store the DataTable in the user's session and simply retrieve it from there, saving a trip to the database. If you are dealing with a large amount of data, then you'll want to look into options for having your SQL queries function in a "paged" format so you only retrieve the rows you intend to display.
With Paging and Sorting, they serve as an event to respond to in which you resort your data and rebind to the grid for presentation.
The advantage of the GridView is that you do have a central object with a lot of functionality built in that you can use for rapid deployment. When you get a hand of how the sorting, paging, row commands, and other things work, you can do some really great things in a small amount of code.
It depends what your data source is, if your datasource supports paging, then it will fetch only the records it needs to show that page, if it does not, it will get all the records and discard the one you don't want.
For both examples, it will talk to the datasource everytime you change page.
See here for more details:
http://msdn.microsoft.com/en-us/library/5aw1xfh3.aspx