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.
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.
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?
dawsona0526 said: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.
dawsona0526 said: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.
Forgot to say, the above code works when you have added unbound columns unlike the datasource method offered by Mike
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
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.
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.DataSourceNewGrid.DataMember = locgrid.DataMemberNewGrid.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?