ASP.NET 2.0 C#: Sorting GridView by Code

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.
samjavAsked:
Who is Participating?
 
unmeshdaveCommented:
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
 
mmarinovCommented:
Hello samjav,

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

Regards,
Martin
0
 
samjavAuthor Commented:
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
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

 
mmarinovCommented:
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
 
samjavAuthor Commented:
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
 
mmarinovCommented:
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
 
samjavAuthor Commented:
sorry i didn't understand what you wrote?
0
 
unmeshdaveCommented:
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
 
unmeshdaveCommented:
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
 
samjavAuthor Commented:
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
 
samjavAuthor Commented:
thanks very much unmeshdave...
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.