How To Add A DataGridViewComboBoxCell to a Bound D

2019-08-08 19:51发布

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?

2条回答
干净又极端
2楼-- · 2019-08-08 20:07

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

查看更多
【Aperson】
3楼-- · 2019-08-08 20:16

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
查看更多
登录 后发表回答