Skip to content

Copy one grid to another grid

New Discussion
Adrian
Adrian asked on Jul 1, 2010 6:51 PM

Is there way to copy a grid to another grid without it being a pointer to its originator?

Example:

UltraGrid tmpGrid = SourceGrid;

Using this snippet in my code makes tmpGrid a pointer to the SourceGrid, however what I want to do is simply assign all the data and column settings( group-by, filters, and etc), essentially assign every aspect of the SourceGrid to the tmpGrid without it being a pointer. Is this possible? Sorry if this sounds like a stupid question, but I had to ask.

 

Sign In to post a reply

Replies

  • 0
    Mike Saltzman
    Mike Saltzman answered on Oct 8, 2008 2:14 PM

    If the grid has a Clone method, you could try that. But I suspect it does not. 

    What you can do is create a new grid, set the DataSource and DataMember, then save the DisplayLayout on the original grid and load it into the new grid. That should cover almost everything. You might have to copy some other root-level properties of the grid. 

    • 0
      Adrian
      Adrian answered on Oct 8, 2008 2:52 PM

      Hello Mike,

       The grid does not have a clone method like you said. So I went ahead and tried setting the DataSource and DataMember of the new grid to the source grid's DataSource and DataMember as you suggested. I believe these are the two items I need to set in order to get the data from the source grid into the new grid, however it is not working for me.

      Is there way to programmatically copy the rows from the source grid to the new grid?

      • 0
        Mike Saltzman
        Mike Saltzman answered on Oct 8, 2008 7:18 PM
        • I believe these are the two items I need to set in order to get the data from the source grid into the new grid, however it is not working for me.
          What’s not working? Settings the DataSource and DataMember on the grid definitely works. If it’s not working, then something is wrong.
        • Is there way to programmatically copy the rows from the source grid to the new grid?
          I’m not sure what you mean by this. The grid cannot operate without a data source. So you have to set the DataSource on the grid. You can set it to the same data source as the original grid. Or you could set it to a new data source. But there must be a data source of some kind.
      • 0
        Wolfgang
        Wolfgang answered on Nov 28, 2009 3:51 AM

        I have the same issue:

        This code doesn't work (Excel sheet is emtpy, ds.tables(0).rows.count shows 50!):

         
        Private Function PrepareGrid4Export(ByVal locgrid As UltraWinGrid.UltraGrid) As UltraWinGrid.UltraGrid

        Dim NewGrid As New UltraWinGrid.UltraGrid Dim strDisplayout As String = "TEST.xml" My.Computer.FileSystem.DeleteFile(strDisplayout)

        locgrid.DisplayLayout.SaveAsXml(strDisplayout)

        NewGrid.DataSource = locgrid.DataSource
        NewGrid.DataMember = locgrid.DataMember
        NewGrid.DataBind()

        NewGrid.DisplayLayout.LoadFromXml(strDisplayout)
         

        exporter.Export(NewGrid,  "TEST.xls")

        Trying with

        exporter.Export(locgrid, "TEST.xls")

        the excel is filles with 50 rows!

         

        any ideas?

      • 0
        Mike Saltzman
        Mike Saltzman answered on Dec 1, 2009 6:49 PM

        This is not related to the original post here as far as I can see. But it's not working because your grid is not on a form. This means the grid will not be disposed along with your application and it also means that it will have no BindingContext. So you should add your grid to a form or set it's BindingContext to the BindingContext of the form or a new BindingContext.

      • 0
        Justin
        Justin answered on Jul 1, 2010 6:49 PM

        Bit of an old post but I've been looking for the solution to this and have come up with this…  It copied the data and layout but doesn't do the groupby.. I suspect the save layout code from above will do that bit but anyway..

            Sub LoadUltraGrid(ByVal grd As UltraWinGrid.UltraGrid)

                Dim tbl As New DataTable
                Dim tblcol As New DataColumn
                For Each col As UltraWinGrid.UltraGridColumn In grd.DisplayLayout.Bands(0).Columns
                    If Not col.Hidden Then
                        tblcol = New DataColumn
                        With tblcol
                            .ColumnName = col.Key
                            .DataType = GetType(String)
                            .Caption = col.Header.Caption
                        End With
                        tbl.Columns.Add(tblcol)
                    End If
                Next

                Dim row As DataRow
                For Each ugrow As UltraWinGrid.UltraGridRow In grd.Rows
                    row = tbl.NewRow
                    For Each col As DataColumn In tbl.Columns
                        row.Item(col.ColumnName) = ugrow.Cells(col.ColumnName).Value
                    Next
                    tbl.Rows.Add(row)
                Next

                Dim i As Integer = 0

                ugrdExport.DataSource = tbl

                For Each col As UltraWinGrid.UltraGridColumn In grd.DisplayLayout.Bands(0).Columns
                    If Not col.Hidden Then
                        ugrdExport.DisplayLayout.Bands(0).Columns(col.Key).Header.VisiblePosition = grd.DisplayLayout.Bands(0).Columns(col.Key).Header.VisiblePosition
                    End If
                Next

                With ugrdExport
                    .DisplayLayout.Override.CellClickAction = UltraWinGrid.CellClickAction.EditAndSelectText
                    .DisplayLayout.ViewStyleBand = UltraWinGrid.ViewStyleBand.OutlookGroupBy
                    .DisplayLayout.Override.AllowRowFiltering = DefaultableBoolean.True
                    .DisplayLayout.Bands(0).PerformAutoResizeColumns(False, UltraWinGrid.PerformAutoSizeType.AllRowsInBand)
                End With

            End Sub

      • 0
        Justin
        Justin answered on Jul 1, 2010 6:51 PM

        Forgot to say, the above code works when you have added unbound columns unlike the datasource method offered by Mike

  • You must be logged in to reply to this topic.
Discussion created by
Favorites
Replies
Created On
Last Post
Discussion created by
Adrian
Favorites
0
Replies
7
Created On
Jul 01, 2010
Last Post
15 years, 8 months ago

Suggested Discussions

Tags

Created by

Created on

Jul 1, 2010 6:51 PM

Last activity on

Feb 25, 2026 10:44 AM