Ver Mensaje Individual
  #2 (permalink)  
Antiguo 09/08/2007, 12:51
programadorvip
 
Fecha de Ingreso: agosto-2006
Ubicación: en lima peru
Mensajes: 184
Antigüedad: 18 años, 4 meses
Puntos: 0
Re: problema evento sorting

CODE BEHIND
Código:
Imports System.Data
Imports System.Data.SqlClient

Partial Class _Default
    Inherits System.Web.UI.Page
    Private dvwOrden As DataView 'Definir la vista de Ordenes
    Private dvwDetalle As DataView 'Definir la vista de Detalles

    Private Sub EnlazarGridView(ByVal gvw As GridView, _
    ByVal dvw As DataView)
        With gvw
            .DataSource = dvw
            .DataBind()
        End With
    End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Me.Page.MaintainScrollPositionOnPostBack = True
        If Not Page.IsPostBack Then
            Using con As New SqlConnection("uid=NWindUsuario;pwd=chaminade;server=Lab2_10;database=Northwind")
                Using dap As New SqlDataAdapter("", con)
                    Dim dst As New DataSet
                    con.Open()
                    dap.SelectCommand.CommandText = _
                    "Select OrderID,OrderDate,CustomerID From Orders"
                    dap.Fill(dst, "Ordenes")
                    'Crear la vista de Ordenes
                    dvwOrden = dst.Tables(0).DefaultView
                    dap.SelectCommand.CommandText = _
                    "Select OrderID,ProductID,UnitPrice,Quantity From [Order Details]"
                    dap.Fill(dst, "Detalles")
                    'Crear la vista de Detalles
                    dvwDetalle = dst.Tables(1).DefaultView
                    'Guardar las vistas en el Cache
                    Cache("dvwOrden") = dvwOrden
                    Cache("dvwDetalle") = dvwDetalle
                    'Enlazar la vista de Ordenes al GridView
                    EnlazarGridView(gvwOrden, dvwOrden)
                End Using
            End Using
        End If
    End Sub

    Protected Sub gvwOrden_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles gvwOrden.PageIndexChanging
        'Recuperar la vista de Ordenes de la memoria cache
        dvwOrden = Cache("dvwOrden")
        'Especificar como pagina actual la seleccionada
        gvwOrden.PageIndex = e.NewPageIndex
        EnlazarGridView(gvwOrden, dvwOrden)
        If gvwOrden.SelectedIndex > -1 Then ListarDetalles()
    End Sub

    Private Sub ListarDetalles()
        'Recuperar de la memoria Cache la vista de Ordenes
        dvwDetalle = Cache("dvwDetalle")
        'Filtrar la vista de Ordenes de acuerdo al Empleado seleccionado
        dvwDetalle.RowFilter = "OrderID=" & _
        gvwOrden.SelectedRow.Cells(0).Text
        'Enlazar la vista de Detalles al GridView
        EnlazarGridView(gvwDetalle, dvwDetalle)
    End Sub

    Protected Sub gvwOrden_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles gvwOrden.SelectedIndexChanged
        ListarDetalles()
    End Sub

    Protected Sub gvwOrden_Sorting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewSortEventArgs) Handles gvwOrden.Sorting
        'Recuperar la vista de la memoria cache
        dvwOrden = Cache("dvwOrden")
        Dim Campo As String = e.SortExpression
        'Si ya existe Recuperar el Tipo de Orden del Estado de la Vista 
        If ViewState(Campo) IsNot Nothing Then
            e.SortDirection = ViewState(Campo)
        Else
            e.SortDirection = SortDirection.Descending
        End If
        'Recuperar el tipo de orden
        If e.SortDirection = SortDirection.Descending Then
            dvwOrden.Sort = e.SortExpression & " asc"
            e.SortDirection = SortDirection.Ascending
        Else
            dvwOrden.Sort = e.SortExpression & " desc"
            e.SortDirection = SortDirection.Descending
        End If
        'Ordenar la vista por el campo seleccionado en el GridView
        EnlazarGridView(gvwOrden, dvwOrden)
        If gvwOrden.SelectedIndex > -1 Then ListarDetalles()
        'Guardar el Tipo de Orden en una variable de sesion
        ViewState(Campo) = e.SortDirection
    End Sub
End Class
EN EL ASPX

Código:
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body bgcolor="#3399ff">
    <form id="form1" runat="server">
    <div>
        <div style="text-align: center">
            <table style="width: 400px">
                <tr>
                    <td align="center" style="width: 100px">
                        <asp:Label ID="lblTitulo" runat="server" Font-Size="Large" ForeColor="Blue" Text="Demo 27: Paginacion, Ordenacion y Filtro"></asp:Label></td>
                </tr>
                <tr>
                    <td align="center" style="width: 100px; height: 21px">
                        <asp:Label ID="lblSubtitulo" runat="server" Font-Size="Medium" ForeColor="White"
                            Text="Consulta de Detalles de Ordenes"></asp:Label></td>
                </tr>
                <tr>
                    <td align="center" style="width: 100px; height: 222px">
                        &nbsp;<asp:GridView ID="gvwOrden" runat="server" AutoGenerateColumns="False" BackColor="White"
                            BorderColor="#999999" BorderStyle="None" BorderWidth="1px" CellPadding="3" GridLines="Vertical"
                            Width="403px" AllowPaging="True" AllowSorting="True">
                            <FooterStyle BackColor="#CCCCCC" ForeColor="Black" />
                            <Columns>
                                <asp:BoundField DataField="OrderID" HeaderText="Nro Orden" SortExpression="OrderID" />
                                <asp:BoundField DataField="OrderDate" HeaderText="Fecha Orden" SortExpression="OrderDate" />
                                <asp:BoundField DataField="CustomerID" HeaderText="Cliente" SortExpression="CustomerID" />
                                <asp:CommandField ButtonType="Button" HeaderText="Operacion" SelectText="Ver Detalles"
                                    ShowSelectButton="True" />
                            </Columns>
                            <RowStyle BackColor="#EEEEEE" ForeColor="Black" />
                            <SelectedRowStyle BackColor="#008A8C" Font-Bold="True" ForeColor="White" />
                            <PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />
                            <HeaderStyle BackColor="#000084" Font-Bold="True" ForeColor="White" />
                            <AlternatingRowStyle BackColor="Gainsboro" />
                        </asp:GridView>
                    </td>
                </tr>
                <tr>
                    <td align="center" style="width: 100px; height: 21px;">
                        <asp:GridView ID="gvwDetalle" runat="server" AutoGenerateColumns="False" BackColor="#DEBA84"
                            BorderColor="#DEBA84" BorderStyle="None" BorderWidth="1px" CellPadding="3" CellSpacing="2"
                            Width="407px">
                            <FooterStyle BackColor="#F7DFB5" ForeColor="#8C4510" />
                            <Columns>
                                <asp:BoundField DataField="ProductID" HeaderText="Producto" />
                                <asp:BoundField DataField="UnitPrice" HeaderText="Precio" />
                                <asp:BoundField DataField="Quantity" HeaderText="Cantidad" />
                            </Columns>
                            <RowStyle BackColor="#FFF7E7" ForeColor="#8C4510" />
                            <SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="White" />
                            <PagerStyle ForeColor="#8C4510" HorizontalAlign="Center" />
                            <HeaderStyle BackColor="#A55129" Font-Bold="True" ForeColor="White" />
                        </asp:GridView>
                        &nbsp;</td>
                </tr>
            </table>
        </div>
    
    </div>
    </form>
</body>
</html>