简体   繁体   English

Unbound DataGridView:仅当用户单击单元格时,才需要在第二列的单元格中使用ComboBox

[英]Unbound DataGridView : Need ComboBox In cell of 2nd column only when user clicks cell

I have a unbound DataGridView with two columns. 我有两列的未绑定DataGridView。 First column is just string values. 第一列只是字符串值。 Second column I want to display a combobox, only when user click the cell(not the whole column as DataGridViewColumn). 我只想在用户单击单元格时才显示一个组合框(而不是整个列为DataGridViewColumn)。 I use the below code which is incorrect and gives me the error : Operation is not valid because it results in a reentrant call to the SetCurrentCellAddressCore function. 我使用以下代码不正确,并给我错误: 操作无效,因为它导致对SetCurrentCellAddressCore函数的可重入调用。 The first column is popuated, and the second column is empty. 第一列填充,第二列为空。

The code is as below : 代码如下:

Private Sub DGVFieldsMap_CellEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DGVFieldsMap.CellEnter
    If e.ColumnIndex = 1 Then
        If cboClmCell Is Nothing Then
            Dim dgv As DataGridView = CType(sender, DataGridView)
            cboClmCell = New DataGridViewComboBoxCell
            cboClmCell.Items.Add("A")
            cboClmCell.Items.Add("B")
            cboClmCell.Items.Add("C")
            cboClmCell.Items.Add("D")
            cboClmCell.Items.Add("E")
            cboClmCell.Items.Add("F")
            dgv.Focus()
            dgv(e.ColumnIndex, e.RowIndex) = cboClmCell '[Error Here]
            isCombo = True
        End If
    End If
End Sub


Private Sub DGVFieldsMap_CellValidating(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellValidatingEventArgs) Handles DGVFieldsMap.CellValidating
        If e.ColumnIndex = 1 Then
            Dim dgv As DataGridView = CType(sender, DataGridView)
            If isCombo Then
                isCombo = False
                cboClmCell = Nothing
                dgv(e.ColumnIndex, e.RowIndex) = New DataGridViewTextBoxCell()
            End If
        End If
End Sub

Can anybody give me a complete working example with two columns, the second column being a ComboBoxCell, but only when user clicks. 任何人都可以给我一个完整的示例,其中有两列,第二列是ComboBoxCell,但只有在用户单击时才可以。 Also I need to get the selected values in the DataGridView cell. 另外,我需要在DataGridView单元格中获取选定的值。 Thanks In Advance. 提前致谢。

Don't try to replace the columns in event handlers, instead create a DataGridView with 2 columns, have the 2nd column be your DataGridViewComboBoxColumn. 不要尝试替换事件处理程序中的列,而是创建一个包含2列的DataGridView,让第二列成为您的DataGridViewComboBoxColumn。 There is a property on that column called "DisplayStyle" which determines how the column looks when not editing. 该列上有一个称为“ DisplayStyle”的属性,该属性确定该列在不编辑时的外观。 Set it to "Nothing". 将其设置为“无”。 Now it will look like a textbox until you go into edit mode at which point it looks like a combobox. 现在它看起来像一个文本框,直到您进入编辑模式,此时它看起来像一个组合框。

I have a similar DataGridView where the first column is a textual label and the second column is ComboBox. 我有一个类似的DataGridView,其中第一列是文本标签,第二列是ComboBox。

Note: The code below is in C#, but the concept is the same as in vb.net 注意:以下代码在C#中,但概念与vb.net中的相同

In the form's load event, call a function that sets up the datasource and creates the columns 在表单的load事件中,调用一个函数来设置数据源并创建列

private void frmCfgEdit_Load(object sender, EventArgs e)
{
    // Fill CFG Data Grid
    FillCfgDataGrid();
}

 private void FillCfgDataGrid()
 {

    // Do not automatically generate the columns based on the datasource public fields
    dgCFG.AutoGenerateColumns = false;

    // Define data source
    dgCFG.DataSource = _pDriveElement.CfgTableViewRecs;

    // Define data grid columns 
    SetUpCFGDataGrid(dgCFG);
 }

 public void SetUpCFGDataGrid(DataGridView dgCFG, String TableIdentifier)
 {
    // Create datagridview text column
    AddGridColumn(dgCFG,  "Label", "CfgLabel", 350, typeof(System.String), true, false);
    // Create datadridview combobox column
    AddGridComboColumn(dgCFG,  "Value",  350, typeof(System.String), false, true);

 }

 public void AddGridColumn(DataGridView dg, String sHeaderText, String sDataPropertyName, int iWidth, Type tyValueType, bool bReadOnly, bool bLastCol)
 {
     DataGridViewTextBoxColumn colTxt = new DataGridViewTextBoxColumn();
     colTxt.HeaderText = sHeaderText;
     colTxt.Width = iWidth;
     colTxt.ReadOnly = bReadOnly;

     // Add the text box to the data grid
     dg.Columns.Add(colTxt);
     int iColumn = dg.Columns.Count - 1;

     // Define bindings to text columns
     dg.Columns[iColumn].DataPropertyName = sDataPropertyName;
     dg.Columns[iColumn].ValueType = tyValueType;
     if (tyValueType == typeof(System.Single))    dg.Columns[iColumn].DefaultCellStyle.Format = "F6";
        if (bLastCol) dg.Columns[iColumn].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
        if (iColumn > 0) dg.Columns[iColumn].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
 }

 public void AddGridComboColumn(DataGridView dg, String sHeaderText,  int iWidth, Type tyValueType, bool bReadOnly, bool bLastCol)
 {
     DataGridViewComboBoxColumn cb = new DataGridViewComboBoxColumn();
     cb.FlatStyle = FlatStyle.Flat;
     cb.HeaderText = sHeaderText;
     cb.Width = iWidth;
     cb.ReadOnly = bReadOnly;
     dg.Columns.Add(cb);
     int iColumn = dg.Columns.Count - 1;

     // Combo box is always left aligned
     dg.Columns[iColumn].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleLeft;

     if (bLastCol) dg.Columns[iColumn].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;

 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM