添加组合框到一个DataGridView编程(Adding ComboBoxes to a Data

2019-10-29 05:26发布

我需要一些帮助DataGridView类Windows窗体 ,我不知道这是否只是我,但我真的与此类挣扎编程。

你可能会认为,一个简单的任务,因为添加Column作为ComboBoxDataGridView将是小菜一碟,但显然它不是! 我需要以编程方式做到这一点的原因是因为我需要在DataGridView在选择下拉项目将依赖于一个或多个其他等选择每行的多个组合框...

这是我如何初始化我DataGridView

private void InitializeDataGridView()
{
    _objectDataGridView.AutoGenerateColumns = false;
    _objectDataGridView.Columns.Add(new DataGridViewTextBoxColumn
                                        {
                                                DataPropertyName = "Id",
                                                HeaderText = "Id",
                                                ValueType = typeof(int)
                                        });
    _objectDataGridView.Columns.Add(new DataGridViewTextBoxColumn
                                        {
                                                DataPropertyName = "Name",
                                                HeaderText = "Name",
                                                ValueType = typeof(string),
                                                AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill
                                        });
    var objectTypeComboBoxColumn = new DataGridViewComboBoxColumn
                                       {
                                               DataPropertyName = "Type",
                                               HeaderText = "Object Type",
                                               ValueType = typeof(ObjectType),
                                               ValueMember = "Id",
                                               DisplayMember = "Name",
                                               DisplayStyle = DataGridViewComboBoxDisplayStyle.ComboBox
                                       };
    _objectDataGridView.Columns.Add(objectTypeComboBoxColumn);
    var containerComboBoxColumn = new DataGridViewComboBoxColumn
                                      {
                                              DataPropertyName = "Container",
                                              HeaderText = "Container",
                                              ValueType = typeof(Container),
                                              ValueMember = "Id",
                                              DisplayMember = "Name",
                                              DisplayStyle = DataGridViewComboBoxDisplayStyle.ComboBox
                                      };
    _objectDataGridView.Columns.Add(containerComboBoxColumn);
}

到现在为止一切都很好。 现在,当我点击一个TreeNodeDecorator (源自TreeNode的) TreeViewOnBeforeSelect由本人听了事件触发。 然后我用TreeNodeDecorator.Id检索相关对象的列表,通过功能NHibernate ( ORM )。

随着新检索到的名单我想清楚了,然后填写_objectDataGridViewRows 。 在该方法中聆听OnBeforeSelect事件我再试着设置了_objectDataGridViewDataSource这样的。

private void SetDataSource(IEnumerable<Object> assignedObjects)
{
    var dataTable = new DataTable();
    dataTable.Columns.Add("Id");
    dataTable.Columns.Add("Name");
    dataTable.Columns.Add("Type");
    dataTable.Columns.Add("Container");
    foreach(var assignedObject in assignedObjects)
    {
        dataTable.Rows.Add(assignedObject.Id,
                           assignedObject.Name,
                           assignedObject.ObjectType,
                           assignedObject.Container);
    }
    _objectDataGridView.DataSource = dataTable;
}

但是,我还没有得到设置为可选项目DataGridViewComboBoxColumns 。 我可以设置一组静态当我创建的可选项目DataGridViewComboBoxColumns ,但我需要每行,并根据在同一行中的其它组合框选择的项目进行动态地和单独添加的项目。 而且我真的不知道如何做到这一点。

我想有我需要听一个事件,并从那里充满DataGridViewComboBoxColumns.ItemsDataGridViewComboBoxColumns.DataSource与正确的项目。 然后我还需要听的时候在这些选择的变化ComboBoxes ,并填写/相关改变了选择的项目ComboBoxes

Answer 1:

我最终加入RowsDataGridView是这样的:

private void SetDataSource(IEnumerable<Object> assignedObjects, 
                           List<Container> subContainersAssociatedWithSystem)
{
    _objectDataGridView.Rows.Clear();
    foreach (var assignedObject in assignedObjects)
    {
        var newRowIndex = _objectDataGridView.Rows.Add();
        var row = _objectDataGridView.Rows[newRowIndex];
        row.Cells["Id"].Value = assignedObject.Id;
        row.Cells["Name"].Value = assignedObject.Name;
        var containerColumn = (DataGridViewComboBoxCell)row.Cells["Container"];
        containerColumn.DataSource = subContainersAssociatedWithSystem;
        containerColumn.Value = assignedObject.Container.Id;
        var objectTypeColumn = (DataGridViewComboBoxCell)row.Cells["Type"];
        objectTypeColumn.DataSource = new List<ObjectType> {assignedObject.ObjectType};
        objectTypeColumn.Value = assignedObject.ObjectType.Id;
    }
}

然后听EditingControlShowing这样的事件:

private void InitializeDataGridView()
{
    ...
    _objectDataGridView.EditingControlShowing += (sender, e) =>
    {
        var cb = e.Control as ComboBox;
        if (_objectDataGridView.CurrentCellAddress.X == objectTypeComboBoxColumn.DisplayIndex && cb != null)
        {
            var value = _objectDataGridView[containerComboBoxColumn.DisplayIndex, _objectDataGridView.CurrentCellAddress.Y].Value;
            if(value != null)
            {
                var containerId = (int)value;
                using(var dao = _daoFactory.Create(_daoAdminRole))
                {
                    var container = dao.Get<Container>(containerId);
                    var objectTypes = dao.GetByQueryObject(new ObjectTypeQueryObject {ContainerType = new ContainerType {Id = container.ContainerType.Id}});
                    cb.DataSource = objectTypes;
                }
            }
        }
    };
    ...
}

现在,当我点击objectTypeComboBoxColumnDataSource获取设置有根据在选择正确的项目containerComboBoxColumn



文章来源: Adding ComboBoxes to a DataGridView programatically