.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month
Gaurav Pal
Post New Web Links

How to sort # column in GridView (asp.net) ?

Posted By:      Posted Date: September 21, 2010    Points: 0   Category :ASP.Net

I have a GridView with a column that contains rows numbers. somethign like below :

1    |
2    |
3    |
.     |
.     |
.     |
11  |
12  |
13  |
.    |
.    |
.    |

I've added it to the GridView with the following code :

<asp:TemplateField HeaderText="#" SortExpression="#">
<%# Container.DataItemIndex + 1 %>

Now I wanna sort GridView with this column, I've done it by reading this thread. But when I sort the GridView by # column, it sorts the GridView like below :

1   |
11 |
12 |
.   |
.   |
.   |
21 |
22 |
23 |
.   |
.   |
.   |

How can I fix it

View Complete Post

More Related Resource Links

WPF: how to make a GridView only sort on one column as ascending order?


We use GridView inside the Listview. We need the grid view alwasy sort on one column ascending order and not allow to click column header to change it.

Is there a easy way to achieve it in Xaml?

If not possible, how to achieve it in code behind?



Tooltip for GridView Column Headers (ASP.Net)

I just wanted to add auto tooltips for my GridView headers. There are couple of ways but I don't want to use javascript and wanted to use simple 'title' attribute. It took couple of hours to get the working code supporting sortable columns. I thought it will be very useful for others. Here's the code and logic behind it.

How to sort GridView?

In this article I will discuss how to sort columns in grid and more. At the end it will help answer some very frequently asked questions like following

Use a column as parameter for a sds in another column; gridview


Hola, I have a gridview which is bound to a sds which returns 7 columns and I added two columns from the GUI.  What I want is to fill up the 9th column with DDLs and these  DDLS will get their data from a different sds which is using a stored procedure with a single parameter.  I created the 9th row, used template field, made the sds for 9th row, the stored_procedure, threw a DDL in Item template and all  I need now is to pass a parameter in the first column to the sds in the 9th column.  I can code this but i'm getting tired of aspx.cs :) and would like to do it from the GUI.

Rendering data with HTML tags in the DD Gridview for a selected column


I am having trouble finding out where and how to HTML Encode a cell's data on the Dynamic Data (v4.0) gridview of List.aspx.  As a simple case, suppose I have formatted cell data that is A<br/>B in the DB.    Obviously, I want A stacked on B in the cell.

It seems gridView1.HtmlEncode = true has gone away.

So maybe I'll try to catch it on the RowDataBound event:

protected void GridView1_RowDataBound(Object sender, GridViewRowEventArgs e)

            if (e.Row.RowType == DataControlRowType.DataRow)
                // Html Encode the cells


but this event never fires?

Has anyone figured out how to properly render HTML tags data in the List.aspx's GridView1? 

If I figure this out, then I can add a MetaAttribute called something like [EncodeAsHtml(true)] and be on my way.  Thanks!

Sort by gridview SortExpression parameter via Stored Procedure


I have a gridview that calls data via a stored procedure.  I am unable to enable the gridview columns to be sortable. I need to set the parameter in the Stored Procedure, can someone help me with this?

Here is my gridview:

<asp:GridView ID="AllUsersGrid" runat="server" AutoGenerateColumns="False" DataKeyNames="UserName"
                        GridLines="Vertical" Width="900px" DataSourceID="SqlDataSource1" AllowSorting="True"
                        SelectedRowStyle-Height="30px" CellPadding="4" BackColor="White">
                            <asp:TemplateField HeaderText="Full Name" SortExpression="lastname">
                                    <asp:Label ID="DisplayName" runat="server" Text='<%# Eval("firstname").ToString() & " " & Eval("lastname").ToString() %>' />
                            <asp:BoundField HeaderText="User Name" DataField="UserName" />

How To Access Datakeys In Javascript...and How To make gridview Column Visible true in javascript


How To Access Gridview's Datakeys In Javascript...and How To make gridview Column Visible true in javascript

Reply me..

sum of 7 column totals in footer of gridview

I have implemented column tolals in the footer of my gridview using the technicque described in this article http://aspalliance.com/782 .Now I would like to have a total of the 7 column totals that are displayed in the footer.  Can anyone tell me how to do this.  I would like to display this in a label or an addition row in the footer if that is possible.

listView Sort for a celltmplate column

hi , i have a list view with a column that is binded to a field in my database the field contains integers that represents the City_ID , and i a have another table that contains the cities and their ID's, so , when i apply the sort based on this column (City Column) the list View is sorted based on the integers not on the city names here is my XAML definition of that column : GridViewColumn Header="City_Name_ID" Width="75">                         <GridViewColumn.CellTemplate>                             <DataTemplate>                                 <ComboBox IsSynchronizedWithCurrentItem="False" ItemsSource="{Binding Source={StaticResource CitysViewSource}}" SelectedValue="{Binding Path=City_Name_ID}" SelectedValuePath="City_ID" DisplayMemberPath="City_Name"/>                      &nb

What is the meaning of Ascending\descending sort order of a column within an index

When I add a new index I can also specify the "sort order" as ascending or descending. What does it mean? does it affect performance? does it mean that the binary search tree will be set in a certain way?

How to set the width of column on GridView

How to set the specific the width of each column, the columns are created automaticly 

Use Data in a Column as Select Button in GridView

I don't want to use the built in "Select" hyperlink that's in the gridview, I'd like to just use the first data column, is there a way to do that or are we forced to use the automatically generated "Select" column.Thanks!

Up Down GridView Icon Image for Column Sorting

I'm trying to add an Ascending and Descending sort direction Icon Image next to the header text of my ASP.NET 2.0 Gridview Columns.I've searched and found some examples that seem to do this but have had difficult time recoding them from C# to VB or some just seemed overly complex for what I want to do.Does anyone have a simple VB solution for sort icons?Thanks,Ethan   

convert column into hyperlink in gridview

hi how can i convert column in grid view into hyperlink  thanks

extra column gridview

Dear All; I have a gridview with the following columns pid           Location I would like to add an extra column called quantity, that could be used to get a user input. How do i go about doing that? A sample code will be very helpful. See illustration below pid          Location              quantity   All help will be greatly appreciated. 

Sort a WPF DataGrid on a Customized Column

I have several wpf DataGrids that use something similar to: {     Binding Path=ContactID, Converter={StaticResource contactIDToNameConverter}}"       As a best concept for various ID fields in my database.  This is great when needing to display editable data that is linked by ID, and my converter handles the validation and cleanup if needed. Otherwise an SQL View, or Joined Table SQL would not be able to send updates directly to the database (DataSet Wizard).  For this DataGrid's Contact column (bound to the ContactID field from a ICollectionView; bound to a DataSet.ClientsToContacts DataTable), I have an underlying value of Integer, and a converted surface value of String.  I want to sort on the string. Using ICollectionView.View.SortDescription(path,direction) will only sort on the Integer, and I am desprately seeking work arounds.  Please help. jvj
ASP.NetWindows Application  .NET Framework  C#  VB.Net  ADO.Net  
Sql Server  SharePoint  Silverlight  Others  All   

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend