Ver Mensaje Individual
  #2 (permalink)  
Antiguo 14/11/2008, 09:37
Avatar de mdavila
mdavila
 
Fecha de Ingreso: julio-2007
Ubicación: Montevideo (Uruguay)
Mensajes: 919
Antigüedad: 16 años, 9 meses
Puntos: 13
Respuesta: ordenar gridview por columnas

No te voy a mentir.... lo realize y me fue dificil hacerlo funcionar correctamente.... te recomiendo que pongas un DropDownList con las opciones de ordenamiento y listo.

Igualmente te tiro como lo realize, aunque es un poco entreverado de entender.


Protected Sub Gr_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles Gr.RowDataBound
If Page.IsPostBack Then
If e.Row.RowType = ListItemType.Header Then
For Each tc As TableCell In e.Row.Cells
If tc.Controls.Count > 0 And tc.Controls.Count < 2 Then
If Ordenar.Value.ToString.Contains(CType(tc.Controls( 0), System.Web.UI.WebControls.LinkButton).CommandArgum ent) Then
Dim img As New System.Web.UI.WebControls.Image
If HOrden3.Value = "Asc" Then
img.ImageUrl = "arriba.gif"
Else
img.ImageUrl = "abajo.gif"
End If
img.Width = System.Web.UI.WebControls.Unit.Pixel(15)
img.Height = System.Web.UI.WebControls.Unit.Pixel(10)
tc.Controls.AddAt(0, img)
End If
End If
Next
End If
End If
End Sub


Horden3 es un campo oculto que no inicializo en Asc en el page load.


Protected Sub Gr_Sorting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewSortEventArgs) Handles Gr.Sorting
Select Case e.SortExpression.Trim
Case "Fecha"
Ordenar.Value = "UsuariosPostulados.FechaPostula"
HIndicePintar.Value = 1
IndicePintar = 1
Case "Nombre"
Ordenar.Value = "Usuarios.Nombre"
HIndicePintar.Value = 2
IndicePintar = 2
Case "Apellido"
Ordenar.Value = "Usuarios.Apellido"
HIndicePintar.Value = 3
IndicePintar = 3
Case "Telefono"
Ordenar.Value = "Usuarios.Telefono"
HIndicePintar.Value = 4
IndicePintar = 4
Case "Mail"
Ordenar.Value = "Usuarios.Mail"
HIndicePintar.Value = 5
IndicePintar = 5
Case "Pais"
Ordenar.Value = "Usuarios.Pais"
HIndicePintar.Value = 6
IndicePintar = 6
End Select
HOrden2.Value = "True"
Gr.PageIndex = 0
LlenarGrilla(Ordenar.Value)
End Sub


'cuando se crea la grilla
Protected Sub Gr_RowCreated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles Gr.RowCreated
Dim itemType As ListItemType

'esto es para que cuando pase el mouse por arriba las celdas cambien de color
If e.Row.RowType <> DataControlRowType.Header And e.Row.RowType <> DataControlRowType.Pager Then

e.Row.BackColor = Color.FromName("#EEEEEE")

If (itemType <> ListItemType.AlternatingItem) Then
e.Row.Attributes.Add("OnMouseOver", "this.style.backgroundColor = 'Gainsboro';")
e.Row.Attributes.Add("OnMouseOut", "this.style.backgroundColor = '#EEEEEE';")
Else
e.Row.Attributes.Add("OnMouseOver", "this.style.backgroundColor = 'Gainsboro';")
e.Row.Attributes.Add("OnMouseOut", "this.style.backgroundColor = '#EEEEEE';")
End If
End If

If e.Row.RowType = DataControlRowType.Header Then
e.Row.Cells(IndicePintar).BackColor = Color.FromName("#760205")

End If

End Sub




Mucha suerte y vas a tener que cambiar cosas porque son personales de mi proyecto ok.