未绑定的DataGridView:需要ComboBox仅在用户单击单元格时才在第二列的单元格中

时间:2012-08-12 11:19:55

标签: vb.net datagridview datagridtemplatecolumn datagridcomboboxcolumn datagridviewcomboboxcell

我有一个带有两列的未绑定DataGridView。第一列只是字符串值。 第二列我想显示一个组合框,只有当用户单击该单元格时(不是整列作为DataGridViewColumn)。我使用下面的代码不正确并给我错误:操作无效,因为它导致对SetCurrentCellAddressCore函数的可重入调用。 第一列是popuated,第二列是空的。

代码如下:

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

任何人都可以给我一个包含两列的完整工作示例,第二列是ComboBoxCell,但仅限用户点击。此外,我需要在DataGridView单元格中获取所选值。在此先感谢。

2 个答案:

答案 0 :(得分:0)

不要尝试替换事件处理程序中的列,而是创建一个包含2列的DataGridView,将第二列作为DataGridViewComboBoxColumn。该列上有一个名为“DisplayStyle”的属性,用于确定列在不编辑时的外观。将其设置为“Nothing”。现在它看起来像一个文本框,直到你进入编辑模式,此时它看起来像一个组合框。

答案 1 :(得分:0)

我有一个类似的DataGridView,其中第一列是文本标签,第二列是ComboBox。

注意:下面的代码在C#中,但概念与vb.net中的相同

在表单的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;

 }
相关问题