如何将DataGridViewComboBoxCell添加到绑定的DataGridView数据源(以编程方式) [英] How To Add A DataGridViewComboBoxCell to a Bound DataGridView DataSource (Programatically)

查看:262
本文介绍了如何将DataGridViewComboBoxCell添加到绑定的DataGridView数据源(以编程方式)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在使用DataView设置DataGridView的数据源后拥有一个DataGridViewComboBoxCell。

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

< img src = https://i.stack.imgur.com/x2ZGL.png alt = DataGridView目标>

当用户浏览 BillDetails 时,我有一个数据表c $ c> Bills 。

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

我创建了一个DataView,并将DataView的Table设置为等于Bill Details的DataTable。然后,我将DataGridView的数据源设置为DataView。

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.

设置DataGridView的数据源

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

用于格式化DataGridView的代码。

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

此方法很好用,信息是成功填充。我还没有ComboBoxCell。

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

我的问题是,对于代码列,我必须具有DataGridViewComboBoxCell(上方的红色矩形)。

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?

推荐答案

在设计视图中,右键单击DataGridView,如何在已使用数据源的DataView创建列的情况下设置DataGridViewComboBoxCell?并选择编辑列。找到要调整的列,然后在ColumnType下将其更改为DataGridViewComboBoxColumn。然后,可以将DataSource和DisplayMembers设置为所需。

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

这篇关于如何将DataGridViewComboBoxCell添加到绑定的DataGridView数据源(以编程方式)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆