Selective Sorting with AutoGeneratedColumns in GridView


One user from ASP.NET forum was asking how to do this. (http://forums.asp.net/t/1191176.aspx)

Here is the solution for that. 

protected void GridView1_OnRowDataBound(object sender, GridViewRowEventArgs e)
    {    if (e.Row.RowType == DataControlRowType.Header)

{
    Control ctl0 = e.Row.Controls[2];// column 3
    Control ctl = ctl0.Controls[0]; //first control in column 3

    if (ctl is LinkButton)
    {
     ctl0.Controls.Remove(ctl);
     ctl0.Controls.AddAt(0, new LiteralControl((ctl as LinkButton).Text));
    }
}

GridView1:

 <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="true" DataKeyNames="id" AllowSorting="true"
            DataSourceID="SqlDataSource1" OnRowDataBound="GridView1_OnRowDataBound"></asp:GridView

Advertisements


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s