martes, 6 de diciembre de 2011

VB.NET Funcion para cargar un datagridview

Esta función nos permite cargar un datagridview con el resultado de una búsqueda en base de datos.

'Funcion para cargar los registros consultados
Public Sub loadRegistros(ByVal mySQL As String, ByVal grView As DataGridView, Optional ByVal Check As Boolean = False)
Dim dt As New DataTable
Dim da As New Odbc.OdbcDataAdapter
Dim cmd As New Odbc.OdbcCommand
Try
Cursor.Current = Cursors.WaitCursor
cmd.Connection = conn
cmd.CommandText = mySQL
cmd.CommandType = CommandType.Text
da.SelectCommand = cmd
da.Fill(dt)
grView.DataSource = dt
If Check = True Then
If grView.Columns(0).Name = ":::" Then grView.Columns.Remove(":::")
Dim column As New DataGridViewCheckBoxColumn()
With column
.Name = ":::"
.HeaderText = .Name
.AutoSizeMode = DataGridViewAutoSizeColumnMode.NotSet
.FlatStyle = FlatStyle.Standard
.CellTemplate = New DataGridViewCheckBoxCell()
'.ReadOnly = False
End With
grView.Columns.Insert(0, column)
grView.Columns(0).ReadOnly = True
End If
grView.AlternatingRowsDefaultCellStyle.BackColor = Color.LightGray
For i As Integer = 0 To grView.ColumnCount - 1
grView.AutoResizeColumn(i)
Next
grView.Refresh()
Cursor.Current = Cursors.Default
Catch ex As Exception
MsgBox(ex.Message)
Finally
cmd.Dispose()
End Try
End Sub

No hay comentarios:

Publicar un comentario