私は自分のDataGridViewに問題があります。実際には8つのタブを持つTabControlを持つフォームがあり、各タブにはDataGridViewコントロールがあります。フォームの読み込み時に問題が発生します.3つのDataGridViewだけが3つの特定のものではなく、3つのコードが最初に配置されます。私はそれらを手動で設定し、mysqlクエリを実行して、DataTableを充填してから、データソースをDataTableと等しくします。MySQL + VB.NET - TabControlと複数のDataGridviews
アイデア?
Imports MySql.Data.MySqlClient
Imports System.Data
Public Class frmMantenimiento
Dim conn As MySqlConnection
Dim myCommand As New MySqlCommand
Dim myAdapter As New MySqlDataAdapter
Dim dataEmp As New DataTable
Dim dataNomi As New DataTable
Dim dataPres As New DataTable
Dim dataGast As New DataTable
Dim dataAmon As New DataTable
Dim dataHora As New DataTable
Dim SQL As String
Public Sub FillEmps()
conn = New MySqlConnection("Data Source=localhost; user id=root; password=; database=arj;")
myCommand.Connection = conn
SQL = "SELECT IDEmpleado as ID, Nombre, Cargo FROM Empleados"
Try
conn.Open()
Try
myCommand.Connection = conn
myCommand.CommandText = SQL
myAdapter.SelectCommand = myCommand
myAdapter.Fill(dataEmp)
dgvConsEmp.DataSource = dataEmp
dgvConsEmp.Columns(0).Width = 30
dgvConsEmp.Columns(1).Width = 200
dgvConsEmp.Columns(2).Width = 200
Catch myerror As MySqlException
MsgBox("Ocurrió un error leyendo la base de datos: " & myerror.Message)
End Try
Catch myerror As MySqlException
MessageBox.Show("Ocurrió un error conectando a la base de datos: " & myerror.Message)
Finally
If conn.State <> ConnectionState.Closed Then conn.Close()
End Try
End Sub
Public Sub FillNom()
conn = New MySqlConnection("Data Source=localhost; user id=root; password=; database=arj;")
myCommand.Connection = conn
SQL = String.Empty
SQL = "SELECT IDNomina as ID, Fecha, MontoBruto as 'Monto Bruto', MontoNeto as 'Monto Neto', CantidadEmpleados as Empleados FROM Nomina"
Try
conn.Open()
Try
myCommand.Connection = conn
myCommand.CommandText = SQL
myAdapter.SelectCommand = myCommand
myAdapter.Fill(dataNomi)
dgvConsNomi.DataSource = dataNomi
dgvConsNomi.Columns(0).Width = 30
Catch myerror As MySqlException
MsgBox("Ocurrió un error leyendo la base de datos: " & myerror.Message)
End Try
Catch myerror As MySqlException
MessageBox.Show("Ocurrió un error conectando a la base de datos: " & myerror.Message)
Finally
If conn.State <> ConnectionState.Closed Then conn.Close()
End Try
End Sub
Public Sub FillPres()
conn = New MySqlConnection("Data Source=localhost; user id=root; password=; database=arj;")
myCommand.Connection = conn
SQL = String.Empty
SQL = "SELECT IDPrestamo as ID, IDEmpleado as 'ID Empleado', MontoInicial as Monto, Fecha, MontoDescuento as Descuento, FormaDescuento as Recurrencia, Status FROM Prestamos"
Try
conn.Open()
Try
myCommand.Connection = conn
myCommand.CommandText = SQL
myAdapter.SelectCommand = myCommand
myAdapter.Fill(dataPres)
dgvConsPres.DataSource = dataPres
dgvConsPres.Columns(0).Width = 30
Catch myerror As MySqlException
MsgBox("Ocurrió un error leyendo la base de datos: " & myerror.Message)
End Try
Catch myerror As MySqlException
MessageBox.Show("Ocurrió un error conectando a la base de datos: " & myerror.Message)
Finally
If conn.State <> ConnectionState.Closed Then conn.Close()
End Try
End Sub
Public Sub FillGast()
conn = New MySqlConnection("Data Source=localhost; user id=root; password=; database=arj;")
myCommand.Connection = conn
SQL = String.Empty
SQL = "SELECT IDGasto as ID, IDEmpleado as 'ID Empleado', Monto, Fecha, Concepto, IDUsuario as Creador FROM GastosEmpleados"
Try
conn.Open()
Try
myCommand.Connection = conn
myCommand.CommandText = SQL
myAdapter.SelectCommand = myCommand
myAdapter.Fill(dataGast)
Me.dgvConsGast.DataSource = dataGast
Me.dgvConsGast.Columns(0).Width = 30
Catch myerror As MySqlException
MsgBox("Ocurrió un error leyendo la base de datos: " & myerror.Message)
End Try
Catch myerror As MySqlException
MessageBox.Show("Ocurrió un error conectando a la base de datos: " & myerror.Message)
Finally
If conn.State <> ConnectionState.Closed Then conn.Close()
End Try
End Sub
Public Sub FillAmon()
conn = New MySqlConnection("Data Source=localhost; user id=root; password=; database=arj;")
myCommand.Connection = conn
SQL = "SELECT IDAmonestacion as ID, TipoAmonestacion as Tipo, Descripcion, IDUsuario as Creador FROM Amonestaciones"
Try
conn.Open()
Try
myCommand.Connection = conn
myCommand.CommandText = SQL
myAdapter.SelectCommand = myCommand
myAdapter.Fill(dataAmon)
dgvConsAmon.DataSource = dataAmon
dgvConsAmon.Columns(0).Width = 30
Catch myerror As MySqlException
MsgBox("Ocurrió un error leyendo la base de datos: " & myerror.Message)
End Try
Catch myerror As MySqlException
MessageBox.Show("Ocurrió un error conectando a la base de datos: " & myerror.Message)
Finally
If conn.State <> ConnectionState.Closed Then conn.Close()
End Try
End Sub
Public Sub FillHora()
conn = New MySqlConnection("Data Source=localhost; user id=root; password=; database=arj;")
myCommand.Connection = conn
SQL = "SELECT IDControlHorario as ID, IDEmpleado as 'ID Empleado', Fecha, Hora FROM ControlHorario"
Try
conn.Open()
Try
myCommand.Connection = conn
myCommand.CommandText = SQL
myAdapter.SelectCommand = myCommand
myAdapter.Fill(dataHora)
dgvConsHora.DataSource = dataHora
dgvConsHora.Columns(0).Width = 30
Catch myerror As MySqlException
MsgBox("Ocurrió un error leyendo la base de datos: " & myerror.Message)
End Try
Catch myerror As MySqlException
MessageBox.Show("Ocurrió un error conectando a la base de datos: " & myerror.Message)
Finally
If conn.State <> ConnectionState.Closed Then conn.Close()
End Try
End Sub
Private Sub frmMantenimiento_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
FillEmps()
FillNom()
FillPres()
FillGast()
FillAmon()
FillHora()
End Sub
コードを掲載してください。 – Xint0
もちろん、追加されました。 –