MySQL + VB.NET - TabControl 和多个 DataGridview

发布于 2024-12-12 01:01:04 字数 7398 浏览 0 评论 0原文

我的 DataGridViews 有问题,实际上我有一个带有 TabControl 的表单,有 8 个选项卡,每个选项卡都有一个 DataGridView 控件。我在表单加载时填充它们,问题是我在这里受到了某种限制,只有 3 个 DataGridView 被填充,而不是 3 个特定的,而是我在代码中首先放置的任何 3 个。我手动填充它们,执行 mysql 查询,填充 DataTable,然后使数据源等于 DataTable。

有什么想法吗?

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

I have a problem with my DataGridViews, actually i have a form with a TabControl with 8 tabs, each tab have a DataGridView control. I'm populating them when the form load, the problem is that i got kind of a limit here, only 3 DataGridViews are getting populated, not 3 specific ones but any 3 i put first on my code. I'm populating them manually, doing a mysql query, filling a DataTable and then making the data source equal to the DataTable.

Any ideas?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

牵强ㄟ 2024-12-19 01:01:04

有很多重复的代码可以重构。但是,我认为根本原因是您没有在每个 FillX() 方法中重新初始化 myCommandmyAdapter 变量。因此,我首先将 connmyCommandmyAdapterSQL 变量设置为每个 的本地变量>FillX() 方法。

然后我会重构代码以应用 DRY(不要重复自己)原则并消除重复的代码。

There is a lot of code duplication that you can refactor. But, I think the root cause is that you are not reinitializing myCommand and myAdapter variables in each FillX() method. So, I would start by making the conn, myCommand, myAdapter, and SQL variables local to each FillX() method.

Then I would refactor the code to apply the DRY (Don't Repeat Yourself) principle and eliminate duplicate code.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文