单击asp.net Gridview Header时尝试排序

时间:2015-07-21 01:33:06

标签: asp.net entity-framework c#-4.0 gridview

我把我的代码放在下面的所有步骤中。 Asp.Net代码

<asp:GridView AllowSorting="true" ID="GrdSolicitacoes" runat="server" GridLines="None" AllowPaging="True" PageSize="10" OnPageIndexChanging="GrdSolicitacoes_PageIndexChanging" OnSorting="gvShows_Sorting">
<Columns>
<asp:BoundField DataField="id" HeaderText="Cód." SortExpression="id </asp:BoundField>
 <asp:BoundField DataField="cns" DataFormatString="{0:dd/MM/yyyy}" HeaderText="CNS" SortExpression="cns" ></asp:BoundField>
<asp:BoundField DataField="nome" HeaderText="Nome do paciente" SortExpression="nome"  ></asp:BoundField>
<asp:BoundField DataField="data_nascimento" HeaderText="Data de nascimento" DataFormatString="{0:dd/MM/yyyy}" SortExpression="data_nascimento </asp:BoundField>
<asp:BoundField DataField="nome_mae" HeaderText="Nome da mãe" SortExpression="nome_mae"></asp:BoundField>
</Columns>
</asp:GridView>

C#(BackEnd Code) 在下面填充网格视图

private void populaGrid()
{
private ModelDataContext mdc;
mdc = new ModelDataContext();
        try
        {
            var pacientes = from pac in mdc.Tbl_Pacientes
                            select new
                            {
                                id = pac.id,
                                cns = pac.cns,
                                nome = pac.nome,
                                data_nascimento = pac.data_nascimento,
                                nome_mae = pac.nome_mae,
                                idAtendimento = 0

                            }; 
                GrdSolicitacoes.DataSource = pacientes;
                GrdSolicitacoes.DataBind();
        }
        catch (Exception ex) { Page.ClientScript.RegisterClientScriptBlock(GetType(), "alerta", "alert('Erro ao Consultar')" + ex.Message, true); }
        finally { mdc.Dispose(); }
    }

下面的导入方法

    private string ConvertSortDirection(SortDirection sortDirection)
    {
        string newSortDirection = String.Empty;
        switch (sortDirection)
        {
            case SortDirection.Ascending:
                newSortDirection = "ASC";
                break;

            case SortDirection.Descending:
                newSortDirection = "DESC";
                break;
        }
        return newSortDirection;
    }

    protected void gvShows_Sorting(object sender, GridViewSortEventArgs e)
    {
        Debug.WriteLine("sorting is called");
        var dataTable = Session["pacientes"] as DataTable;

        if (dataTable != null)
        {
            var dataView = new DataView(dataTable)
            {
                Sort = e.SortExpression + " " + ConvertSortDirection(e.SortDirection)
            };

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

任何人都可以指导我做错了吗?当我单击“OnSort”方法时,它会调用该函数但不会更改任何内容。我检查了这里已经讨论过的所有教程但是这个东西不起作用。我相信我错过了一些非常详细的plx帮助。

0 个答案:

没有答案