Attempting to create a multi-select drop-down, using an ultratexteditor, a dropdowneditorbutton, and an ultragrid, I am having trouble accessing the rows and columns of the grid.
I have tried adding unbound columns, using an ultradatasource, and numerous other approaches. Currently, I created a form with a grid and an ultradatasource, and then ported the designer code into my control initialization.
No matter what I try, _grid.Bands[0].Columns.Count is always 0. _grid.Rows.Count is also always 0. However, the dropdown grid does have rows and columns, and the multi-select feature works well. I need access to the columns so that I can change the column header. I tried accessing via rows to access cells[0].Column.Header.
I have also reflected into the code to try to determine if a flag is set during initialization that would prevent the property collections from initializing, but I can't seem to find anything.
Can you please help me understand what I might need to do to access the grid columns? I am posting the initialization code, which is a little lengthy.
private DropDownEditorButton _dropDownEditorButton;
private BaseGrid _grid;
{
_grid.InitializeGrid();
_grid.DataSource = _dataSource;
((ISupportInitialize)(_grid)).BeginInit();
// create data bands and columns
var displayDataColumn = new UltraDataColumn("DisplayMember") {DataType = typeof (string)};
_dataSource.Band.Columns.AddRange(new object[{selectedDataColumn, displayDataColumn});
selectedGridColumn.Header.VisiblePosition = 0;
var displayGridColumn = new UltraGridColumn("DisplayMember");
displayGridColumn.Header.VisiblePosition = 0;
IndentationGroupByRow = 0,
IndentationGroupByRowExpansionIndicator = 0,
Override =
AllowRowLayoutColMoving = Infragistics.Win.Layout.GridBagLayoutAllowMoving.None,
}
};
_grid.DisplayLayout.BandsSerializer.Add(band);
_grid.Height = 150;
_grid.DisplayLayout.ScrollBounds = ScrollBounds.ScrollToFill;
/// <summary>
/// Sets data source
/// </summary>
/// <typeparam name="T">Object Type</typeparam>
/// <param name="dataSource">IList of types</param>
foreach (var item in dataSource)
_grid.SetDataBinding(bindingSource, null);
_grid.Rows[0].Cells[1].Column.Header.Caption = DisplayCaption;
In this last routine, Rows.Count always == 0, even though the data does bind correctly.
I am not actually on a form, but rather extending the texteditor control. This control implements IDisposable, where I dispose all of the disposable objects.
The issue that I had was that the properties are not loaded until the InitializeLayout event fires. So, I was trying to access columns and rows before the grid performed the necessary binding. I moved my cosmetic code to that event and things are now working correctly.
Thanks!
My first guess is that since you are creating the grid in code and never parenting it to anything, it has no BindingContext. Try adding the grid to the form's Control's collection. This is a good idea anyway, because the form's Dispose method will dispose the grid when the form is disposed.