1

Goal

I want to have a DataGridViewComboBoxCell after setting my DataGridView's DataSource with a DataView.

DataGridView Goal


Current Situation

I have a DataTable that is populated with BillDetails as the user navigates through his Bills.

I created a DataView and set the DataView's Table to equal to the DataTable of Bill Details. I then set the DataGridView's DataSource to the DataView.

Setting the DataGridView's DataSource

Dim ViewContent As New DataView
ViewContent.Table = dsBillMat.Tables("dtBillDetails") 'Set the DataTable to my DataView's Table
ViewContent.RowFilter = "FK_BillHeader = '" & Bill.PK_BillHeader & "'" 'Filter the tables to get the correct Details for the corresponding Bill
dgvArticles.DataSource = ViewContent
FormatContentGridView() 'Formats the DataGridView Headers, Visible columns, etc.

FormatContentGridView

Code to format my DataGridView. Probably where I would need to add code for my ComboBoxCell?

Private Sub FormatContentGridView()
    With dgvArticles
        'Hide columns
        .Columns("PK_BillDetail").Visible = False
        .Columns("FK_BillHeader").Visible = False

        'Header text
        .Columns("ILNum").HeaderText = "# IL"
        .Columns("ArtNum").HeaderText = "# Article"
        .Columns("Description").HeaderText = "Description"
        .Columns("PartNum").HeaderText = "# Pièce"
        .Columns("Quantity").HeaderText = "Qté."
        .Columns("Manufacturer").HeaderText = "Manufacturier"
        .Columns("ShippedLose").HeaderText = "Sép."
        .Columns("OnHand").HeaderText = "En Main"
        .Columns("RSPL").HeaderText = "RSPL"
        .Columns("Code").HeaderText = "Code"
        .Columns("Cost").HeaderText = "Coût ($)"

        'Widths
        .Columns("Description").AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill
        .Columns("Description").MinimumWidth = 150

        For Each c As DataGridViewColumn In dgvArticles.Columns
            If c.Visible And c.AutoSizeMode <> DataGridViewAutoSizeColumnMode.Fill Then
                c.AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells
            End If
        Next

        'Display Index
        .Columns("ILNum").DisplayIndex = 0
        .Columns("ArtNum").DisplayIndex = 1
        .Columns("Description").DisplayIndex = 2
        .Columns("PartNum").DisplayIndex = 3
        .Columns("Quantity").DisplayIndex = 4
        .Columns("Manufacturer").DisplayIndex = 5
        .Columns("ShippedLose").DisplayIndex = 6
        .Columns("OnHand").DisplayIndex = 7
        .Columns("RSPL").DisplayIndex = 8
        .Columns("Code").DisplayIndex = 9
        .Columns("Cost").DisplayIndex = 10
    End With
End Sub

This works great, the information is populated succesfully. I just don't have my ComboBoxCell yet.

Successful DataGridView Display


Problem

My issue is, I must have a DataGridViewComboBoxCell for the Code column (red rectangle above). How do I set a DataGridViewComboBoxCell when the column is already created with the DataSource's DataView?

Alex
  • 4,821
  • 16
  • 65
  • 106

2 Answers2

2

In design view, Right click the DataGridView and choose Edit Columns. Find the column you want to adjust, and under ColumnType, change it to DataGridViewComboBoxColumn. Then you can set the DataSource and DisplayMembers to what you need.

At Run-time, you can create a new column and hide the previous column.

   Dim cboCode As New DataGridViewComboBoxColumn()
   cboCode.HeaderText = "Code"
   cboCode.DataPropertyName = "Code"
   cboCode.AutoSizeMode = DataGridViewAutoSizeColumnMode.NotSet
   cboCode.Name = "cboCode"
   cboCode.DataSource = dtData
   cboCode.DisplayMember = "Code"
   cboCode.ToolTipText = "the code for this account , blah, blah, blah"
   dgvArticles.Columns.Add(cboCode )
   .Columns("Code").visible = False
APrough
  • 2,671
  • 3
  • 23
  • 31
  • My DataGridView has no columns on design time. They are populated by the DataTable's columns. Unfortunately, I cannot do that. – Alex Feb 05 '14 at 16:34
  • See above for run-time solution. I left the design time solution in case anyone else needs it. – APrough Feb 05 '14 at 16:44
  • Almost there ... How would a value be applied to that column though; if the DataView is set to DataGridView's DataSource before the FormatContentGridView? (I put the following code in my FormatContentGridView) – Alex Feb 05 '14 at 16:52
  • @Alex, basically, by setting the new column's DataPropertyName to the underlying datasources' column that you want. So, you now technically have two columns with the same data in it; the hidden text field and the visible combobox, both of which display the "Code" column from the dataTable. (And both get updated as either of the fields are changed). – APrough Feb 05 '14 at 16:57
0

I'm not sure if this is the right place to post this, but here goes. I've got this exact same code but when I update my binding source the combo box value is not saved to my database until the second time I enter the value and run my save code

  • How do you "update your binding source" ? Through what event do you cause the ComboBox value to cause a trigger in the DataGridView? Once you find that, you can update your DataTable with the new value. But the way, I suggest you remove your answer because you might get "Flagged" for posting a question / comment as an answer. I know you don't have enough reputation to post a comment but ... unfortunately that's how it works :s – Alex Mar 28 '14 at 12:22