i'm having trouble to be able to sort my gridview
with the parameter AutoGenerateColumns = false
The headers are not clickable and so i can't sort, although having the parameter AllowSort = true
Here is my gridview:
<asp:Label ID="lbSortColumn" runat="server" Visible="False"></asp:Label>
<asp:GridView ID="gvDeslocFinal" runat="server" Height="181px" Width="1042px" OnRowDataBound="gvDeslocFinal_RowDataBound" AutoGenerateColumns="false" AllowSorting="true" OnSorting="gvDeslocFinal_Sorting" OnPageIndexChanging="gvDeslocFinal_PageIndexChanging" OnSorted="gvDeslocFinal_Sorted" AllowPaging="True">
<Columns>
...
</Columns>
<EditRowStyle HorizontalAlign="Center" />
<HeaderStyle VerticalAlign="Middle" HorizontalAlign="Center" Font-Bold="False" Height="30px" />
<RowStyle HorizontalAlign="Center" VerticalAlign="Middle" Height="20px" />
</asp:GridView>
This are my functions:
protected void gvDeslocFinal_Sorting(object sender, GridViewSortEventArgs e)
{
if (gvDeslocFinal.EditIndex >= 0)
return;
string[] values = lbSortColumn.Text.Split(' ');
if (values[0] == e.SortExpression)
{
if (values[1] != null && values[1] == "ASC")
lbSortColumn.Text = e.SortExpression + " DESC";
else
lbSortColumn.Text = e.SortExpression + " ASC";
}
else
{
lbSortColumn.Text = e.SortExpression + " ASC";
}
}
protected void gvDeslocFinal_Sorted(object sender, EventArgs e)
{
if (gvDeslocFinal.EditIndex >= 0)
return;
BindGrid();
}
In the code snippet you pasted, you don't show your Column definitions for me to confirm, but this is probably your problem:
If you set
AutoGenerateColumns=false
, then you must define your columns, and in those Column definitions you must set the SortExpression property. E.g....