Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Shaun E on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

sorting Gridview

Status
Not open for further replies.

jrenae

Programmer
Jan 18, 2006
142
US
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.

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();
        }
    }
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top