?
Solved

ASP.NET 2.0 C#: Sorting GridView by Code

Posted on 2007-08-06
11
Medium Priority
?
13,058 Views
Last Modified: 2013-11-05
Hi All,
 i have a gridview control that is binded using DataSet object that is filled by code.
my gridview has a template fields(for example i use template field to display country name instead of countrycode)
I need to apply sorting on the gridview, and when click on the template field i want to sort using the displayed text not the original code that is used while binding.
i don't know how to do that.
any comment will very helpful
thanks.
0
Comment
Question by:samjav
  • 5
  • 3
  • 3
11 Comments
 
LVL 28

Expert Comment

by:mmarinov
ID: 19637459
Hello samjav,

See this post: http://weblogs.asp.net/scottgu/archive/2006/02/11/437995.aspx

Regards,
Martin
0
 

Author Comment

by:samjav
ID: 19643761
thanks mmarinov for the post
but i need to sort the template filed according to the displyed value not to the bounded column value!!!!
0
 
LVL 28

Expert Comment

by:mmarinov
ID: 19643772
As far as I know and have the experience, you can determine the criteria for sorting.
How do you set the information shown in the GridView ?

Martin
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

Author Comment

by:samjav
ID: 19643816
I get it from database using Dataset that is filled by code not from the wizard(we need to do that in our project), then i define bound and template fields acooring to my need in the gridview at design time.
in the template fields i call a public function to return string to be displayed as text in the template field  accoring to the value i got from database.
the problem when i do sorting, i have to define the sort expression for each column, an in case of template filed it will be not the displayed  text;for example  the column from database  is "Application No" but the displayed one is "Application Name" (which returned from the public function i talked about(from another table)).
and if i set the sor expression for this column Application Name, it gave an error saying Invalid column Name Application Name....
0
 
LVL 28

Expert Comment

by:mmarinov
ID: 19643886
So you have changed the label of the column correct?
If this is true you can use this
HeaderText = your custom name
SortExpression = the original column name

Then you will have the appropriate data.

Martin
0
 

Author Comment

by:samjav
ID: 19643911
sorry i didn't understand what you wrote?
0
 
LVL 8

Expert Comment

by:unmeshdave
ID: 19646057
Hi Samjav,
I hope follwing will be useful.

in your templatefield tag u can use the sortExpression as ur original field means here as countrycode.
eg;
<asp:TemplateField HeaderText=""Countryname"" SortExpression=""CountryCode"">.
(Note the twice use of doublequotes on both the side of field name).
 Hope I answered ur question correctly.
0
 
LVL 8

Expert Comment

by:unmeshdave
ID: 19646121
and if you are adding templatefield dynamically, through code, u can set the relevant properties(sortExpression & HeaderText) of the templatefield dynamically same way as above.
0
 

Author Comment

by:samjav
ID: 19651699
Hi unmeshdave
sorry it seems that i didn't clarify what i need correctly...
i do not want to sort using CountryCode, i want to sort using the Country names dispayed in the template field after binding..
My Template field is like this:
<asp:TemplateField HeaderText="Country Name">
                    <ItemTemplate>
                             <%# GetCountryName((int)Eval("CountryCode"))%>
                     </ItemTemplate>
                     <ItemStyle Width="100px" />
</asp:TemplateField>

where GetCountryName is a public function used to get the name of the country code, i want to sort using this name.
can i do that or not?
0
 
LVL 8

Accepted Solution

by:
unmeshdave earned 500 total points
ID: 19654855
yes you can, little bit different way...

I guess, "countryname" is not part of your database. so the following.

As you are filling your dataset dynamically so you add one more column as "countryname" to dataset (dynamically after getting result from database).
the value for countryname column in each row will be filled by using your public method. then use this countryname field as sortexpression into grid view.

--------------------------------------
for adding column dynamically (i think u r already doing this)
eg;
Dim VisasDS As New DataSet("Visas") //this is ur dataset

Dim CountriesTable As DataTable = VisasDS.Tables.Add("Countries Table") //no need to add table for u as u r already have a table as a part of ur dataset -ie - result of ur query or storedproc.

CountriesTables.Columns.Add("Countryname", Type.GetType("System.String")
-------------------------------------
0
 

Author Comment

by:samjav
ID: 19659838
thanks very much unmeshdave...
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Loops Section Overview
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
Suggested Courses

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question