以编程方式将ComboBox添加到DataGridView [英] Adding ComboBoxes to a DataGridView programatically

查看:123
本文介绍了以编程方式将ComboBox添加到DataGridView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要有关 <$ c $的帮助 Windows窗体<中的c> DataGridView 类/ a>,我不知道是否只有我一个人,但是我确实在编程上为此课程苦苦挣扎。

I need some help with the DataGridView class in Windows Forms, I don't know if it's just me but I am really struggling with this class programmatically.

您会认为,添加作为 ComboBox DataGridView 真是小菜一碟,但显然不是!我需要以编程方式执行此操作的原因是,我在DataGridView的每一行上都需要多个ComboBox,其中可选的下拉项将取决于其他一项或多项的选择,等等...

You would think that a simple task as adding a Column as a ComboBox to a DataGridView would be a piece of cake, but apparently it's not! The reason I need to do it programmatically is because I need multiple ComboBoxes on each Row in the DataGridView where the selectable drop down items will be dependent on selections in one or more of the others etc...

这是我初始化 DataGridView

Here is how I initialize my 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 )在 TreeView OnBeforeSelect 事件触发。然后,我使用 TreeNodeDecorator.Id 通过流利的NHibernate ORM )。

Up to this point everything is well and good. Now when I click a TreeNodeDecorator (derived from TreeNode) in a TreeView the OnBeforeSelect event fires of which I am listening to. I then use the TreeNodeDecorator.Id to retrieve a list of associated objects, via Fluent NHibernate (ORM).

使用新检索的列表,我想清除,然后用新的填充 _objectDataGridView
在侦听 OnBeforeSelect 事件,然后尝试设置 _objectDataGridView DataSource 这样。

With the newly retrieved list I want to clear and then fill the _objectDataGridView with new Rows. In the method listening to the OnBeforeSelect event I then try to set the _objectDataGridView.DataSource like this.

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 ,但是我需要根据每行中其他组合框中的选定项目动态地,单独地逐行动态添加这些项目。而且我真的不确定如何做到这一点。

But I haven't gotten to set the selectable items for the DataGridViewComboBoxColumns. I could have set a static set of selectable items when I created the DataGridViewComboBoxColumns, but I need those items to be added dynamically and individually per row and in accordance with selected items in other comboboxes on the same row. And I'm really not sure of how to do that.

我想我需要听一个事件,然后在其中填充 DataGridViewComboBoxColumns.Items 带有正确项目的DataGridViewComboBoxColumns.DataSource 。然后,我还需要听这些 ComboBoxes 中的选择何时更改,并填充/更改相关的 ComboBoxes 中的可选项目。 。

I guess there is an event I need to listen to, and from there fill the DataGridViewComboBoxColumns.Items or DataGridViewComboBoxColumns.DataSource with the correct items. I then also need to listen to when selections changes in these ComboBoxes and fill / change up the selectable items in related ComboBoxes.

推荐答案

我最终将添加到 DataGridView 像这样:

I ended up adding the Rows to the DataGridView like this:

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 这样的事件:

And then listen to the EditingControlShowing event like this:

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 根据 containerComboBoxColumn DataSource 设置正确的项目>。

Now when I click the objectTypeComboBoxColumn the DataSource gets set with the correct items according to the selection in the containerComboBoxColumn.

这篇关于以编程方式将ComboBox添加到DataGridView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆