Results 1 to 3 of 3

Thread: [RESOLVED] Add and populate Combobox into Datagridview

Threaded View

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2004
    Location
    Bloomingdale, IL USA
    Posts
    284

    Resolved [RESOLVED] Add and populate Combobox into Datagridview

    Hello,

    Here is what I am trying to do:

    • Populate a datagridview with data from the database

    • Use 1st column in the returned data to populate a combobox column in the datagridview

    • Allow user to change the selection of the combobox and write the update back to the database


    I am populating the DGV using a datatable... here is my code. I am familiar with displaying data in the grid, but not much else. I can get the combobox column to show up with the selections, but how can I populate the combobox using the data being pulled into the datagrid? Do I have to populate the datagridview row by row and programatically "select" the value from the dropdown that matches the data?

    Code:
             Dim rsLeads As New SQLControl
    
                rsLeads.ExecQuery("SELECT u.salesperson_name, rtrim(Lead_ID) 'Lead_ID', rtrim(infogroup_id) InfoGroup_Id, rtrim(Lead_status) 'Lead Status', rtrim(Company_Name) 'Company Name', rtrim(mailing_address) Address,
                                rtrim(Mailing_City) + ', ' + rtrim(Mailing_State) State, l.date_created 'Date Created' FROM Leads l , users u WHERE u.User_Id = l.User_ID")
    
                If rsLeads.HasException Then
                    MsgBox(rsLeads.Exception, MsgBoxStyle.OkOnly, "Error Loading Leads")
                End If
    
                If rsLeads.RecordCount = 0 Then
                    MsgBox("No leads found.", MsgBoxStyle.OkOnly, "No Records")
                    Exit Sub
                End If
    
                DgvLeads.DataSource = rsLeads.DBDT
    
                Dim Sql As New SQLControl
                Sql.ExecQuery("SELECT rtrim(salesperson_name) xName, user_id FROM Users Order by salesperson_name ")
    
                Dim cmb As New DataGridViewComboBoxColumn()
                cmb.HeaderText = "Assign User"
                cmb.Name = "cmb"
                cmb.DataSource = Sql.DBDT
                cmb.ValueMember = "user_id"
                cmb.DisplayMember = "xName"
    
                DgvLeads.Columns.Insert(1, cmb)
    This is how my data looks. Basically I want the salesperon column to be populated in the "Assign User" column.
    Name:  DGV.JPG
Views: 2249
Size:  33.0 KB

    I have tried multiple variations and can't seem to get what I am looking for.

    Thank you in advance for any help you can offer.

    UPDATE:
    I rearranged my code to this and it works EXCEPT I get an error every time it updates the combobox.

    Code:
                Dim Sql As New SQLControl
                Sql.ExecQuery("SELECT rtrim(salesperson_name) xName, user_id FROM Users Order by salesperson_name ")
    
                Dim cmb As New DataGridViewComboBoxColumn()
                cmb.HeaderText = "Assign User"
                cmb.Name = "cmb"
                cmb.DataSource = Sql.DBDT
                cmb.ValueMember = "user_id"
                cmb.DisplayMember = "xName"
    
    'I added this line of code that corresponds to the name in my datatable when populating
                cmb.DataPropertyName = "xName"
    
                DgvLeads.Columns.Insert(0, cmb)
    
                Dim rsLeads As New SQLControl
    
                'rsLeads.AddParam("@user", globalUserID)
                rsLeads.ExecQuery("SELECT rtrim(salesperson_name) xName, rtrim(Lead_ID) 'Lead_ID', rtrim(infogroup_id) InfoGroup_Id, rtrim(Lead_status) 'Lead Status', rtrim(Company_Name) 'Company Name', rtrim(mailing_address) Address,
                                rtrim(Mailing_City) + ', ' + rtrim(Mailing_State) State, l.date_created 'Date Created' FROM Leads l , users u WHERE u.User_Id = l.User_ID" + sqlFilter)
    
                If rsLeads.HasException Then
                    MsgBox(rsLeads.Exception, MsgBoxStyle.OkOnly, "Error Loading Leads")
                End If
    
                If rsLeads.RecordCount = 0 Then
                    MsgBox("No leads found.", MsgBoxStyle.OkOnly, "No Records")
                    Exit Sub
                End If
    
                DgvLeads.DataSource = rsLeads.DBDT
    This is the error I get
    Name:  Error.jpg
Views: 1878
Size:  50.2 KB
    Last edited by Chrissy; Sep 20th, 2024 at 12:46 PM.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width