Disable sorting for one column in autogenerated GridView

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.AddAt(0, new LiteralControl((ctl as LinkButton).Text));


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


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 )

Google+ photo

You are commenting using your Google+ 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 )


Connecting to %s