Sorting and Template Columns

D

Dave E

I have a DataGrid set up and it works great. Because I need to modify the data on the fly and I don't know what columns I will have I create template columns programmatically adn add them to the datagrid. That works all fine and dandy. However I am now trying to add sorting and I cannot figure out how to assign the Sort function. I have created my template class from ITemplate and have the InstantiateIn function created. If I just add the Allowsorting and OnSortCommand to the definition in .aspx file it does not give me links the column headers. If I add them programmatically it also does not get me links in the column headers. I am assuming this problem is because I have the Template class that takes over the rendering of the items.

My question is, how would I add this to the InstantiateIn function in the Template class? I do not have a clue. I have tried a variety of things but cannot seem to get it to work.
 
J

Jacob Yang [MSFT]

Hi David,

I have reviewed your issue. Due to the nature of your issue I need to do
additional research to determine the best way to provide assistance. I will
contact you as soon as possible.

Best regards,

Jacob Yang
Microsoft Online Partner Support
Get Secure! ¨C www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
J

Jacob Yang [MSFT]

Hi David,

Based on my research, the problem comes from the SortExpression property on
the Template Column, not the InstantiateIn function.

If the SortExpression property on a template column was not set, the column
header will not be rendered as LinkButton controls that raise the
SortCommand event when clicked. You can experience this behavior with the
VS.NET IDE. You can drag a DataGrid control on to a web page, enable the
AllowSorting properties, and then add a template column with the Header
Text into the DataGrid. If left the SortExpression empty, the Header Text
of this template column will not be displayed as a link.

Please refer to this URL for the sample code on how to set the
SortExpression properties on a template column.

http://www.superdotnet.com/Article.aspx?ArticleID=116

Does it answer your question? If I have misunderstood your concern, please
feel free to let me know.

Best regards,

Jacob Yang
Microsoft Online Partner Support
Get Secure! ¨C www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
J

Jim Nugent

Dave E said:
I have a DataGrid set up and it works great. Because I need to modify the
data on the fly and I don't know what columns I will have I create template
columns programmatically adn add them to the datagrid. That works all fine
and dandy. However I am now trying to add sorting and I cannot figure out
how to assign the Sort function. I have created my template class from
ITemplate and have the InstantiateIn function created. If I just add the
Allowsorting and OnSortCommand to the definition in .aspx file it does not
give me links the column headers. If I add them programmatically it also
does not get me links in the column headers. I am assuming this problem is
because I have the Template class that takes over the rendering of the
items.
My question is, how would I add this to the InstantiateIn function in the
Template class? I do not have a clue. I have tried a variety of things but
cannot seem to get it to work.

To get the linkbuttons, you need to set the sort expression for the column,
something like this:

DataGrid1.Columns[0].SortExpression = "LastName". This cause make your
column headers into linkbuttons.

The expression is what will be passed to your OnSortCommand() prodcudure as

DataGridSortCommandEventArgs e.SortExpression when the user clicks on that
link. Normally you would enter the sort expression for each column in the
DataGrid property builder but since you are creating columns dynamically,
you will have to set this property in the program.

You also have to register a delegate to hand the SortCommand event:

this.DataGrid1.SortCommand += new
System.Web.UI.WebControls.DataGridSortCommandEventHandler(this.DataGrid1_Sor
tCommand);

if it is not being done already. You can do it dymanically but since it is
only necessary to do it once for the grid, It's probably easier to do it in
design mode: in the Datagrid properties tab, select events (the lighning
bolt) and next sort command select the member function to handle the event
from the drop down. Then it add the code to register your function to the
designer code.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,983
Messages
2,570,187
Members
46,747
Latest member
jojoBizaroo

Latest Threads

Top