Jump to content

ordenar gridview


999ANDRE999

Recommended Posts

Boas pessoal,

Necessito de ordenar a minha gridview a partir da coluna. Tentei utilizar o sorting mas sem sucesso.

adicionei na gridview:

<asp:GridView ... OnSorting="gridView1_Sorting" AllowSorting="true">

E no CodeBehind:

private string ConverterSortDirection(SortDirection sortDirection)
{
    string novaDirecao = String.Empty;

    switch (sortDirection)
    {
        case SortDirection.Ascending:
            novaDirecao = "ASC";
            break;
        case SortDirection.Descending:
            novaDirecao = "DESC";
            break;
    }
    return novaDirecao;
}


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 + " " + ConverterSortDirection(e.SortDirection);

        gridView1.DataSource = dataView;
        gridView1.DataBind();
    }
}

Mas não faz nada.

Alguem consegue ajudar?

Link to comment
Share on other sites

Coloquei este código e já não dá erros nenhuns, mas ao clicar no nome da coluna não faz nada, só pisca, tipo um refresh

public string SortingExpression
    {
        get
        {
            if (this.ViewState["SortExpression"] == null)
                return "";
            else
                return (string)this.ViewState["SortExpression"];
        }

        set
        {
            this.ViewState["SortExpression"] = value;
        }
    }

    protected void Sorting(object sender, GridViewSortEventArgs e)
    {
        DataTable m_DataTable = GridView_phone.DataSource as DataTable;

        if (m_DataTable != null)
        {
            DataView m_DataView = new DataView(m_DataTable);
            SortingExpression = e.SortExpression + " " + (SortingExpression.Contains("ASC") ? "DESC" : "ASC");
            m_DataView.Sort = SortingExpression;

            GridView_phone.DataSource = m_DataView;
            GridView_phone.DataBind();
        }
    }
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.