Agregar Columnas A Un Datagridview
Agregar Columnas A Un Datagridview
Agregar Columnas A Un Datagridview
Hola a todos...
A continuacin tenemos un ejemplo que permitir personalizar
las columnas del datagridview.
Imagen
******
Cdigo fuente
************
Paso 1:
******
'Configuramos el nuevo tamao de las columna al momento de cargar el formulario.
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) H
andles MyBase.Load
' Habilitar el cambio de tamao en los encabezados de columna
DataGridView1.ColumnHeadersHeightSizeMode = _
DataGridViewColumnHeadersHeightSizeMode.EnableResizing
'Ajustamos el alto para la columna cabecera
DataGridView1.ColumnHeadersHeight = _
DataGridView1.ColumnHeadersHeight * 2
' Ajuste la alineacin del texto en los encabezados de las
Paso 2:
******
r1.Y += 1
r1.Width = r1.Width * 2 - 2
r1.Height = r1.Height / 2 - 2
Using br As SolidBrush = _
New SolidBrush(DataGridView1.ColumnHeadersDefaultCellStyle.BackColor)
e.Graphics.FillRectangle(br, r1)
End Using
Using p As Pen = New Pen(SystemColors.InactiveBorder)
e.Graphics.DrawLine(p, r1.X, r1.Bottom, r1.Right, r1.Bottom)
End Using
Using format As StringFormat = New StringFormat()
Using br As SolidBrush = New SolidBrush(DataGridView1.ColumnHeadersDefaultCell
Style.ForeColor)
format.Alignment = StringAlignment.Center
format.LineAlignment = StringAlignment.Center
e.Graphics.DrawString(Grupos(j / 2),
DataGridView1.ColumnHeadersDefaultCellStyle.Font, _
br, r1, format)
End Using
End Using
Next
End Sub
Paso 3:
******
'Dibujamos las celdas
Private Sub DataGridView1_CellPainting(ByVal sender As System.Object, ByVal e As Sys
tem.Windows.Forms.DataGridViewCellPaintingEventArgs) Handles DataGridView1.CellPaint
ing
If e.RowIndex = -1 AndAlso e.ColumnIndex > -1 Then
e.PaintBackground(e.CellBounds, False)
Dim r2 As Rectangle = e.CellBounds
r2.Y += e.CellBounds.Height / 2
r2.Height = e.CellBounds.Height / 2
e.PaintContent(r2)
e.Handled = True
End If
End Sub
Fuentes de Informacin
*******************
2
3
.Columns.Add("", "Gan")
.Columns.Add("", "Perdi")
.Columns.Add("", "Gan")
.Columns.Add("", "Perdi")
8
9
.Columns.Add("", "Gan")
.Columns.Add("", "Perdi")
10
.ColumnHeadersHeightSizeMode =
11DataGridViewColumnHeadersHeightSizeMode.EnableResizing
12
.ColumnHeadersHeight = .ColumnHeadersHeight * 2
.ColumnHeadersDefaultCellStyle.Alignment =
DataGridViewContentAlignment.BottomCenter
13
14
End With
15
End Sub
2
3
Dim j As Integer = 0
While j < 6
Dim r1 As Rectangle =
r1.X += 1
r1.Y += 1
r1.Width = r1.Width * 2 - 2
9
10
r1.Height = r1.Height / 2 - 2
11
e.Graphics.FillRectangle(New
12SolidBrush(Me.DataGridView1.ColumnHeadersDefaultCellStyle.BackColor),
r1)
13
14
15
16
17
format.Alignment = StringAlignment.Center
format.LineAlignment = StringAlignment.Center
18
e.Graphics.DrawString(monthes(j \ 2),
Me.DataGridView1.ColumnHeadersDefaultCellStyle.Font,
_
19
New
20SolidBrush(Me.DataGridView1.ColumnHeadersDefaultCellStyle.ForeColor),
21r1, format)
22
j += 2
End While
End Sub