Hi,
I have an ultragrid with 2 bound columns on it. I want to be able to create and remove columns at runtime.
When an event fires in my application, I need to refresh the grid's columns. I want to delete all the unbound columns from my grid, and then add in columns again. The columns are being added to an UltraGridGroup when they are created, by setting
column.Group = grid.DisplayLayout.Bands(0).Groups("grpName")
grid.DisplayLayout.Bands(0).Columns.ClearUnbound()
but that doesn't seem to work correctly. It looks like they are being deleted, but maybe the Group is holding on to them (???). When I try to add the columns back in, I get an error that the Key already exists when I try to set the column.Group again. My column keys are unique, so I don't know what's wrong.
Any thoughts?
Thanks,
~Karen
Karen,
If your columns are bound, then by definition they are owned and controlled by the underlying data source, which is why calling ClearUnbound() is not doing anything. It's hard to say what's going on with your errors here, but if you want to actually *remove* a bound column, it would have to be removed from the data source. Should you want to maintain the data source structure, you could simply set the column's Hidden property to true.
-Matt
Thanks Matt, but the columns I'm trying to get rid of are NOT bound. I have 2 Bound columns columns, but then a few unbound columns that I want to clear out. I have attached a sample grid that shows my problem. Try running it, and then change the number of rows or columns that are supposed to show. You'll hit the error that way.
Thanks again for the help,
You're getting an exception because you're trying to add a column to a group that already contains a column with that key. The code I changed in the LayoutGrid method is:
'create columns for each column key Dim col As Infragistics.Win.UltraWinGrid.UltraGridColumn For Each key As String In myColumnKeys If grid.DisplayLayout.Bands(0).Columns.Exists(key) = False Then col = grid.DisplayLayout.Bands(0).Columns.Add(key) col.DataType = GetType(System.Boolean) col.DefaultCellValue = False col.Style = Infragistics.Win.UltraWinGrid.ColumnStyle.CheckBox Dim group As UltraGridGroup = grid.DisplayLayout.Bands(0).Groups("X Vals") If group.Columns.Exists(col.Key) = False Then col.Group = group End If End If Next key
I hope I solved the problem - the datatable should be created as a new object every time I want to change the columns in grid group.
I have the same problem as Karen, I tried to solve it as you wrote in the post, but it doesn't work:( It's my code :
dataTable is of type DataTable. I want only to get rid of these grid columns, which have index greater than 2. When I launch my form, it works fine, but if I reload it because of data change, it throws an exception attached. It seems this exception occurs in grid.OnPaint event It's my code:
UltraGridBand band = grid.DisplayLayout.Bands[0];UltraGridGroup group = band.Groups[1];UltraGridColumn col;for (int i = 3; i < dataTable.Columns.Count; i++){ String colKey = dataTable.Columns[i].Caption; if (!grid.DisplayLayout.Bands[0].Columns.Exists(colKey)) { col = grid.DisplayLayout.Bands[0].Columns.Add(colKey); col.DataType = typeof(decimal); } else col = grid.DisplayLayout.Bands[0].Columns[colKey]; if (!group.Columns.Exists(colKey)) col.Group = group;}
~Gosia