0

I'm trying to get selected cell value of multi DataGridView in TabControl to main datagridview in form1 with VB.NET

Please Guide me

for example when I selected row in the Criteria1 tabcontrol with enter 2 times then the value is in the selected row in the DatagridView column (Criteria1,DesCriteria1) in form1

Form1 datagridview with selected row

CodeProduct Barcode Criteria1 DesCriteria1
1000 01-1000

Form2 datagridview in tabcontrol Criteria1 with selected row then press keypress in keyboard with enter 2 times

Criteria1 DesCriteria1
B BLACK
G GREEN

The cell value from Form2 datagridview in tabcontrol Criteria1 Appears in the DatagridView form1

CodeProduct Barcode Criteria1 DesCriteria1
1000 01-1000 B BLACK

and also the same way for other tabcontrol criteria or other fields

Thanks

Code in Form1

Public Class Form1
    Public _myTable As New DataTable()
    Protected Overrides Sub OnLoad(e As EventArgs)
        MyBase.OnLoad(e)
        _myTable.Columns.AddRange({
        New DataColumn("CodeProduct", GetType(String)),
        New DataColumn("Barcode", GetType(String)),
        New DataColumn("Criteria1", GetType(String)),
        New DataColumn("DesCriteria1", GetType(String)),
        New DataColumn("Criteria2", GetType(String)),
        New DataColumn("DesCriteria2", GetType(String)),
        New DataColumn("Criteria3", GetType(Integer)),
        New DataColumn("DesCriteria3", GetType(Integer)),
        New DataColumn("Criteria4", GetType(String)),
        New DataColumn("DesCriteria4", GetType(String))})
        DataGridView1.Columns("Codeproduct").ReadOnly = False
        DataGridView1.Columns("Barcode").ReadOnly = True
        DataGridView1.Columns("Criteria1").ReadOnly = False
        DataGridView1.Columns("DesCriteria1").ReadOnly = True
        DataGridView1.Columns("Criteria2").ReadOnly = False
        DataGridView1.Columns("DesCriteria2").ReadOnly = True
        DataGridView1.Columns("Criteria3").ReadOnly = False
        DataGridView1.Columns("DesCriteria3").ReadOnly = True
        DataGridView1.Columns("Criteria4").ReadOnly = False
        DataGridView1.Columns("DesCriteria4").ReadOnly = True

        For Each column As DataGridViewColumn In DataGridView1.Columns
            column.SortMode = DataGridViewColumnSortMode.NotSortable
        Next column
    End Sub
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        If DataGridView1.RowCount <= 0 Then
            MessageBox.Show("No data to select from", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information)
            Return
        End If
        If DataGridView1.SelectedCells.Count > 1 Then
            Using frm = New Form2()
                If frm.ShowDialog() = DialogResult.OK Then
                End If
            End Using
        Else
            MessageBox.Show("Please select a row", "Message", MessageBoxButtons.OK, MessageBoxIcon.Warning)
        End If
    End Sub

    Private Sub dataGridView1_CellValueChanged(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellValueChanged
        If e.RowIndex < 0 OrElse e.RowIndex < 0 Then Return
        Dim dgv = DirectCast(sender, DataGridView)
        'If Not dgv.IsCurrentCellDirty Then Return
        Dim row = dgv.Rows(e.RowIndex)
        DirectCast(row.DataBoundItem, DataRowView)?.EndEdit()
        Dim arrQueries() = {
            New With {
            .KeyCell = row.Cells("CODEPRODUCT"),
            .ValueCell = row.Cells("BARCODE"),
            .Table = "PRODUCTS"},
            New With {
                .KeyCell = row.Cells("CRITERIA1"),
                .ValueCell = row.Cells("DESCRITERIA1"),
                .Table = "CRITERIA1"},
            New With {
                .KeyCell = row.Cells("CRITERIA2"),
                .ValueCell = row.Cells("DESCRITERIA2"),
                .Table = "CRITERIA2"},
            New With {
                .KeyCell = row.Cells("CRITERIA3"),
                .ValueCell = row.Cells("DESCRITERIA3"),
                .Table = "CRITERIA3"},
            New With {
                .KeyCell = row.Cells("CRITERIA4"),
                .ValueCell = row.Cells("DESCRITERIA4"),
                .Table = "CRITERIA4"}
        }

        If Not arrQueries.Any(
            Function(q) q.KeyCell.OwningColumn Is dgv.Columns(e.ColumnIndex)) Then
            Return
        End If
        Using con = New OleDbConnection(GetOledbConnectionString()), cmd = con.CreateCommand()
            con.Open()
            For Each q In arrQueries
                If q.KeyCell.Value IsNot Nothing AndAlso
                    q.KeyCell.Value IsNot DBNull.Value AndAlso
                    Not String.IsNullOrEmpty(q.KeyCell.Value.ToString()) Then
                    cmd.CommandText = String.Format(
                        "SELECT {0} FROM {1} WHERE {2} = ?",
                        q.ValueCell.OwningColumn.Name, q.Table, q.KeyCell.OwningColumn.Name)
                    cmd.Parameters.Clear()
                    cmd.Parameters.Add("?", OleDbType.VarWChar).Value = q.KeyCell.Value
                    q.ValueCell.Value = cmd.ExecuteScalar()
                    'If q.ValueCell Is DBNull.Value Then q.KeyCell.Value = Nothing

                Else
                    q.ValueCell.Value = Nothing
                End If
            Next
        End Using
    End Sub

End Class

Public Module DbContext
    Public Function GetOledbConnectionString() As String
        Return "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\WAREHOUSE.accdb;Persist Security Info=False;"
    End Function
End Module
Public Class PRODUCTS
    Public Property CODEPRODUCT() As String
    Public Property BARCODE() As String
End Class
Public Class CRITERIA1
    Public Property CRITERIA1() As String
    Public Property DESCRITERIA1() As String
End Class
Public Class CRITERIA2
    Public Property CRITERIA2() As String
    Public Property DESCRITERIA2() As String
End Class
Public Class CRITERIA3
    Public Property CRITERIA3() As String
    Public Property DESCRITERIA3() As String
End Class
Public Class CRITERIA4
    Public Property CRITERIA4() As String
    Public Property DESCRITERIA4() As String
End Class
Public Class CRITERIA5
    Public Property CRITERIA5() As String
    Public Property DESCRITERIA5() As String
End Class
Public Class ALLService
    Private ReadOnly _conn As OleDbConnection
    Private _connectionString As String = GetOledbConnectionString()
    Public Sub New()
        _conn = New OleDbConnection(_connectionString)
    End Sub
    Public Function GetPRODUCTS() As IEnumerable(Of PRODUCTS)
        Dim sql = "SELECT * FROM PRODUCTS"
        Using _conn = New OleDbConnection(GetOledbConnectionString())
            Return _conn.Query(Of PRODUCTS)(sql).ToList()
        End Using
    End Function
    Public Function GetCRITERIA1() As IEnumerable(Of CRITERIA1)
        Dim sql = "SELECT * FROM CRITERIA1"
        Using _conn = New OleDbConnection(GetOledbConnectionString())
            Return _conn.Query(Of CRITERIA1)(sql).ToList()
        End Using
    End Function
    Public Function GetCRITERIA2() As IEnumerable(Of CRITERIA2)
        Dim sql = "SELECT * FROM CRITERIA2"
        Using _conn = New OleDbConnection(GetOledbConnectionString())
            Return _conn.Query(Of CRITERIA2)(sql).ToList()
        End Using
    End Function
    Public Function GetCRITERIA3() As IEnumerable(Of CRITERIA3)
        Dim sql = "SELECT * FROM CRITERIA3"
        Using _conn = New OleDbConnection(GetOledbConnectionString())
            Return _conn.Query(Of CRITERIA3)(sql).ToList()
        End Using
    End Function
    Public Function GetCRITERIA4() As IEnumerable(Of CRITERIA4)
        Dim sql = "SELECT * FROM CRITERIA4"
        Using _conn = New OleDbConnection(GetOledbConnectionString())
            Return _conn.Query(Of CRITERIA4)(sql).ToList()
        End Using
    End Function
    Public Function GetCRITERIA5() As IEnumerable(Of CRITERIA5)
        Dim sql = "SELECT * FROM CRITERIA5"
        Using _conn = New OleDbConnection(GetOledbConnectionString())
            Return _conn.Query(Of CRITERIA5)(sql).ToList()
        End Using
    End Function
End Class

Code in Form2

Public Class Form2
    Dim ALLService As New ALLService()
    Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Me.DataGridView1.DataSource = ALLService.GetPRODUCTS()
        Me.DataGridView2.DataSource = ALLService.GetCRITERIA1()
        Me.DataGridView3.DataSource = ALLService.GetCRITERIA2()
        Me.DataGridView4.DataSource = ALLService.GetCRITERIA3()
        Me.DataGridView5.DataSource = ALLService.GetCRITERIA4()
    End Sub
End Class

RESULT CODE

5
  • What's the actual problem here? Don't just say "I want to do X, here's a big fat wad of code that I'm not going to explain". You need to provide details of EXACTLY where and how your code doesn't meet your expectations. Is the actual issue determining which grid to get the data from in the first place? Commented Jul 17, 2024 at 8:35
  • @jmcilhinney , Sorry if I didn't give an explanation. How to get selected cell value of multi DataGridView in TabControl to main datagridview in form1. for example when I selected row in the Criteria1 tabcontrol with enter 2 times then the value is in the selected row in the DatagridView column (Criteria1,DesCriteria1) in form1 Commented Jul 17, 2024 at 8:41
  • Firstly, no one should have to read the comments to understand the question. If you're going to provide more info then you need to edit the question. Secondly, you've really just repeated the same vague description. You need to provide specific details. Please spend some time in the Help Center to learn how to ask a question properly on this site. Commented Jul 17, 2024 at 9:48
  • What is a "multi DataGridView"? Commented Jul 17, 2024 at 15:30
  • @HardCode ,Thank you for your reply. What is a "multi DataGridView"? I mean the DataGridView in each of those tabControls. Please guide me. I have also updated the example Commented Jul 18, 2024 at 2:12

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.