Geeks With Blogs
AzamSharp Some day I will know everything. I hope that day never comes.

You can also sort DataBound columns of the GridView. The one thing that you need to do is to make the header columns as the links and write the client side scripts which will handle the client postbacks. Check out the code below.

 protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        
if (e.Row.RowType == DataControlRowType.Header)
        {
            TableCellCollection cells = e.Row.Cells;

            
foreach (TableCell cell in cells)
            {
                cell.Text = Server.HtmlDecode(GenerateScript
                (cell.Text,GridView1));
            }
        }
    }

 private string GenerateScript(string columnName,GridView gv)
    {
        
string optionalParam = "Sort$" + columnName;

        StringBuilder sb = 
new StringBuilder();
        sb.Append("<a href=\"");
        sb.Append("javascript:");
        sb.Append(ClientScript.GetPostBackEventReference
        (gv, optionalParam,
false));
        sb.Append("\">");
        sb.Append(columnName);
        sb.Append("</a>");

        
return sb.ToString();         

    }
protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
    {

// Handle this event like I did in the previous post.

    }
    
protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
    {

        
if (e.Row.RowType == DataControlRowType.Header)
        {
            TableCellCollection cells = e.Row.Cells;

            
foreach (TableCell cell in cells)
            {
                GenerateScript(cell.Text, GridView1);
            }
        }
       
    }

As, you can see in the above code that I have made use of the ClientScript.GetPostBackEventReference method. You can also manually type the JavaScript __doPostBack method if you want. Something like this:

 private string GenerateScript(string id,string columnName)
    {
        StringBuilder sb = 
new StringBuilder(); 
        sb.Append("<a href=\"");
        sb.Append("javascript:__doPostBack('");
        sb.Append(id);
        sb.Append("','Sort$");
        sb.Append(columnName);
        sb.Append("')\">");
        sb.Append(columnName);
        sb.Append("</a>"); 
        
        
return sb.ToString();         
    }
Hope it helps!

 

powered by IMHO 1.3

Posted on Friday, April 28, 2006 12:25 AM | Back to top


Comments on this post: Manually Sorting DataBound GridView Columns

# re: Manually Sorting DataBound GridView Columns
Requesting Gravatar...
I keep getting the error:

"Invalid postback or callback argument. Event validation is enabled using <pages enableEventValidation="true"/> in configuration or <%@ Page EnableEventValidation="true" %> in a page. For security purposes, this feature verifies that arguments to postback or callback events originate from the server control that originally rendered them. If the data is valid and expected, use the ClientScriptManager.RegisterForEventValidation method in order to register the postback or callback data for validation."
Left by Gijs on Jul 13, 2006 12:09 AM

# re: Manually Sorting DataBound GridView Columns
Requesting Gravatar...
Btw, I like to keep enableEventValidation to True. I couldn't get it to work with ClientScriptManager.RegisterForEventValidation but that's probably because I suck ;)
Left by Gijs on Jul 13, 2006 12:10 AM

# re: Manually Sorting DataBound GridView Columns
Requesting Gravatar...
This is a new issue with .Net 2.0. See http://www.codeproject.com/useritems/DoubleClickGridviewRow.asp for some guidance.
Left by BPM on Nov 30, 2006 1:57 PM

# re: Manually Sorting DataBound GridView Columns
Requesting Gravatar...
I keep getting the error:

"Invalid postback or callback argument. Event validation is enabled using <pages enableEventValidation="true"/> in configuration or <%@ Page EnableEventValidation="true" %> in a page. For security purposes, this feature verifies that arguments to postback or callback events originate from the server control that originally rendered them. If the data is valid and expected, use the ClientScriptManager.RegisterForEventValidation method in order to register the postback or callback data for validation."

pls rply asap
Left by Gunjan on Sep 11, 2007 2:08 AM

# re: Manually Sorting DataBound GridView Columns
Requesting Gravatar...
This code works well....bt nt on columns with datetime datatype


I want to know how we can sort data with datatype column in grid view


Left by stella on Sep 13, 2007 1:54 AM

Your comment:
 (will show your gravatar)


Copyright © Mohammad Azam | Powered by: GeeksWithBlogs.net | Join free