how to save the corresponding data based on ComboBox user input in visual studio using VB (not the displayed data)

Hossam 40 Reputation points
2023-03-03T09:28:32.7966667+00:00

Hi

how to save the corresponding data based on ComboBox user input in visual studio using VB (not the displayed data)

Clarification:

In my database in the customer table, I have customer code and customer name fields, I filled the combobox from the table customers, the combobox show only the customer's name but when the user saves the transaction, I save in another table called transaction, in this table transaction I want to save the customer code not the customer's name, knowing that I'm using Visual Studio 2022 and writing code using visual basic, the database SQL server

Please help

Regards

VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,568 questions
{count} votes

Accepted answer
  1. LesHay 7,126 Reputation points
    2023-03-06T04:15:16.92+00:00

    Hi

    Not sure if I understand your question, but maybe this is in the right area.

    Here is some demo code that will add customer code to transaction datatable (and displayed in datagridview named DGV) based on user selection from combobox named CB (filled with names from customer datatable). If you want to try this code please start a new project and put a combobox named CB and a dtagridview named DGV onto Form1, then copy/replace Form1 code with this code.

    ' Form1 with ComboBox named CB
    ' DataGridView named DGV
    Option Strict On
    Option Explicit On
    Public Class Form1
      Dim customer As New DataTable
      Dim transaction As New DataTable
    
      ' just to stop calls to CB event handler
      ' before data has been created
      Dim started As Boolean = False
      Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        With customer
          .Columns.Add("code", GetType(String))
          .Columns.Add("name", GetType(String))
    
          ' some dummy data
          .Rows.Add("AZ23BY675", "LesHay")
          .Rows.Add("PQ7ZXT368", "Hossam")
          .Rows.Add("WWP988GTW", "Somebody")
          .Rows.Add("SK59UGG", "Else")
        End With
        With transaction
          .Columns.Add("code", GetType(String))
        End With
        DGV.DataSource = transaction
        With CB
          .DataSource = customer
          .DisplayMember = "name"
          .ValueMember = "code"
        End With
        started = True
      End Sub
      Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles CB.SelectedIndexChanged
        ' add code to transaction datatable
        ' when user selects name from combobox
        If Not started Then Exit Sub
        transaction.Rows.Add(CB.SelectedValue.ToString)
      End Sub
    End Class
    
    
    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful