Forms Data Controls :: Gridview Sort Expression - How To Reset A Gridview Or Prevent It From Doing A Sort
Sep 23, 2010
Just spent about 8 hours googling and looking through this forum for a solution on how to make sure that I can dynamically sort. Here is the situation.
I have 1 Gridview that displays 10 different scenarios based on what button is clicked.
I am also returning only top 10 records. I am doing all of the data binding pragmatically. So I have BoundFields, HyperLinkFields etc.
I also want to sort some records. When I change DataSource SQL statement the gridview somehow remembers what the last sort value was and it errors out saying that value "x" cannot be sorted because it does not exists.
Here I get an error that says that the data source does not support sorting? Doesnt it say gridview1.allowsorting = false;
I also tried gridview1.sort("", SortDirection.Ascending); This does nothin... except query my database one more time because i have a onSorting event that looks like this:
[Code]....
Here is an example of just one of those SLQ statements inside GetSQLQuery:
and I would like to modify my sort expression so that when the expirydate header is clicked on, it will make all those id that has expired pop to the top . In this case, we will have the following as the results
My program running good. But i try to add up-down icon on my gridView header. but if i make it, My break point should drop below "if comparison" For example; field.SortExpression=Category but every time CustomersGridView.SortExpression is empty when gridview.SortExp is not empty.
foreach (DataControlField field in CustomersGridView.Columns) { if (field.SortExpression == CustomersGridView.SortExpression) { return CustomersGridView.Columns.IndexOf(field); } } i need : foreach (DataControlField field in CustomersGridView.Columns) { if (field.SortExpression == "Category") { return 2; } } CustomersGridView.SortExpression must not be empty!!!!! MY source: <head runat="server"> <title></title> <link type="text/css" href="StyleSheet.css" rel="stylesheet" /> </head> <body> <form id="form1" runat="server"> <div> <asp:ScriptManager ID="ScriptManager1" runat="server"> </asp:ScriptManager> <asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional"> <ContentTemplate> <asp:GridView ID="gvCustomers" runat="server" CssClass="tablestyle" AllowSorting="true" OnRowDataBound="GvCustomers_RowDataBound" AutoGenerateColumns="false"> <AlternatingRowStyle CssClass="altrowstyle" /> <HeaderStyle CssClass="headerstyle" /> <RowStyle CssClass="rowstyle" /> <Columns> <asp:BoundField HeaderText="Kategori" DataField="Category" SortExpression="Category" /> <asp:BoundField HeaderText="Tarih" DataField="Date" SortExpression="Date" /> </Columns> </asp:GridView> </ContentTemplate> </asp:UpdatePanel> protected void GvCustomers_RowDataBound(object sender, GridViewRowEventArgs e) { GridView gridView = (GridView)sender; if (gridView.SortExpression.Length > 0) { int cellIndex = -1; foreach (DataControlField field in gridView.Columns) { if (field.SortExpression == gvCustomers.SortExpression) { cellIndex = gridView.Columns.IndexOf(field); break; } } if (cellIndex > -1) { if (e.Row.RowType == DataControlRowType.Header) { // this is a header row, // set the sort style e.Row.Cells[cellIndex].CssClass += (gridView.SortDirection == SortDirection.Ascending ? " sortascheader" : " sortdescheader"); } else if (e.Row.RowType == DataControlRowType.DataRow) { // this is an alternating row e.Row.Cells[cellIndex].CssClass += (e.Row.RowIndex % 2 == 0 ? " sortaltrow" : " sortrow"); } } } }
I was given help in a previous topic located here: Accessing Gridview In Another Form In this topic I had gridview rows that are selectable and a search window that returned search results. We found a way for me to select a row in the parent window of the search one. My issue now is that when the parent windows gridview is sorted, the function no longer selects the correct row. I was wondering if there was any way to access the gridviews sort expression and direction, or some other way I can do this so it accounts for sorting. Relavent code is as follow: This is in my parent window
<script type = "text/javascript"> function selectRow(index) { //Retrieve a reference to the table that represents the GridView var gridView = document.getElementById('<%=gvSideList.ClientID%>'); if(gridView) { //If able to get a reference to the table representing the GridView... var doPostbackArgument='Select$'+index; __doPostBack('<%=gvSideList.ClientID%>',doPostbackArgument); } }
This is in the child window
<script type = "text/javascript" > function selecttherow(index) { window.opener.selectRow(index); } </script> protected void gvresults_SelectedIndexChanged(object sender, EventArgs e) { //declare variables GridViewRow row; int index; int id; //retrieve index, row, and id from gvresults index = gvresults.SelectedIndex; row = gvresults.Rows[index]; id = Convert.ToInt32(row.Cells[5].Text); //fill a datatable from general with id's for selection SqlCommand cmd = new SqlCommand("SELECT ID FROM General", csbg); cmd.Parameters.AddWithValue("@id", id); SqlDataAdapter da = new SqlDataAdapter(cmd); DataTable dt = new DataTable(); da.Fill(dt); //loop through the dt and find the index of the matching id from the search window for (int i = 0; i < dt.Rows.Count; i++) { if (dt.Rows[i]["ID"].Equals(id)) { //execute script to select the matching row in the dataentry page string script = ""; script += "selecttherow("; script += i; script += ");"; ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "selecting", script, true); break; } } }
Let me know if you need me to further clarify anything.
I have a gridview with data. I want a button which is not part of the gridview. Then if I press a button the gridview is sorted by two colums. i.e column1 ascending column2 descending.
I am not looking to work with a Sqldatasource as I use a data access layer. So the sort would have to be from a datatable and databind or directly with the gridview.
I want to create simple rating page where player's place,name and rating is displayed. I've created database with ID,Name and Rating Columns, binded Gridview to this database and created TemplateField "Place". With following code I've created numbered list for Place:
protected void Page_Load(object sender, EventArgs e){ for (int i = 0; i < GridView1.Rows.Count; i++)
I'm a newbie to .NET, so the solution might be really trivial. I have a database, I have an Entity Data Model, I add in my Index.aspx page an EntitryDataSource and a GridView. I connect them and everything works fine (the data is displayed as expected). The problem is that clicking on the coulmn name or on the pagination lists doesnt do a thing... Although I did set the properties AllowSorting="True" and AllowPaging="True". I also tried with another datasource type (SqlDataSource) and the same problem.
i am having a gridview in which all columns get dynamically added, i applied sorting in it, all column added as boundcolumn get sort properly but two column which are added as templated column do'nt get sort. Though my dataview have these column as it is.
I am using gridview created by visual components. I have registered gridview_onsortclick event during the page initialisation, but when i try to click on the header of any column, this event does not get fired at that time. This event gets fired only after all the controls, sorting query is generated, filling the current dataset is done. But the same event is getting fired before query is generated and dataset is filled,for the other page. And one more thing to add with it, I want the sort function to be done on the whole dataset regardless of current page. Why is there such a difference in both the pages?
Is there a way to Sort a Gridview by a Calculated Column? I have a gridview with template fields using lablels and a linq datasource. Those columns sort just fine. I can not get the Calculated column to sort, because there is no sort expression to use...
I'm using asp.net 2 and would like to add sort direction arrows to GridView column headers.Please could someone point me to a soluton, VB.Net if possible.
Tried several different ways to do this but am just not seeing it. Proably a quick solution for someone more skilled than me.
I have a gridview generated from a datatable. One of the columns is "client name" whose data is generated from a class.
I added a link button for the column header that fires a sub that should sort the datagrid, but is not.
Here is the code I have so far that creates the gridview which works fine:
Private Sub PopulateCaseListGrid() 'Kill the existing DataSource, in case of a page reload gvCaseList.DataSource = Nothing 'Create a datatable with whatever column you want to use
I have a GridView and Formview attached to the same ObjectDataSource.
Everything is working ok so far.
If I enter new search criteria and click "Search", both the GridView and FormView rebind properly to the ObjectDataSource results. And I am able to page through the FormView records and they match the GridView records above.
When I click a sort link in the GridView, the ObjectDataSource returns the correct results based on the header link's commandName of 'Sort' and the CommandArg of 'SortExpression'.
The only thing wrong now is on the sort, the formview records do not reflect the reversing of the sorted record order. If I page through the FormView the records remain in the same order.
Why does the FormView not recognize the ObjectDataSource's changed results on the sort post back (even if it is only the sortExpression parameter of my ODS select method).
How do I get the FormView to rebind to the new ODS result set? I have called formview.DataBind() from every event I can think of. That does not seem to work either.