Click here to Skip to main content
15,887,135 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
I want to fill combobox in datagrid with name of items not the id

i have this code :





its working fine but it dosent load in combobox its create new 3 columns

What I have tried:

<pre lang="vb"><pre> Public Sub getItemUnits(DGV As DataGridView, ItemID As Integer)
        Dim adp As New SqlClient.SqlDataAdapter("Select FirstUnit, SecondUnit, ThirdUnit from ItemsMainTbl where ItemID = '" & ItemID & "'", ConfigurationManager.ConnectionStrings("connSQLServer").ConnectionString)
        Dim ds As New DataSet
        adp.Fill(ds)
        Dim dt = ds.Tables(0)
        If dt.Rows.Count > 0 Then
            Dim dgvcc As DataGridViewComboBoxCell
            dgvcc = DGV.Rows(DGV.Rows.Count - 1).Cells(2)
            dgvcc.Items.Add(dt.Rows(0).Item("FirstUnit"))
            dgvcc.Items.Add(dt.Rows(0).Item("SecondUnit"))
            dgvcc.Items.Add(dt.Rows(0).Item("ThirdUnit"))
            DGV.ClearSelection()
            DGV.Rows(DGV.Rows.Count - 1).Cells(2).Selected = True
        End If


    End Sub
VB
  FrmManage_Store.ItemLoadGv.Rows.Add()
        FrmManage_Store.ItemLoadGv.Rows(FrmManage_Store.ItemLoadGv.Rows.Count - 1).Cells(0).Value = ItemsGv.CurrentRow.Cells(0).Value
        FrmManage_Store.ItemLoadGv.Rows(FrmManage_Store.ItemLoadGv.Rows.Count - 1).Cells(1).Value = ItemsGv.CurrentRow.Cells(1).Value
        ItemID = ItemsGv.CurrentRow.Cells(0).Value
FrmManage_Store.ItemLoadGv.Rows(FrmManage_Store.ItemLoadGv.CurrentRow.Index).Cells(2).DataGridView.DataSource = p4.Get_Units_By_ItemID(ItemID)
Posted
Updated 17-May-17 5:38am

Never build an SQL query by concatenating with user inputs, it is named "SQL injection", it is dangerous for your database and error prone.
A single quote in a name and your program crash. If a user input like "Brian O'Conner" can crash your app, it is an SQL injection vulnerability.
SQL injection - Wikipedia[^]
SQL Injection[^]
 
Share this answer
 
i have fixed like so :


VB
<pre>      Public Function Get_Units_By_ItemID(DGV As DataGridView, ByVal ItemID As Integer) As DataTable
            Dim dt As New DataTable
            p.Get_Units_By_ItemID(dt, ItemID)
            If dt.Rows.Count > 0 Then
                Dim dgvcc As DataGridViewComboBoxCell
                dgvcc = DGV.Rows(DGV.Rows.Count - 1).Cells(2)
                dgvcc.Items.Add(dt.Rows(0).Item("UnitName"))
                dgvcc.Items.Add(dt.Rows(0).Item("UnitName1"))
                dgvcc.Items.Add(dt.Rows(0).Item("UnitName2"))
                DGV.ClearSelection()
                DGV.Rows(DGV.Rows.Count - 1).Cells(2).Selected = True
            End If
            Return dt
        End Function
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900