locked
binding a combobox in a datagridview to a new table vb.net

    Question

  • I have set up at DataGridView "cdgvExp" for data entry.    cdgvExp.Datasource = ExpTab.  The datagridview can accept new rows according to how many entries there are.   I will then use the data in ExpTab.   The Fourth colum is called "Lot" and is a combobox in the datagridview.   The goal is to have the Combobox in the fourth column have the datasource be LotTab which is populated according to other choices in the form.   I am hunting for something that would work like an independant combobox like

    cbLots.datasource = LotsTab

    How do I dynamically set the Datasource and Datamember of a combobox that is already set up in a datagridview?

    Sunday, April 29, 2012 7:47 PM

Answers

  • Hi Jjobcorp,

    I think Mitja shows the right code here, but it seems you need the VB.net edition, so please take a look at this code snippet:

        Private Sub ComboxBoxDataGridView_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
            
            Try
                SetUpDataGridView()
            Catch ex As Exception
                Console.WriteLine(ex.Message)
            End Try
        End Sub
    
        Public dataTableData As DataTable
        Public Sub SetUpDataGridView()
            'fill dataTableData with database data
            QueryDataBase()
    
            Dim c1 As New DataGridViewTextBoxColumn
            With c1
                .DataPropertyName = "name"
            End With
            Dim c2 As New DataGridViewComboBoxColumn
            With c2
                .Items.AddRange({"male", "female"})
                .DataPropertyName = "gender"
            End With
    
            Me.DataGridView1.Columns.AddRange({c1, c2})
            Me.DataGridView1.DataSource = dataTableData
    
        End Sub
    
        Public Sub QueryDataBase()
            dataTableData = New DataTable()
            Dim c1 As New DataColumn("name")
            Dim c2 As New DataColumn("gender")
    
            dataTableData.Columns.AddRange({c1, c2})
            dataTableData.Rows.Add({"mike", "male"})
            dataTableData.Rows.Add({"annie", "female"})
            dataTableData.Rows.Add({"luke", "male"})
            dataTableData.Rows.Add({"Jessica", "female"})
        End Sub

    I hope this will be helpful.

    Best regards,


    Mike Feng
    MSDN Community Support | Feedback to us
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.


    Tuesday, May 15, 2012 1:37 PM
  • Thank you.   

    Again, I am hearing you say that you can not do this unless the dgv is built with code.     It works when built with code.  

    I am using this as an input grid into an empty table; so, I did not use it as you showed.    I also am filling the combobox from a different table.   Here is my code if anlyone else would benefit from what I have done.

    Thank you again...

     Public Sub SetUpDataGridView()
            'fill dataTableData with database data
            ExpTab.Columns.Add("Descr", Type.GetType("System.String"))
            ExpTab.Columns.Add("Dol", Type.GetType("System.Double"))
            ExpTab.Columns.Add("CA", Type.GetType("System.String"))
            ExpTab.Columns.Add("Lot", Type.GetType("System.String"))
    
            Dim c1 As New DataGridViewTextBoxColumn
            With c1
                .DataPropertyName = "Descr"
                .Width = 150
                .MaxInputLength = 50
                .HeaderText = "Description"
            End With
    
            Dim c2 As New DataGridViewTextBoxColumn
            With c2
                .DataPropertyName = "Dol"
                .Width = 50
                .MaxInputLength = 14
                .HeaderText = "Amount"
            End With
    
            Dim c3 As New DataGridViewTextBoxColumn
            With c3
                .DataPropertyName = "CA"
                .Width = 45
                .MaxInputLength = 5
                .HeaderText = "CA #"
            End With
    
            Dim c4 As New DataGridViewComboBoxColumn
            With c4
                For Each FoundRow As DataRow In LotsTab.Rows
                    .Items.Add(FoundRow("Lots"))
                Next
                .DataPropertyName = "Lot"
                .HeaderText = "Lot #"
                .Width = 160
            End With
    
            Me.dgvExp.Columns.AddRange({c1, c2, c3, c4})
            Me.dgvExp.DataSource = ExpTab
        End Sub

    Tuesday, May 15, 2012 6:36 PM

All replies

  • Kindly view this post.

    Best regards,


    Nevin Janzen (Visit my Website)
    If this post answers your question, please click Propose As Answer. If this post is helpful, please click Vote As Helpful.
    How to Get Better Answers to Your Questions

    Monday, April 30, 2012 2:14 AM
  • Okay, I am new at this and thought I had done that; however I will change th form to hit your form

    1&2.   I am wanting to learn how to use code to dynamically bind a combobox in a datagridview to a table (LotTab) different from the table (ExpTab) that is bound to the datgridview.    Exptab uses the datagridview for dataentry and is blank before the data is entered.   There are various numbers of rows in the datagridview.

    3.   I don't want to build a datagridview with code, just bind the combobox to a table so I pick from the choices in that table.

    4.    I have not found a way to do this.   

    5.  I do not use links in the datagridview.

    6.   I have thought my problem was that I dudn't know how to identify the column to use .datasource.   I have tried cdgvexp.columns(1).datasource=Lottab and lots of other variations.    Since lotTab is populated according to other choices on the form, I can not link using the choices on the datagrid view.

    Monday, April 30, 2012 12:09 PM
  • You create new DataGridViewComboBoxColumn. And specify the datasource to this column only. Check here how it has to be done.

    Hope it helps, if not come back to ask for more.

    bye


    Mitja

    • Proposed as answer by Mike FengModerator Friday, May 04, 2012 6:06 AM
    • Marked as answer by Mike FengModerator Tuesday, May 08, 2012 1:02 PM
    • Unmarked as answer by jjobcorp Monday, May 14, 2012 1:09 PM
    • Unproposed as answer by jjobcorp Monday, May 14, 2012 1:10 PM
    Monday, April 30, 2012 12:24 PM
  • Excellent, @jjobcorp!! Now users can identify exactly what you need and can answer your question more accordingly. Keep it up! :)

    P.S. Does Mitja's response answer your question? If so, mark it as the answer by clicking Mark As Answer so users that view this forum will know that Mitja's response is the answer to the problem, therefore it might also answer theirs.

    Best regards,


    Nevin Janzen (Visit my Website)
    If this post answers your question, please click Propose As Answer. If this post is helpful, please click Vote As Helpful.
    How to Get Better Answers to Your Questions
    P.S. My replies are not critical, but humble responses. :)

    Monday, April 30, 2012 2:58 PM
  • Mr. Feng, I am new enough that this doesn't answer it for me.   I have built the columns using "add column" and set Column Type as a DataGridViewComboBoxColumn.   That worked fine.   Where I am having problems is setting the datasource in code to that column only.   I could do it if the datasource did not change depending upon the entry of other controls on the form.    I need how to set the datasource of that column only through code.  

    Thank you very much

    Tuesday, May 08, 2012 1:33 PM
  • Can you show us what you did so far? 

    This way we can be more helpful as well.

    But this is the simpliest example we can have:

               DataGridView dgv = new DataGridView();
                dgv.Location = new Point(20, 20);
                dgv.Columns.Add("col1", "Column 1"); //some usual column
                //lets add some rows:
                dgv.Rows.Add("item 1");
                dgv.Rows.Add("item 2");
                dgv.Rows.Add("item 3");
                DataGridViewComboBoxColumn cmbcolumn = new DataGridViewComboBoxColumn();
                cmbcolumn.Name = "col2";
                cmbcolumn.HeaderText = "combobox column";
                cmbcolumn.Items.AddRange(new string[] { "a1", "a2", "a3" }); // specifying data source to comboBox column!
                dgv.Columns.Insert(1, cmbcolumn);
                this.Controls.Add(dgv);

    ----

    Or by specifying the real datasource property to dgv: 

    NOTE: my example uses a list<T> as datasource:

       class Person
        {
            public int ID { get; set; }
            public string Name { get; set; }
        }
    
    
                //IN your code do:
                DataGridView dgv = new DataGridView();
                dgv.Location = new Point(20, 20);
                dgv.Columns.Add("col1", "Column 1"); //some usual column
                //lets add some rows:
                dgv.Rows.Add("item 1");
                dgv.Rows.Add("item 2");
                dgv.Rows.Add("item 3");
                DataGridViewComboBoxColumn cmbcolumn = new DataGridViewComboBoxColumn();
                cmbcolumn.Name = "col2";
                cmbcolumn.HeaderText = "combobox column";
    
                //now creating a generic list of person, and adding some data into it, and bind it to comboBox column:
                List<Person> people = new List<Person>();
                people.AddRange(new Person[] { new Person { ID = 1, Name = "Name 1" }, new Person { ID = 2, Name = "Name 2" } });
                cmbcolumn.DataSource = people;
                cmbcolumn.DisplayMember = "Name"; //same as properties
                cmbcolumn.ValueMember = "ID";
                dgv.Columns.Insert(1, cmbcolumn);
                this.Controls.Add(dgv);


    Mitja


    Tuesday, May 08, 2012 1:48 PM
  • Okay, are you saying that I have to build the dgv with code?   I did it using the form.   The combobox column is called "Lot"   I am setting the datasource for whole dgv to a table called ExpTab that I am building with code and populating from the dgv.    My problem is finding how to set the datasource of the combobox Lot in the grid; so, the user can pick a Lot and populate the ExpTab.   The Lot column will be populated from LotTab and the dgv will populate the ExpTab Tab once entries are made.
        Private Sub MiscExpMult_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
            Dim thisMonth As New DateTime(DateTime.Today.Year, DateTime.Today.Month, 1)
            Dim lastDayLastMonth As DateTime
            lastDayLastMonth = thisMonth.AddDays(-1)
            Me.CompaniesTableAdapter.Fill(Me.MasterDataDataSet.Companies)
            Me.VendorsTableAdapter.Fill(Me.MasterDataDataSet.Vendors)
            cbCompany.Text = gCompany
            dtpDate.Value = DateTime.Today
            dtpBil.Value = lastDayLastMonth
            cbVend.Text = ""
            mtbCk.Text = fGetReqAcctNo("Main Checking")
            ExpTab.Columns.Add("Descr", Type.GetType("System.String"))
            ExpTab.Columns.Add("Dol", Type.GetType("System.Double"))
            ExpTab.Columns.Add("CA", Type.GetType("System.String"))
            ExpTab.Columns.Add("Lot", Type.GetType("System.String"))
            sFillLotNo()
            cdgvExp.DataSource = ExpTab
    
         
        End Sub

    Thanks...
    Tuesday, May 08, 2012 3:48 PM
  • Hi Jjobcorp,

    I think Mitja shows the right code here, but it seems you need the VB.net edition, so please take a look at this code snippet:

        Private Sub ComboxBoxDataGridView_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
            
            Try
                SetUpDataGridView()
            Catch ex As Exception
                Console.WriteLine(ex.Message)
            End Try
        End Sub
    
        Public dataTableData As DataTable
        Public Sub SetUpDataGridView()
            'fill dataTableData with database data
            QueryDataBase()
    
            Dim c1 As New DataGridViewTextBoxColumn
            With c1
                .DataPropertyName = "name"
            End With
            Dim c2 As New DataGridViewComboBoxColumn
            With c2
                .Items.AddRange({"male", "female"})
                .DataPropertyName = "gender"
            End With
    
            Me.DataGridView1.Columns.AddRange({c1, c2})
            Me.DataGridView1.DataSource = dataTableData
    
        End Sub
    
        Public Sub QueryDataBase()
            dataTableData = New DataTable()
            Dim c1 As New DataColumn("name")
            Dim c2 As New DataColumn("gender")
    
            dataTableData.Columns.AddRange({c1, c2})
            dataTableData.Rows.Add({"mike", "male"})
            dataTableData.Rows.Add({"annie", "female"})
            dataTableData.Rows.Add({"luke", "male"})
            dataTableData.Rows.Add({"Jessica", "female"})
        End Sub

    I hope this will be helpful.

    Best regards,


    Mike Feng
    MSDN Community Support | Feedback to us
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.


    Tuesday, May 15, 2012 1:37 PM
  • Thank you.   

    Again, I am hearing you say that you can not do this unless the dgv is built with code.     It works when built with code.  

    I am using this as an input grid into an empty table; so, I did not use it as you showed.    I also am filling the combobox from a different table.   Here is my code if anlyone else would benefit from what I have done.

    Thank you again...

     Public Sub SetUpDataGridView()
            'fill dataTableData with database data
            ExpTab.Columns.Add("Descr", Type.GetType("System.String"))
            ExpTab.Columns.Add("Dol", Type.GetType("System.Double"))
            ExpTab.Columns.Add("CA", Type.GetType("System.String"))
            ExpTab.Columns.Add("Lot", Type.GetType("System.String"))
    
            Dim c1 As New DataGridViewTextBoxColumn
            With c1
                .DataPropertyName = "Descr"
                .Width = 150
                .MaxInputLength = 50
                .HeaderText = "Description"
            End With
    
            Dim c2 As New DataGridViewTextBoxColumn
            With c2
                .DataPropertyName = "Dol"
                .Width = 50
                .MaxInputLength = 14
                .HeaderText = "Amount"
            End With
    
            Dim c3 As New DataGridViewTextBoxColumn
            With c3
                .DataPropertyName = "CA"
                .Width = 45
                .MaxInputLength = 5
                .HeaderText = "CA #"
            End With
    
            Dim c4 As New DataGridViewComboBoxColumn
            With c4
                For Each FoundRow As DataRow In LotsTab.Rows
                    .Items.Add(FoundRow("Lots"))
                Next
                .DataPropertyName = "Lot"
                .HeaderText = "Lot #"
                .Width = 160
            End With
    
            Me.dgvExp.Columns.AddRange({c1, c2, c3, c4})
            Me.dgvExp.DataSource = ExpTab
        End Sub

    Tuesday, May 15, 2012 6:36 PM
  • I have two tables itemaster and podetail table. I am using itemmaster table to fill comoboxcolumn whose fileds are (itemno,itemname) and save it's value in podetail table. how i can serch particular podetail value and load it to comoboxcolumn.... 
    Saturday, October 27, 2012 7:28 AM