以编程方式将ComboBox添加到DataGridView

时间:2013-08-14 06:56:10

标签: c# .net winforms datagridview datagridviewcombobox

我需要DataGridViewWindows Forms课程的一些帮助,我不知道它是否仅仅是我,但我正在以编程方式努力学习这门课程。

您会认为将Column作为ComboBox添加到DataGridView这样的简单任务将是小菜一碟,但显然不是!我需要以编程方式执行此操作的原因是因为我需要在DataGridView中的每一行上有多个ComboBox,其中可选择的下拉项将取决于其中一个或多个中的选择等... < / p>

以下是我初始化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);
}

到目前为止,一切都很顺利。现在,当我点击TreeNode TreeView事件中的TreeNodeDecorator(源自OnBeforeSelect)时,我正在收听该事件。然后我使用TreeNodeDecorator.Id通过Fluent NHibernateORM)检索关联对象列表。

使用新检索的列表,我想清除,然后使用新的_objectDataGridView填充Rows。 在收听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中的可选项。

1 个答案:

答案 0 :(得分:0)

我最终将Rows添加到DataGridView,如下所示:

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;
                }
            }
        }
    };
    ...
}

现在,当我点击objectTypeComboBoxColumn DataSource根据containerComboBoxColumn中的选择设置了正确的项目。