C# 如何在显示嵌套属性的DataGridViewComboboxColumn中禁用一个组合框?

C# 如何在显示嵌套属性的DataGridViewComboboxColumn中禁用一个组合框?,c#,winforms,datagridview,C#,Winforms,Datagridview,我在dataGridView中显示对象列表。清单如下: List<IAction> actions = new List<IAction> 此附加属性对象类似于: public class Area { public string Name { get; set; } ... } dataGridView列定义如下: DataGridViewComboBoxColumn CreateComboBoxWithArea() {

我在dataGridView中显示对象列表。清单如下:

List<IAction> actions = new List<IAction>
此附加属性对象类似于:

public class Area
{
    public string Name { get; set; }
    ...
}
dataGridView列定义如下:

    DataGridViewComboBoxColumn CreateComboBoxWithArea()
    {
        DataGridViewComboBoxColumn combo = new DataGridViewComboBoxColumn();
        combo.DataSource = areas.Select(t => t.Name).ToArray();
        combo.DataPropertyName = "Area.Name";
        combo.Name = "Area";
        return combo;
    }

您可以绑定到单元格的BeginEdit事件中,并在此时取消对组合的编辑。比如:

dataGrid.CellBeginEdit += dataGrid_CellBeginEdit;

void dgSamples_CellBeginEdit(object sender, GridViewCellCancelEventArgs e)
{
    var areaObject = e.Row.Cells["Area"].Value AS Area; // .Value will be the current selected item in the cell's combo.
    if (areaObject.Area == null)
    {
         e.Cancel = true;
    }
}
这不会将组合显示为“已禁用”,但会阻止用户删除组合并更改值。基本上使单元格为只读


最后,在为组合框定义数据源时,我不确定区域列表对象的类型。因此,当我将.Value属性强制转换为区域时,我在猜测类型。您可能需要对此进行更改。

我找到了自己的解决方案:

在释放发送者后,我可以访问整个网格。之后,我可以用这个组合框做我想做的事情隐藏按钮,设置只读。这是因为combobox没有属性bool Enable/

谢谢你的帮助jaredbaszler

private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            DataGridView grid = (DataGridView)sender;
            DataGridViewRow row = grid.Rows[e.RowIndex];
            DataGridViewColumn col = grid.Columns[e.ColumnIndex];
            if (row.DataBoundItem != null && col.DataPropertyName.Contains("."))
            {
                string[] props = col.DataPropertyName.Split('.');
                PropertyInfo propInfo = row.DataBoundItem.GetType().GetProperty(props[0]);
                if(propInfo != null)
                {
                    object val = propInfo.GetValue(row.DataBoundItem, null);
                    for (int i = 1; i < props.Length; i++)
                    {
                        propInfo = val.GetType().GetProperty(props[i]);
                        val = propInfo.GetValue(val, null);
                    }
                    e.Value = val;
                }
                else
                {
                    DataGridViewCell cell = grid.Rows[e.RowIndex].Cells[e.ColumnIndex];
                    DataGridViewComboBoxCell chkCell = cell as DataGridViewComboBoxCell;
                    chkCell.DisplayStyle = DataGridViewComboBoxDisplayStyle.Nothing;
                    cell.ReadOnly = true;
                }
            }
        }
private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            DataGridView grid = (DataGridView)sender;
            DataGridViewRow row = grid.Rows[e.RowIndex];
            DataGridViewColumn col = grid.Columns[e.ColumnIndex];
            if (row.DataBoundItem != null && col.DataPropertyName.Contains("."))
            {
                string[] props = col.DataPropertyName.Split('.');
                PropertyInfo propInfo = row.DataBoundItem.GetType().GetProperty(props[0]);
                if(propInfo != null)
                {
                    object val = propInfo.GetValue(row.DataBoundItem, null);
                    for (int i = 1; i < props.Length; i++)
                    {
                        propInfo = val.GetType().GetProperty(props[i]);
                        val = propInfo.GetValue(val, null);
                    }
                    e.Value = val;
                }
                else
                {
                    DataGridViewCell cell = grid.Rows[e.RowIndex].Cells[e.ColumnIndex];
                    DataGridViewComboBoxCell chkCell = cell as DataGridViewComboBoxCell;
                    chkCell.DisplayStyle = DataGridViewComboBoxDisplayStyle.Nothing;
                    cell.ReadOnly = true;
                }
            }
        }