none
change column in datagridview when change datagridview combobox RRS feed

  • Question

  • i have a datagridview with one combobox column with multi data index " FirstUnit ", " SecondUnit", " ThirdUnit"

    and the second column is : " FirstUnitBarcode", " SecondUnitBarcode", " ThirdUnitBarcode"

    and also one column: " ItemCode"

    now i want if the user select " FirstUnit " from DataGridViewComBobox , the barcode column change to " FirstUnitBarcode", ...etc

    my code to display data to invoice form is:

        Sub getItemUnits(Itemcode As String)
            Dim adp As New SqlClient.SqlDataAdapter("select * from Items where ItemCode=N'" & Itemcode & "'", SQlconn)
            Dim ds As New DataSet
            adp.Fill(ds)
            Dim dt = ds.Tables(0)
            If dt.Rows.Count > 0 Then
                Dim dgvcc As DataGridViewComboBoxCell
                dgvcc = DataGridView1.Rows(DataGridView1.Rows.Count - 1).Cells(3)
                dgvcc.Items.Add(dt.Rows(0).Item("FirstUnit"))
                If dt.Rows(0).Item("SecondUnit").ToString <> "" Then dgvcc.Items.Add(dt.Rows(0).Item("SecondUnit"))
                If dt.Rows(0).Item("ThirdUnit").ToString <> "" Then dgvcc.Items.Add(dt.Rows(0).Item("ThirdUnit"))
                DataGridView1.ClearSelection()
                DataGridView1.Rows(DataGridView1.Rows.Count - 1).Cells(3).Selected = True
            End If
        End Sub


    and i called it in Search form by:

    Dim itmcod As String
    FrmBuyInvoice.DataGridView1.Rows.Add()
            FrmBuyInvoice.DataGridView1.Rows(FrmBuyInvoice.DataGridView1.Rows.Count - 1).Cells(0).Value = DataGridView1.CurrentRow.Cells(1).Value
            FrmBuyInvoice.DataGridView1.Rows(FrmBuyInvoice.DataGridView1.Rows.Count - 1).Cells(2).Value = DataGridView1.CurrentRow.Cells(2).Value
            FrmBuyInvoice.DataGridView1.Rows(FrmBuyInvoice.DataGridView1.Rows.Count - 1).Cells(1).Value = DataGridView1.CurrentRow.Cells(3).Value
    
    itmcod = DataGridView1.CurrentRow.Cells(1).Value
    FrmBuyInvoice.getItemUnits(itmcod)

    i need something like:

    If dt.Rows(0).Item("FirstUnit").ToString = ("FirstUnit") Then DataGridView1.Rows(0).Cells(2).Value = ("FirstUnitBarcode")
    If dt.Rows(0).Item("SecondUnit").ToString = ("SecondUnit") Then DataGridView1.Rows(0).Cells(2).Value = ("SecondUnitBarcode")
    If dt.Rows(0).Item("ThirdUnit").ToString = ("ThirdUnit") Then DataGridView1.Rows(0).Cells(2).Value = ("ThirdUnitBarcode")

    help please



    • Edited by M.G.R Sunday, June 10, 2018 7:07 AM
    Sunday, June 10, 2018 12:58 AM

All replies

  • Hi M.G.R,

    According to your description, I am not sure what problem you encounter, do you want to add combobox in datagridview, when you select different value in combobox, the data in other datagridview column will be different?

    Imports System.Data.SqlClient
    
    Public Class Form1
        Dim constr As String = "Data Source = (localdb)\MSSQLLocalDB; Integrated Security = True ;AttachDbFileName= C:\Users\alexl2\Desktop\DataBase\Alex\alex.mdf"
        Dim adp As SqlDataAdapter
        Dim conn As SqlConnection
        Dim dt As DataTable
        Dim cb As DataGridViewComboBoxCell
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            dt = New DataTable
            Using conn = New SqlConnection(constr)
                '    conn.Open()
                '    adp = New SqlDataAdapter("select * from Test", conn)
                '    adp.Fill(dt)
                '    DataGridView1.DataSource = dt
                cb = New DataGridViewComboBoxCell()
                cb.Items.Add("FirstUnit")
                cb.Items.Add("SecondUnit")
                cb.Items.Add("ThirdUnit")
                DataGridView1.Rows(0).Cells(0) = cb
            End Using
        End Sub
    
        Private Sub DataGridView1_EditingControlShowing(sender As Object, e As DataGridViewEditingControlShowingEventArgs) Handles DataGridView1.EditingControlShowing
    
            AddHandler(TryCast(e.Control, ComboBox)).SelectedIndexChanged, AddressOf ComboBox_SelectedIndexChanged
        End Sub
        Public Sub ComboBox_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)
            Dim combox As ComboBox = TryCast(sender, ComboBox)
            Try
    
                If combox.SelectedItem = "FirstUnit" Then
                    DataGridView1.Rows(0).Cells(1).Value = "FirstUnitBarcode"
    
                End If
                If combox.SelectedItem = "SecondUnit" Then
                    DataGridView1.Rows(0).Cells(1).Value = "SecondUnitBarcode"
    
                End If
                If combox.SelectedItem = "ThirdUnit" Then
                    DataGridView1.Rows(0).Cells(1).Value = "ThirdUnitBarcode"
    
                End If
            Catch ex As Exception
                MessageBox.Show(ex.Message)
            End Try
        End Sub
    
    
    
    
    End Class

    Best Regards,

    Cherry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Monday, June 11, 2018 6:22 AM
    Moderator
  • sorry but it dosen't work for me because i want to add a condition for search:

    "select * from Items where Status='True' and ItemName like N'%" & SearchText.Text.Trim & "%" & "'"

    DataGridView1.Columns(0).DataPropertyName = "ItemCode"

    DataGridView1.Columns(3).DataPropertyName = "ItemName"


    Monday, June 11, 2018 11:38 PM
  • Hi M.G.R,

    You said that you want to add a condition for search,your means that you want to filter datagridview by one textbox? If yes, you can take a look the following code:

    https://www.aspsnippets.com/Articles/Search-Filter-DataGridView-on-TextBox-KeyPress-in-Windows-Forms-Application-using-C-and-VBNet.aspx

    If not, please provide your code here, and describe what problem you encounter here?

    Best Regards,

    Cherry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, June 13, 2018 7:53 AM
    Moderator
  • Ok, i need to add all 3 units for the items in the same DataGridView with new rows if the item already have 3 units  like this:

    Dim Sql As String = ""

    If SearchText.Text.Length = 0 Then Sql = "select * from Items where Status='True' and SupplierName+SupplierName2+SupplierName3 like N'%" & FrmBuyInvoice.SupplierName.SelectedText & "%" & "' and ItemName like N'%" & SearchText.Text.Trim & "%" & "'" With DataGridView1 Dim row As String() = New String() {"ItemCode", "FirstUnit", "ItemName"} DataGridView1.Rows.Add(row) row = New String() {"ItemCode", "SecondUnit", "ItemName"} DataGridView1.Rows.Add(row) row = New String() {"ItemCode", "ThirdUnit", "ItemName"} DataGridView1.Rows.Add(row)

    End With End If

            Dim adp As New SqlClient.SqlDataAdapter(Sql, SQlconn)
            Dim ds As New DataSet
            adp.Fill(ds)
            Dim dt = ds.Tables(0)
            '=====================================================
            DataGridView1.AutoGenerateColumns = False
            DataGridView1.DataSource = dt.DefaultView

    OR

    Dim Sql As String = ""
    If SearchText.Text.Length = 0 Then
                Sql = "select * from Items where Status='True' and SupplierName+SupplierName2+SupplierName3 like N'%" & FrmBuyInvoice.SupplierName.SelectedText & "%" & "' and ItemName like N'%" & SearchText.Text.Trim & "%" & "'"
                With DataGridView1
                    .Rows(0).Cells(0).Value = "ItemCode"
                    .Rows(1).Cells(0).Value = "ItemCode"
                    .Rows(2).Cells(0).Value = "ItemCode"
                    .Rows(0).Cells(1).Value = "FirstUnit"
                    .Rows(1).Cells(1).Value = "SecondUnit"
                    .Rows(2).Cells(1).Value = "ThirdUnit"
                    .Rows(0).Cells(2).Value = "ItemName"
                    .Rows(1).Cells(2).Value = "ItemName"
                    .Rows(2).Cells(2).Value = "ItemName"
    
                End With
    End If
            Dim adp As New SqlClient.SqlDataAdapter(Sql, SQlconn)
            Dim ds As New DataSet
            adp.Fill(ds)
            Dim dt = ds.Tables(0)
            '=====================================================
            DataGridView1.AutoGenerateColumns = False
            DataGridView1.DataSource = dt.DefaultView

    Like that image exactly:



    • Edited by M.G.R Saturday, June 16, 2018 11:56 PM
    Friday, June 15, 2018 1:22 AM
  • Hi M.G.R,

    According to your description and your code, I just see you add three row in datagridview. You said that you need to add all 3 units for the items in the same DataGridView with new rows if the item already have 3 units, what it means? I see that you want to get data from sql database by filtering searchtext, you fill these data into datatable, what do you want to do with this table?

    Best Regards,

    Cherry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, June 19, 2018 8:01 AM
    Moderator
  • Ok, this is my SQL Table:

    i want the above items to shown in datagridview like that:

    *the above image i created it manually to show you what i mean*

    someone told me to using For Each loop over the Rows collection of that and INSIDE that loop use a For loop to add three rows to the grid.

    but i don't know how to do that.

    can you please provide me a code for that?

    Friday, June 22, 2018 9:47 PM