This is a migrated thread and some comments may be shown as answers.

Programmatically setting the sort icon in column headers

6 Answers 383 Views
Grid
This is a migrated thread and some comments may be shown as answers.
MatFindlay
Top achievements
Rank 1
MatFindlay asked on 03 Feb 2009, 05:56 PM
Is there any way to directly set what sort icon is shown in a column header? The sorting of the data is already being taken care of by the service layer of the app I'm working on, so I don't want the grid to take care of any sorting. All I need to do is change the sort icon when the grid is rebound, and I can't seem to find a way to do this. Is there one?

6 Answers, 1 is accepted

Sort by
0
Daniel
Telerik team
answered on 04 Feb 2009, 09:57 AM
Hello Mat,

You can use the properties of the underlying column (SortAscImageUrl, SortDescImageUrl, ShowSortIcon):
protected void Page_PreRender(object o, EventArgs args) 
    RadGrid1.MasterTableView.GetColumn("Name").SortAscImageUrl = "yourImage.gif"

Best regards,
Daniel
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
Princy
Top achievements
Rank 2
answered on 04 Feb 2009, 11:01 AM
Hello Mat,

You can add a custom image to the header for the corresponding column as shown in the code below. Since the data is already sorted prior binding to grid, you can set the image url according to the sort order in which the data is sorted.
cs:
protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e) 
    { 
      if (e.Item is GridHeaderItem) 
        { 
            GridHeaderItem header = (GridHeaderItem)e.Item; 
            Image image = new Image(); 
            image.ID="Image1"
            image.ImageUrl="~/image.gif"
            header["ColumnUniqueName"].Controls.Add(image); 
        } 
     } 

Thanks
Princy.
0
MatFindlay
Top achievements
Rank 1
answered on 05 Feb 2009, 09:32 PM
This technique works great for images. However, if I wanted to include a button or something else that would cause a postback, this technique causes problems. Because the button is created on the server side after Page_Load, the click event won't actually fire when posting back to the server.

I've tried using a header template with the grid that uses the button, but when I used LinkButtons in the template to trigger sorting, something was causing the page to reload something like 6 times without registering as a postback. Do you know of any ways to get around this?
0
Shinu
Top achievements
Rank 2
answered on 06 Feb 2009, 04:03 AM
Hi Mat,

Try adding the above logic in the ItemCreated event and see if it is helpful.

CS:
 protected void RadGrid1_ItemCreated(object sender, GridItemEventArgs e) 
    { 
        if (e.Item is GridHeaderItem) 
        { 
            GridHeaderItem header = (GridHeaderItem)e.Item; 
            ImageButton imageBtn = new ImageButton(); 
            imageBtn.ID = "Image1"
            imageBtn.ImageUrl = "~/Images/Image1.gif"
            imageBtn.Click += new ImageClickEventHandler(imageBtn_Click); 
            header["columnUniqueName"].Controls.Add(imageBtn); 
        }  
    } 
 
    void imageBtn_Click(object sender, ImageClickEventArgs e) 
    { 
       
    } 


Thanks
Shinu
0
MatFindlay
Top achievements
Rank 1
answered on 06 Feb 2009, 04:06 PM
Thanks, that totally worked.
0
Shinu
Top achievements
Rank 2
answered on 09 Feb 2009, 11:33 AM
Hi,

You may also refer the following help article which explains the difference between ItemCreated and ItemDataBound event.
Distinguishing the major differences between ItemCreated and ItemDataBound events

Regards
Shinu
Tags
Grid
Asked by
MatFindlay
Top achievements
Rank 1
Answers by
Daniel
Telerik team
Princy
Top achievements
Rank 2
MatFindlay
Top achievements
Rank 1
Shinu
Top achievements
Rank 2
Share this question
or