Ver Mensaje Individual
  #8 (permalink)  
Antiguo 11/03/2010, 15:15
unidadm
 
Fecha de Ingreso: marzo-2010
Mensajes: 5
Antigüedad: 14 años, 1 mes
Puntos: 0
Respuesta: Formato a columnas autogeneradas en Datagrid o Gridview

Lo intenté de esa forma, aunque no encuentro el evento al que te refieres, lo intente en los eventos Databinding y Databound y nada, y es que resulta que al ser columnas autogeneradas el gridview.columns.count siempre devuelve 0.

Despues de varios intentos he logrado hacer lo siguiente, quiza no sea muy optimo, pero por el momento me sirve y quiza a alguien le pueda servir tb, pero de todas formas sigo buscando una manera mas simple. Este codigo aplica el nombre de los caption de las columnas del datatable al gridview y aplica el formato de fecha a las columnas, ojo que el gridview tiene mas columnas que el datatable porque tiene 2 templates al inicio

Código:
Protected Sub gv_lista_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gv_lista.RowDataBound
            Dim li_columna, li_diferencia As Integer
            Dim ls_columna As String
            Dim ldt_dt As New DataTable
            Dim lgv_gv As GridView = CType(sender, GridView)

            If lgv_gv.DataSource.GetType Is GetType(DataTable) Then
                ldt_dt = lgv_gv.DataSource
            ElseIf lgv_gv.DataSource.GetType Is GetType(DataView) Then
                ldt_dt = lgv_gv.DataSource.table
            End If

            If e.Row.RowType = ListItemType.Header Then
                For li_columna = 0 To e.Row.Cells.Count - 1
                    If Not ldt_dt.Columns(e.Row.Cells(li_columna).Text) Is Nothing Then
                        e.Row.Cells(li_columna).Text = ldt_dt.Columns(e.Row.Cells(li_columna).Text).Caption
                    End If
                Next
            ElseIf e.Row.RowType = ListItemType.Item Or e.Row.RowType = ListItemType.AlternatingItem Then
                li_diferencia = e.Row.Cells.Count - ldt_dt.Columns.Count

                For li_columna = li_diferencia To e.Row.Cells.Count - 1
                    ls_columna = ldt_dt.Columns(li_columna - li_diferencia).ColumnName

                    If e.Row.DataItem(ls_columna).GetType Is GetType(Date) Then
                        e.Row.Cells(li_columna).Text = CType(e.Row.DataItem(ls_columna), Date).ToString("dd/MM/yyyy")
                        e.Row.Cells(li_columna).Width = 80
                        e.Row.Cells(li_columna).Style("text-align") = "center"
                    End If
                Next
            End If
        End Sub