Hello,
I've tried sorting a gridview but am having problems. My gridview has autogeneratecolumns=true, allowsorting=true...it does not have a datasource control..it's is bounded to a datatable I get from my data layer. I've added an event handler for the onsorting event, but it still doesn't work. I don't get an error, it just doesn't sort. When I step thru the code I find that my GridView1.DataSource = null. What am I missing? Do I have to bind it again before the sorting?
Thanks in advance for any help.
I've tried sorting a gridview but am having problems. My gridview has autogeneratecolumns=true, allowsorting=true...it does not have a datasource control..it's is bounded to a datatable I get from my data layer. I've added an event handler for the onsorting event, but it still doesn't work. I don't get an error, it just doesn't sort. When I step thru the code I find that my GridView1.DataSource = null. What am I missing? Do I have to bind it again before the sorting?
Thanks in advance for any help.
Code:
<asp:GridView ID="GridView1" runat="server" allowPaging="True" AllowSorting="true" AutoGenerateColumns="true" EmptyDataText="No results match that criteria" OnSorting="GridView1_Sorting"></asp:GridView>
protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
{
DataTable dataTable = GridView1.DataSource as DataTable;
if (dataTable != null)
{
DataView dataView = new DataView(dataTable);
dataView.Sort = e.SortExpression + " " + ConvertSortDirectionToSql(e.SortDirection);
GridView1.DataSource = dataView;
GridView1.DataBind();
}
}