Forms Data Controls :: Sorting In Data GridView Using C#?
Sep 3, 2010well i m making a project in asp .net c# i used data gridview which is bound at run time after many calculations... problem occured when i want to sort it.
View 8 Replieswell i m making a project in asp .net c# i used data gridview which is bound at run time after many calculations... problem occured when i want to sort it.
View 8 RepliesModerators Note: THIS ISSUE IS BIG FOR ME AND EVEN IF POSSIBLE, GET THE REPLY FROM THE DESIGNER OF GRIDVIEW. I have been looking for him for long. I am really fed up with reviewing a good lot of web pages on how we can possibly customize the gridview to enable sorting and paging. So many sites have listed out a lot of information and so many guidances. But the problem is that one works out fine and the other is a burden. I really feel bad about being given the job of customizing this kind of a gridview which has no user friendly approach to it. Also, this control is rendered without the pager links inside the <tfoot> tag. I have tried the Pear Pager in php. It is that good and easy to use and compared to that, the gridview in asp.net is the worst ever control i have ever tried so far.
1. i can use the images to indicate the sorting direction
2. I can have the custom pager like
[code]
<<Previous 1 2 3 .. 7 Next >>.
[/code]
When i click the next when i am viewing the page at 3 , the pager links should change as
[code]
<<Previous 2 3 4 .. 7 Next >>
[/code]
Kindly look into this type of requirement and firstly tell me whether this is possible with the gridview control. I would like this request even to be escalated to the designers of the gridview control also, so that Microsoft comes out with a reply THAT WORKS and not the kind of stuff like surfing through a lot of links and pages and finally wasting a lot of days precious time and still breaking the head with this useless control. I have spent a lot of time in searching for a perfect way. Not writing a code that is non-standard. I am really serious b'cos I have spent weeks in customizing this control. If I dont get a solid reply atleast now, I am going to generate all the output by HTML content by custom coding.
I just need implement something in order to sort data DESCENDING instead of the default ASCENDING.
I have a Gridview and I tried some code like this:
[Code]....
protected void gv_data_Sorting(object sender, GridViewSortEventArgs e)
Following Code is for Sorting GridView Formed With DataSetSource: ttp://www.highoncoding.com/Articles/176_Sorting_GridView_Manually_.aspxBut it is not displaying any output. There is no problem in sql connection. I am unable to trace the error,
[Code]....
aspx page
<asp:GridView ID="GridView1" runat="server" AllowSorting="True" OnSorting="GridView1_Sorting">
</asp:GridView>
Actually i am using procedures and calling them using linq.I have a gridview and there is no data source bound to it in aspx page.I am binding different procedures with gridview in code behind.I am feeling myself unable to sort the data.
here is my code on page load event
if (Request.QueryString["Id"] != null)
{
if (Request.QueryString["Id"].ToString() == "0")
{
var list = db.prcSelectTodayNewListing();
gvListing.DataSource = list;
gvListing.DataBind();
}
else if (Request.QueryString["Id"].ToString() == "")
{
var list = db.prcSelectListing();
gvListing.DataSource = list;
gvListing.DataBind();
}
else
{
var list = db.prcSelectListingByStatusId(Convert.ToInt32(Request.QueryString.Get("Id")));
gvListing.DataSource = list;
gvListing.DataBind();
}
}
I am using GridView to display data using BLL
Grid view in my aspx file:
[Code]....
I got a web page, it has a tabcontainer, within each tab, there will be a gridview, all the tabs and gridview are created at run-time, i have only one code, that I will loop through to create all those tabs and gridview. When I apply the sorting, and I click on the column name on any grid, it returned an error saying index was out of bound. BUT, when there is only ONE grid view created, it is working fine, I suspect it mixed up all the grid view's id and so on. But I have assigned different ID to gridview
View 8 RepliesI'd like to sort a gridview using linkbuttons situated outside the gridview. For instance :
[code]....
I have the gridview inside another gridview, i want to sort that both gridviews, I don't know how to achieve using asp.net 3.5 visual studio 2008 c# code behind. Following are my aspx page,
<div
id="divclm"
runat="server">
<asp:GridView
ID="claimgrid"
runat="server"
AutoGenerateColumns="False" AllowSorting="True"
Width="100%"
AllowPaging="True"
CssClass="gareahead"
EmptyDataText="No Records To Display"
OnRowDataBound="claimgrid_RowDataBound"
OnRowCreated="claimgrid_RowCreated"
GridLines="Both"
OnPageIndexChanging="claimgrid_PageIndexChanging"
OnSorting="claimgrid_Sorting"
CaptionAlign="Right">
<Columns>
<asp:HyperLinkField
Text="<img id='upimg' src='Styles/Images/appschg.gif'/>"
ItemStyle-Width="10px">
<ItemStyle
Width="10px"
/>
</asp:HyperLinkField>
<asp:BoundField
DataField="name"
HeaderText="Name"
SortExpression="name">
<HeaderStyle
HorizontalAlign="Center"
/>
<ItemStyle
HorizontalAlign="Left"
/>
</asp:BoundField>
<asp:BoundField
DataField="code"
HeaderText="Code"
SortExpression="code">
<HeaderStyle
HorizontalAlign="Center"
/>
<ItemStyle
HorizontalAlign="Left"
/>
</asp:BoundField>
<asp:TemplateField>
<ItemTemplate>
</td></tr>
<tr>
<td
colspan="6"><div
id="<%#Eval("code") %>"
style="display:none;position:relative">
<asp:GridView
ID="Gdclmline1"
runat="server"
AllowPaging="True"
AllowSorting="True"
AutoGenerateColumns="False"
OnSorting="Gdclmline1_Sorting" BorderColor="Green"
BorderStyle="Solid"
BorderWidth="1px"
OnRowDataBound="Gdclmline1_RowDataBound"
OnRowEditing="Gdclmline1_RowEditing" Font-Names="Verdana"
Font-Size="8pt"
Font-Strikeout="False"
OnSelectedIndexChanging="Gdclmline1_SelectedIndexChanging"
OnRowUpdating="Gdclmline1_RowUpdating" PageSize="1000"
RowStyle-Wrap="false"
Width="100%"
CssClass="gareahead"
EmptyDataText="No Records To Display"
GridLines="Both">
<PagerSettings
Mode="NextPreviousFirstLast"
NextPageText="Next"
Position="Top"
PreviousPageText="Prev"
/>
<Columns>
<asp:TemplateField
HeaderText="phone"
SortExpression="phone">
<HeaderStyle
HorizontalAlign="Center"
/>
<ItemStyle
HorizontalAlign="Left"
/>
<ItemTemplate>
<asp:Label
ID="lblphone"
runat="server"
Text='<%#Bind("phone") %>'> </asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField
HeaderText="address1"
SortExpression="address2">
<HeaderStyle
HorizontalAlign="Center"
/>
<ItemStyle
HorizontalAlign="Left"
/>
<ItemTemplate>
<asp:Label
ID="lbladdress1
runat="server"
Text='<%#Bind("address1") %>'> </asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<RowStyle
CssClass="gr"
Height="100px"
HorizontalAlign="Left"
Wrap="true"/>
<SelectedRowStyle
CssClass="gr"
/> <PagerStyle
CssClass="gareahead"
HorizontalAlign="Left"
Wrap="False"
/> <HeaderStyle
CssClass="ghead"
ForeColor="White"
Wrap="true"
/> </asp:GridView>
</div>
</td>
</tr>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<PagerSettings
Mode="NextPreviousFirstLast"
NextPageText="Next"
Position="Top"
PreviousPageText="Prev"
/>
<HeaderStyle
HorizontalAlign="Center"
VerticalAlign="Middle"
CssClass="ghead"
ForeColor="White"
/>
<RowStyle
CssClass="gr"
/>
<SelectedRowStyle
CssClass="gs"
/>
</asp:GridView>
</div>
that aspx page contains gridview that contain one more gridview inside in it.Now i want to sort out both the grid i.e)claimgrid and Gdclmline1 i want code in c#.
I'm trying to extend the GridView control to enable sorting and paging for any situation.
When using my control I am fetching data from a database and filling a DataSet with it, then binding the GridView upon every page load. My first question would be, is this the correct approach?
To sort the GridView I am overriding the OnSorting method which stores the sort expression and direction in the ViewState, then creates a DataView and utalises the Sort method to sort the underlying data. It then sets the Data Source to this DataView and rebinds the GridView.
Paging is handled by OnPageIndexChanging which simply sets the PageIndex property and again rebinds the GridView.
My problem is; when any control causes a postback my GridView is no longer sorted, presumably because it is persistently rebound. If I don't rebind it then the GridView is empty on postback since the data isn't automatically stored in the ViewState. I have considered saving the data source in the ViewState but I would assume that this is bad practice for large amounts of data? - also DataViews are not seralisable.
The only solution I can think of currently is to override OnDataBound and sort the data every time. This results in a double sort when paging triggers a postback which seems inefficient. Code illustration of this below,
[Code]....
I'm looking for the cleanest 'best practice' solution as this is a learning exercise more than anything else.
I'm trying to sort GridView but I have problem with converting DataSource to DataTable. Object of DataTable is empty. I don't know how to convert it.Do you have any ideas? Here is my code:
private void SortGridView(string sortExpression, string direction)
{
DataTable dt = GridViewDzialania.DataSource as DataTable;
DataView dv = new DataView(dt);
dv.Sort = sortExpression + direction;
GridViewDzialania.DataSource = dv;
GridViewDzialania.DataBind();
}
i have a grid view can any one tell how to apply sorting in it
View 2 RepliesI set Allowsorting="true" but sorting is not working in my GridView.Here is code of aspx page:
[Code]...
I have a single gridview and multple objectDataSources. Based on the user's input I bind the correct objectDataSource to the gridview in the OnClick handler when they click 'search'. ThegridView/ObjectDataSource have customPaging enabled and all that works, but I can't get the sorting to work. The sorting works when I have 1 ODS dedicated to 1 GV, but since it is set on the fly, no dice. It complains that I need to have a 'GridView1_Sorting' event. I think the answer lies in some how sorting the data inside of the ODS programmatically, but I don't know.
View 4 Repliesi have gridview with insert row option is possible to the new row alwayz come on 1st No row automatically ? and old rows always goes down ?
View 7 RepliesThere are few columns in Gridview control , i want them to be sorted according to the column which i have specified . .how to specify that . .code is shown below . .By default i want them to be sorted according to "ENTRYDATE" . .
View 5 RepliesI have a GridView with 2 buttons to move the record either up or down (changing the sort order). My problem is that on the line "
[Code]....
I want to sorting the gridview. I am using stored procedure currentlyHow can I put the sorting functions?
protected void populateItem()
{
string itemQuery = "usp_GetStockList";
[code]...
I have a gridview that can be sorted by a few of the columns.
How do I tell the gridview to go back to the unsorted mode. That is, go back to listing rows in the order they appear in the datasource.
It's ok if this will only work the next time a rebind is done on the grid. Currently it's maintaining the sort mode after a rebind. I don't want it to do that.
I have a GridView using LINQ and I need to do pagination and sorting.
View 2 Repliesi want to sort a gridview column which is a template field with a repeater control in it. this column has multiple links and i would like to sort using the first link. how can i achieve this.
here is my code:
<asp:TemplateField HeaderText="Groups">
I am binding DataView data to GridView and using paging and sorting mechanism of the GridView.
In case of sorting, I define the sort expression based on user's selection and assign the same to DataView and then bind the same to GridView.
Issue:
Everytime I sort the DataView, I see different set of rows on the same page on GridView. The issue is, the column which is used in sorting has many rows with same values. So everytime I sort DataView using sort expression and bind it to GridView, it displays different sets of rows and sorts randomly.
What should I do to make sure that it always display same rows? I cant change the sorting expression as it is selected by the user. Or only way is to append sort expression containing unique key internally ?
I need the consistent set of rows as I am operating on rows selected by users and make them editable and allow user to udate values and display the same.
how to set paging and sorting to true in gridview
[Code]....
I have a gridview being data bound by an objectdatasource which points to a BLL/DAL. I have paging and sorting enabled within the gridview. I have the fields configured in the look/fell I want from a templatefield. This templatefield has the sort expression (date) I want things to be sorted by. All this works as it should and I have no issues with it.But, the issue is that when I click on the header to sort asc/desc by date, I have to click on it twice for it switch to descending order. E.g. I hit the webpage and it lists all the information in the gridview, sorted by date, ascending order. If I want to sort by descending order, I have to click the date header twice in order for the gridview to sort the information in descending order; the clicks cause a post-back and the whole screen is refreshed.
View 2 RepliesI am having some difficulty enabling sorting on a gridview that has data in it that has come from an objectdatasource.
The AllowSorting="True" function doesnt work for ObjectDataSource so I need to find a work around...I am guessing that I will need to do something in code in order to get this off the ground.