Visual Basic.NET - Como puedo imprimir fecha sin hora

 
Vista:
sin imagen de perfil
Val: 229
Ha disminuido 1 puesto en Visual Basic.NET (en relación al último mes)
Gráfica de Visual Basic.NET

Como puedo imprimir fecha sin hora

Publicado por José Vicente (113 intervenciones) el 15/09/2020 12:58:02
Hola tengo un form con un botón de imprimir una BD Access. En el datagrid me muestra el campo fecha bien, osea, sólo la fecha pero cuando imprimo la BD me lo imprime como fecha/hora aunque el campo está declarado como fecha y fecha corta. ¿Qué puedo hacer? Gracias.
Adjunto el código del botón:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
Private Sub PrintDocument1_PrintPage(sender As Object, e As Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
 
    'DEFINIMOS LA FUENTE QUE VAMOS A USAR PARA IMPRIMIR
 
    Dim printFont As System.Drawing.Font = New Drawing.Font("Verdana", 10, FontStyle.Regular)
    Dim topmargin As Double = e.MarginBounds.Top
    Dim yPos As Double = 0
    Dim linesPerPage As Double = 0
    Dim count As Integer = 0
    Dim texto As String = ""
    Dim row As System.Windows.Forms.DataGridViewRow
 
    'CALCULAMOS EL NÚMERO DE LÍNEAS QUE CABEN EN CADA PÁGINA
 
    linesPerPage = e.MarginBounds.Height / printFont.GetHeight(e.Graphics)
 
    'IMPRIMIMOS LAS CABECERAS
 
    Dim header As DataGridViewHeaderCell
 
    For Each column As DataGridViewColumn In DataGridView1.Columns
 
        header = column.HeaderCell
        texto += "    " & header.FormattedValue.ToString() & "       "
 
    Next
 
    yPos = topmargin + (count * printFont.GetHeight(e.Graphics))
    e.Graphics.DrawString(texto, printFont, System.Drawing.Brushes.Black, 10, yPos)
 
    'DEJAMOS UNA LÍNEA DE SEPARACIÓN
 
    count += 2
 
    'RECORREMOS EL DATAGRIDVIEW HASTA LLEGAR AL TOPE DE LÍNEAS DE LA PÁGINA O EL FINAL DEL DATAGRIDVIEW
 
    While count < linesPerPage AndAlso i < DataGridView1.Rows.Count
 
        row = DataGridView1.Rows(i)
 
        DataGridView1.Rows(i).Cells(0).Style.Format = "dd/MM/yyyy"
 
        texto = ""
 
        For Each celda As System.Windows.Forms.DataGridViewCell In row.Cells
 
            'COMPROBAMOS QUE LA CELDA NO SEA NULA, SI PERMITIMOS AÑADIR FILAS ESTO ES MUY IMPORTANTE
 
            If celda.Value IsNot Nothing Then
 
                texto += "   " & celda.Value.ToString() & "      "
 
            End If
 
        Next
 
        'CALCULAMOS LA POSICIÓN EN QUE SE ESCRIBE LA LÍNEA
 
        yPos = topmargin + (count * printFont.GetHeight(e.Graphics))
 
        'ESCRIBIMOS LA LINEA CON EL OBJETO GRAPHICS
 
        e.Graphics.DrawString(texto, printFont, System.Drawing.Brushes.Black, 10, yPos)
 
        'INCREMENTAMOS LOS CONTADORES
 
        count += 1
        i += 1
 
    End While
 
    'COMPROBAMOS SI QUEDAN MÁS FILAS POR IMPRIMIR, SI ES ASÍ SALDRÁN EN LA SIGUIENTE PÁGINA
 
    If i < DataGridView1.Rows.Count Then
 
        e.HasMorePages = True
 
    Else
 
        'SI LLEGAMOS AL FINAL DE HASMOREPAGES SE ESTABLECE A FALSE PRA DEJAR DE IMPRIMIR
 
        e.HasMorePages = False
 
        'HAY QUE PONER EL CONTADOR A 0 PARA QUE NO SE QUEDE PILLADA LA VARIABLE i Y NO IMPRIMA NADA
 
        i = 0
 
    End If
 
End Sub
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder
Imágen de perfil de Phil Rob
Val: 3.353
Oro
Ha mantenido su posición en Visual Basic.NET (en relación al último mes)
Gráfica de Visual Basic.NET

Como puedo imprimir fecha sin hora

Publicado por Phil Rob (1546 intervenciones) el 15/09/2020 13:52:29
Hola,

Quizá este codigo es mejor :

1
DataGridView1.Rows(i).Cells(0).Value =     DataGridView1.Rows(i).Cells(0).Value.ToShortDateString()

Si no funciona, tiene que escribir :

1
texto += "   " & celda.Value.ToShortDateString() & "      " ' !!! solamente CUANDO este es la columna 0.

Espero que este te ayudara ...
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar
sin imagen de perfil
Val: 229
Ha disminuido 1 puesto en Visual Basic.NET (en relación al último mes)
Gráfica de Visual Basic.NET

Como puedo imprimir fecha sin hora

Publicado por José Vicente (113 intervenciones) el 15/09/2020 14:12:38
No me funciona. me lo muestra igual.
fecha sistolica diastolica pulsaciones saturacion
29/07/2020 0:00:00 9,8 5,6 67 100
30/07/2020 0:00:00 10,7 6,1 70 99
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar
Imágen de perfil de Phil Rob
Val: 3.353
Oro
Ha mantenido su posición en Visual Basic.NET (en relación al último mes)
Gráfica de Visual Basic.NET

Como puedo imprimir fecha sin hora

Publicado por Phil Rob (1546 intervenciones) el 15/09/2020 14:23:35
Este tu codigo anterior :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
While count < linesPerPage AndAlso i < DataGridView1.Rows.Count
 
    row = DataGridView1.Rows(i)
 
    DataGridView1.Rows(i).Cells(0).Style.Format = "dd/MM/yyyy"
 
    texto = ""
 
    For Each celda As System.Windows.Forms.DataGridViewCell In row.Cells
 
        'COMPROBAMOS QUE LA CELDA NO SEA NULA, SI PERMITIMOS AÑADIR FILAS ESTO ES MUY IMPORTANTE
 
        If celda.Value IsNot Nothing Then
 
            texto += "   " & celda.Value.ToString() & "      "
 
        End If
 
    Next
 
    'CALCULAMOS LA POSICIÓN EN QUE SE ESCRIBE LA LÍNEA
 
    yPos = topmargin + (count * printFont.GetHeight(e.Graphics))
 
    'ESCRIBIMOS LA LINEA CON EL OBJETO GRAPHICS
 
    e.Graphics.DrawString(texto, printFont, System.Drawing.Brushes.Black, 10, yPos)
 
    'INCREMENTAMOS LOS CONTADORES
 
    count += 1
    i += 1
 
End While

Por favor, envia me el codigo actual (con las ajustadas que propongo).

Voy me durante un rato, miraré cuando vuelvo.
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar
sin imagen de perfil
Val: 229
Ha disminuido 1 puesto en Visual Basic.NET (en relación al último mes)
Gráfica de Visual Basic.NET

Como puedo imprimir fecha sin hora

Publicado por José Vicente (113 intervenciones) el 15/09/2020 15:09:12
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
Private Sub PrintDocument1_PrintPage(sender As Object, e As Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
 
        'DEFINIMOS LA FUENTE QUE VAMOS A USAR PARA IMPRIMIR
 
        Dim printFont As System.Drawing.Font = New Drawing.Font("Verdana", 10, FontStyle.Regular)
        Dim topmargin As Double = e.MarginBounds.Top
        Dim yPos As Double = 0
        Dim linesPerPage As Double = 0
        Dim count As Integer = 0
        Dim texto As String = ""
        Dim row As System.Windows.Forms.DataGridViewRow
 
        'CALCULAMOS EL NÚMERO DE LÍNEAS QUE CABEN EN CADA PÁGINA
 
        linesPerPage = e.MarginBounds.Height / printFont.GetHeight(e.Graphics)
 
        'IMPRIMIMOS LAS CABECERAS
 
        Dim header As DataGridViewHeaderCell
 
        For Each column As DataGridViewColumn In DataGridView1.Columns
 
            header = column.HeaderCell
            texto += "    " & header.FormattedValue.ToString() & "       "
 
        Next
 
        yPos = topmargin + (count * printFont.GetHeight(e.Graphics))
        e.Graphics.DrawString(texto, printFont, System.Drawing.Brushes.Black, 10, yPos)
 
        'DEJAMOS UNA LÍNEA DE SEPARACIÓN
 
        count += 2
 
        'RECORREMOS EL DATAGRIDVIEW HASTA LLEGAR AL TOPE DE LÍNEAS DE LA PÁGINA O EL FINAL DEL DATAGRIDVIEW
 
        While count < linesPerPage AndAlso i < DataGridView1.Rows.Count
 
            row = DataGridView1.Rows(i)
 
            texto = ""
 
            For Each celda As System.Windows.Forms.DataGridViewCell In row.Cells
 
                'COMPROBAMOS QUE LA CELDA NO SEA NULA, SI PERMITIMOS AÑADIR FILAS ESTO ES MUY IMPORTANTE
 
                If celda.Value IsNot Nothing Then
 
                    DataGridView1.Rows(i).Cells(0).Value = DataGridView1.Rows(i).Cells(0).Value.ToShortDateString()
 
                    texto += "   " & celda.Value.ToString() & "      "
 
                End If
 
            Next
 
            'CALCULAMOS LA POSICIÓN EN QUE SE ESCRIBE LA LÍNEA
 
            yPos = topmargin + (count * printFont.GetHeight(e.Graphics))
 
            'ESCRIBIMOS LA LINEA CON EL OBJETO GRAPHICS
 
            e.Graphics.DrawString(texto, printFont, System.Drawing.Brushes.Black, 10, yPos)
 
            'INCREMENTAMOS LOS CONTADORES
 
            count += 1
            i += 1
 
        End While
 
        'COMPROBAMOS SI QUEDAN MÁS FILAS POR IMPRIMIR, SI ES ASÍ SALDRÁN EN LA SIGUIENTE PÁGINA
 
        If i < DataGridView1.Rows.Count Then
 
            e.HasMorePages = True
 
        Else
 
            'SI LLEGAMOS AL FINAL DE HASMOREPAGES SE ESTABLECE A FALSE PRA DEJAR DE IMPRIMIR
 
            e.HasMorePages = False
 
            'HAY QUE PONER EL CONTADOR A 0 PARA QUE NO SE QUEDE PILLADA LA VARIABLE i Y NO IMPRIMA NADA
 
            i = 0
 
        End If
 
    End Sub
Aquí tienes el código completo, igual no lo estoy haciendo bien
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar
Imágen de perfil de Phil Rob
Val: 3.353
Oro
Ha mantenido su posición en Visual Basic.NET (en relación al último mes)
Gráfica de Visual Basic.NET

Como puedo imprimir fecha sin hora

Publicado por Phil Rob (1546 intervenciones) el 15/09/2020 16:01:03
Gracias ...

Supongo que de este manera, el valor es bien mostrada en el DataGridView en la pantalla.
Si es bien el caso, la modificación siguiente debería ser utile :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
For Each celda As System.Windows.Forms.DataGridViewCell In row.Cells
 
    'COMPROBAMOS QUE LA CELDA NO SEA NULA, SI PERMITIMOS AÑADIR FILAS ESTO ES MUY IMPORTANTE
 
    If celda.Value IsNot Nothing Then
 
        DataGridView1.Rows(i).Cells(0).Value = DataGridView1.Rows(i).Cells(0).Value.ToShortDateString()
 
        If celda.ColumnIndex = 0 Then
            texto += "   " & celda.Value.ToShortDateString() & "      "
        Else
            texto += "   " & celda.Value.ToString() & "      "
        End If
 
    End If

Comenta me ...
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar
Imágen de perfil de Phil Rob
Val: 3.353
Oro
Ha mantenido su posición en Visual Basic.NET (en relación al último mes)
Gráfica de Visual Basic.NET

Como puedo imprimir fecha sin hora

Publicado por Phil Rob (1546 intervenciones) el 15/09/2020 16:32:50
Continuación ...

Por favor, olvida mi mensaje anterior.
Este el código que quieras :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
For Each celda As System.Windows.Forms.DataGridViewCell In row.Cells
 
    'COMPROBAMOS QUE LA CELDA NO SEA NULA, SI PERMITIMOS AÑADIR FILAS ESTO ES MUY IMPORTANTE
 
    If celda.Value IsNot Nothing Then
 
      ' ***   '  DataGridView1.Rows(i).Cells(0).Value = DataGridView1.Rows(i).Cells(0).Value.ToShortDateString ''' No es posible este frasea !!!
 
        If celda.ColumnIndex = 0 Then
            Dim Fecha As Date
            Fecha = CType(celda.Value, Date)
            texto += "   " & Fecha.ToShortDateString() & "      "
        Else
            texto += "   " & celda.Value.ToString() & "      "
        End If
 
    End If
 
Next

Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar
sin imagen de perfil
Val: 229
Ha disminuido 1 puesto en Visual Basic.NET (en relación al último mes)
Gráfica de Visual Basic.NET

Como puedo imprimir fecha sin hora

Publicado por José Vicente (113 intervenciones) el 15/09/2020 16:34:59
Gracias, ahora sólo me queda centrar las columnas.
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar
Imágen de perfil de Phil Rob
Val: 3.353
Oro
Ha mantenido su posición en Visual Basic.NET (en relación al último mes)
Gráfica de Visual Basic.NET

Como puedo imprimir fecha sin hora

Publicado por Phil Rob (1546 intervenciones) el 15/09/2020 17:05:09
Este es mas difícil ...

Tienes saber la tamaña del dato (en con la propiedad With de cada carácter) y calcular la posición del rectángulo para imprimir ...

Prefiero trabajar con un documento inicial de tipo Word y de lo llenar y imprimir con VB. El formato es hecho en Word. No sé si puedes ...

Mira los vidéos :

Hacer el documento inicial con Word : https://www.dropbox.com/s/4k9ctppelzccht0/PonerCamposYTablas.mp4?dl=0

Imprimir con VB : https://www.dropbox.com/s/ynxpcdc4nwazjvi/Ejecucion.mp4?dl=0

Este es el código para imprimir (puedo te enviar el proyecto de prueba si quieres) :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#Region "Impresion"
 
    Dim ArchivoPrimeroParaImpresion As String = CurDir() & "\DocPrimero.docx"
 
    Private Sub BImprimir_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BImprimir.Click
 
        GoToWord()
 
    End Sub
 
    Private Sub GoToWord()
        Dim WAPP As New Microsoft.Office.Interop.Word.Application
        Dim UnDoc As New Microsoft.Office.Interop.Word.Document
        Dim Table As Microsoft.Office.Interop.Word.Table
 
        ' Trabajar con  un duplicado (FImpresion) del archivo para no cambiar el original
        Dim Punto As Integer
        Punto = ArchivoPrimeroParaImpresion.LastIndexOf(".")
        Dim FImpresion As String = ArchivoPrimeroParaImpresion.Substring(0, Punto) & "_" & Trim(Now.Millisecond) & ".Docx"
        System.IO.File.Copy(ArchivoPrimeroParaImpresion, FImpresion)
 
        ' Abrir el archivo FImpresion y tratar
        UnDoc = WAPP.Documents.Open(FImpresion)
        WAPP.Visible = True
 
        ' ***  Llenar el documento (ejemplos)
 
        Table = UnDoc.Tables(1)  ' Las tablas son indiciado de 1 a N, si N tablas
        ' En una tabla, los datos son indicado por sus lineas y columnas :
        ' Table.Cell(Linea, Columna).Range.Text = ...     ' Linea de 1 a N lineas y Columna de 1 a N columnas
        ' Si una sola celda, este es linea 1 y columna 1
        Table.Cell(1, 1).Range.Text = "TITULO del " & Now.ToShortDateString
 
        ' Un Bookmarks es designado por su nombre
        UnDoc.Bookmarks("MiBookMarks01").Range.Text = "El marca no es hecho para esta, mejor son los campos de fusión y las tablas"
 
        ' Los campos de fusion son indiciado de 1 à N, si N campos
        For F As Integer = 1 To UnDoc.Fields.Count
            UnDoc.Fields(F).Result.Text = "Texto para MiCampos" & String.Format("{0:00}", F)
        Next
 
        Table = UnDoc.Tables(2)
 
        ' Escribir y formatear (mejor es de bien preparar el documento inicial) los titulos de las columnas
 
        For C As Integer = 0 To DGVProba.ColumnCount - 1
            Table.Cell(1, C + 1).Range.Text = DGVProba.Columns(C).HeaderText
            Table.Cell(1, C + 1).Range.Bold = True
        Next
 
        ' Escribir las lineas de la tabla
        For R As Integer = 0 To DGVProba.RowCount - 2
            For C As Integer = 0 To DGVProba.ColumnCount - 1
 
 
                Table.Cell(R + 2, C + 1).Range.Text = DGVProba.Item(C, R).Value.ToString
 
                Table.Cell(R + 2, 1).Select()
                If DGVProba.Item(1, R).Value.ToString = String.Empty Then
 
                    WAPP.Selection.Font.Bold = True
                Else
                    WAPP.Selection.Font.Bold = False
                End If
 
            Next
            If R < DGVProba.RowCount - 2 Then
                Table.Rows.Add()
            End If
        Next
 
        'For R As Integer = 0 To DGVProba.RowCount - 2
        '    If DGVProba.Item(1, R).Value.ToString = String.Empty Then
        '        Table.Cell(R + 2, 1).Select()
        '        WAPP.Selection.Font.Bold = True
        '    End If
        'Next
 
        Table = UnDoc.Tables(3)
 
        ' este es el mismo trabajar para la ultima tabla, pero menos una columna
 
        For C As Integer = 1 To DGVProba.ColumnCount - 1  ' Sin Cuantidad
            Table.Cell(1, C).Range.Text = DGVProba.Columns(C).HeaderText  ' Atencion a el indicio de la columna
            Table.Cell(1, C).Range.Italic = True
        Next
 
        ' Escribir las lineas de la tabla
        For R As Integer = 0 To DGVProba.RowCount - 2
            For C As Integer = 1 To DGVProba.ColumnCount - 1 ' Sin Cuantidad
                Table.Cell(R + 2, C).Range.Text = DGVProba.Item(C, R).Value.ToString  ' Atencion a el indicio de la columna
            Next
            If R < DGVProba.RowCount - 2 Then
                Table.Rows.Add()
            End If
        Next
 
        ' Con el Preview, es posible modificar un detalle y eligar la impresora
        WAPP.PrintPreview = True
        WAPP.ShowMe()
    End Sub
#End Region

...
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar
sin imagen de perfil
Val: 229
Ha disminuido 1 puesto en Visual Basic.NET (en relación al último mes)
Gráfica de Visual Basic.NET

Como puedo imprimir fecha sin hora

Publicado por José Vicente (113 intervenciones) el 16/09/2020 08:54:41
Gracias. Je vais essayer, merci beaucoup. Mon français est un petit peu rouillé.
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar
Imágen de perfil de Phil Rob
Val: 3.353
Oro
Ha mantenido su posición en Visual Basic.NET (en relación al último mes)
Gráfica de Visual Basic.NET

Como puedo imprimir fecha sin hora

Publicado por Phil Rob (1546 intervenciones) el 16/09/2020 09:45:41
"Gracias. Je vais essayer, merci beaucoup. Mon français est un petit peu rouillé." ...
En tout cas, cette phrase est parfaite !
Très bonne journée à toi,
Phil
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar