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.
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...
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);
}
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).
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;
}
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.
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
.
I ended up adding the
Rows
to theDataGridView
like this:And then listen to the
EditingControlShowing
event like this:Now when I click the
objectTypeComboBoxColumn
theDataSource
gets set with the correct items according to the selection in thecontainerComboBoxColumn
.