Buonasera a tutti, avrei questo piccolo programma da realizzare con Visual Studio 2022
Nella mia form ho un datagridview che visualizza gli elementi della tabella, però vorrei che una tabella diventasse una combobox e prendessi i riferimenti da un'altra colonna di un'altra tabella… spero di essermi spiegato, vi lascio il codice. Vorrei visualizzare le opzioni dell altra tabella, cosiche se nell'altra tabella si aggiornano i dati in automatico si aggiorna la combobox.
Imports System.ComponentModel
Imports System.Runtime.CompilerServices
Imports System.Web.UI.WebControls
Imports System.Windows.Controls
Imports System.Windows.Media
Imports System.Windows.Media.TextFormatting
Imports System.Windows.Navigation
Imports Microsoft.Office.Interop.Access
Imports Newtonsoft.Json.Converters
Imports Opc.UaFx
Imports Org.BouncyCastle.Asn1.Cms
Public Class frmMenuConai
Dim m_dtTransc As New DataTable
Dim m_da As SqlClient.SqlDataAdapter
Dim m_cb As SqlClient.SqlCommandBuilder
Dim cod_fascia As New DataGridViewComboBoxColumn
Private Sub btnsomma_Click(sender As Object, e As EventArgs) Handles btnsomma.Click
Try
Dim som As New SqlClient.SqlDataAdapter("Select SUM(Coeff) Somma_totale from dbole.dbo.Donai", dbole)
If dbole.State <> ConnectionState.Open Then ' Apertura connessione al database, nel caso non ci fosse stato la connesione del database
dbole.Open()
End If
Dim dt As New DataTable ' aggiunta dati sommati alla tabella
som.Fill(dt)
DataGridViewconai.DataSource = dt
txtsomma.Text = DataGridViewconai.CurrentCell.Value.ToString() ' conversione del dato sommato in un datattable per poterlo convertire in un textbox
Catch ex As Exception
MessageBox.Show(ex.ToString, "frmMenuConai.Aggiorna", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub
Private Sub btncercaitem_click(sender As Object, e As EventArgs) Handles btncercaitem.Click
Try
Dim da As New SqlClient.SqlDataAdapter("select * from dbole.dbo.Donai where ItemCode = '" & txtCerca.Text & "'", dbole)
If dbole.State <> ConnectionState.Open Then
dbole.Open()
End If
Dim dt As New DataTable
da.Fill(dt)
DataGridViewconai.DataSource = dt
Catch ex As Exception
MessageBox.Show(ex.ToString, "frmMenuDonai.Aggiorna", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub
Private Sub btncercacoef_Click(sender As Object, e As EventArgs) Handles btncercacoef.Click
Try
Dim q As New SqlClient.SqlDataAdapter("Select * from dbole.dbo.Donai where Coeff='" & txtCerca.Text & "'", dbole)
If dbole.State <> ConnectionState.Open Then
dbole.Open() 'stabilire la connesione
End If
Dim f As New DataTable
q.Fill(f) '' aggiunge dato al datagridview
DataGridViewconai.DataSource = f ' visualizzzazione dei dati inderiti per la ricerca e visualizzzati nel datagridview della tabella nella from
Catch ex As Exception
MessageBox.Show(ex.ToString, "frmMenuConai.Aggiorna", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub
Private Sub btnuscita_Click(sender As Object, e As EventArgs) Handles btnuscita.Click
Me.Hide()
End Sub
Private Sub btncercacodfascia_Click(sender As Object, e As EventArgs) Handles btncercacodfascia.Click
Try
Dim q As New SqlClient.SqlDataAdapter("Select * from dbole.dbo.Donai where CodFascia= '" & txtCerca.Text & "'", dbole)
If dbole.State <> ConnectionState.Open Then
dbole.Open()
End If
Dim f As New DataTable
q.Fill(f)
DataGridViewconai.DataSource = f
Catch ex As Exception
MessageBox.Show(ex.ToString, "frmMenuBonai.Aggiorna", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub
Private Sub frmMenuDonai_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Try
Aggiorna()
Catch ex As Exception
MessageBox.Show(ex.Message & " " & (New System.Diagnostics.StackTrace(ex, True)).ToString, Me.Name & " - " & (New System.Diagnostics.StackFrame()).GetMethod().Name, MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub
Public Sub Salva()
Try
m_da.Update(m_dtTransc)
m_dtTransc.AcceptChanges()
Catch ex As Exception
MessageBox.Show(ex.Message & " " & (New System.Diagnostics.StackTrace(ex, True)).ToString, Me.Name & " - " & (New System.Diagnostics.StackFrame()).GetMethod().Name, MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
MsgBox("Salvataggio comletato", vbExclamation)
End Sub
Public Sub Aggiorna()
Try
If Not IsNothing(m_dtTransc.GetChanges()) Then
Select Case MessageBox.Show("Salvare i dati?", "", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question)
Case Windows.Forms.DialogResult.Yes
Salva()
Case Windows.Forms.DialogResult.No
Case Windows.Forms.DialogResult.Cancel
Exit Sub
End Select
End If
m_dtTransc.Rows.Clear()
m_da = Nothing
m_da = New SqlClient.SqlDataAdapter("select * from dbole.dbo.Donai ItemCode ", dbole)
m_da.Fill(m_dtTransc)
m_cb = Nothing
m_cb = New SqlClient.SqlCommandBuilder(m_da)
m_cb.GetDeleteCommand()
m_cb.GetUpdateCommand()
m_cb.GetInsertCommand()
DataGridViewconai.DataSource = Nothing
DataGridViewconai.DataSource = m_dtTransc
Catch ex As Exception
MessageBox.Show(ex.Message & " " & (New System.Diagnostics.StackTrace(ex, True)).ToString, Me.Name & " - " & (New System.Diagnostics.StackFrame()).GetMethod().Name, MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub
End Class