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;
}
}
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;
}
}
}
};
...
}
Now when I click the objectTypeComboBoxColumn
the DataSource
gets set with the correct items according to the selection in the containerComboBoxColumn
.