Skip to content

Infragistics Community Forum / Desktop / Ultimate UI for Windows Forms / Adding and removing columns at runtime

Adding and removing columns at runtime

New Discussion
Karen
Karen asked on Jan 14, 2009 10:42 AM

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”)

 

I tried using

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

 

Sign In to post a reply

Replies

  • 0
    Matt Snyder
    Matt Snyder answered on Aug 25, 2008 4:46 PM

     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

    • 0
      Karen
      Karen answered on Aug 25, 2008 4:56 PM

      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,

      ~Karen

      • 0
        Matt Snyder
        Matt Snyder answered on Aug 25, 2008 5:22 PM

         Karen,

        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

        -Matt

      • 0
        Malgorzata
        Malgorzata answered on Jan 14, 2009 10:02 AM

        Hi,

         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;
        }

        Thanks,

        ~Gosia

      • 0
        Malgorzata
        Malgorzata answered on Jan 14, 2009 10:42 AM

         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.

    • 0
      Karen
      Karen answered on Aug 25, 2008 5:05 PM

      Hi Matt,

      One other note on this problem…

      If I don't try to put my columns in UltraGridGroups, I don't have any problems.

  • You must be logged in to reply to this topic.
Discussion created by
Favorites
Replies
Created On
Last Post
Discussion created by
Karen
Favorites
0
Replies
6
Created On
Jan 14, 2009
Last Post
17 years, 7 months ago

Suggested Discussions

Tags

Created by

Created on

Jan 14, 2009 10:42 AM

Last activity on

Feb 19, 2026 9:20 AM