This is a migrated thread and some comments may be shown as answers.

Data binding GridView in memory (Grid is not displayed)

6 Answers 500 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Sharon Eden
Top achievements
Rank 1
Sharon Eden asked on 28 Jul 2010, 01:36 PM

Hi,

I would like to send a GridView to the RadGridReportingLite in order to create a report. Since I need to change the grouping and sorting of the GridView for the report with no effect on the display, I thought of cloning the GridView, send it to the RadGridReportingLite and dispose it.

There is no method of deep copying of the GridView so what I do is:

  1. Create new GridView.
  2. Save the layout of the original GridView and load it to the new one.
  3. Create a copy of the original DataSet (which is the data source of the grid).
  4. Bind the new DataSet to the new GridView;
  5. Send the new GridView to the RadGridReportingLite.

The following code snippet demonstrates it.

// Create a new grid for the report.
RadGridView grdReport = new RadGridView();
 
// Clone the dataset with the original data.
DataSet dsReport = ((DataView)grdOriginal.DataSource).Table.DataSet.Copy();
 
// Set it as the new grid's data source.
grdReport.DataSource = dsReport.Tables[0].DefaultView;
 
// Create a stream in the memory.
using (MemoryStream stream = new MemoryStream())
{
    // Save the grid to the stream.
    grdOriginal.SaveLayout(stream);
 
    stream.Seek(0, SeekOrigin.Begin);
 
    // Load the original grid layout to the new one.
    grdReport.LoadLayout(stream);
}

When I execute this code I get an exception from the RadGridReportingLite, because the GridView is empty. The GridView is never displayed so the binding doesn’t occur. What I’m asking is how do I execute the binding explicitly (something like the DataBind() method in ASP.NET)?

BTW, is there a better way to achieve this functionality?

I’m using 2010 Q1 SP controls.

Thanks in advance,

Sharon.

6 Answers, 1 is accepted

Sort by
0
Emanuel Varga
Top achievements
Rank 1
answered on 28 Jul 2010, 02:46 PM
Hello Sharon,

I've tried your example with a simple example and it's working as expected, Please try the same thing with two different gridviews on the same form and a button and you will see that it works, the problem must be somewhere in the data source, maybe it needs some more time to bind, you could try taking a look on the DataBindingComplete event which only fires if a late binding occurred.

P.s. I've tried this with a business object collection.

Please let me know if there is something else i can do to help.

Best Regards,
Emanuel Varga
0
Sharon Eden
Top achievements
Rank 1
answered on 28 Jul 2010, 03:14 PM
Hi Emanuel,

Thanks for the quick response.

I think that the major difference between your test and my code is that my GridView is not added to any form. I believe that the Form calls some method of the GridView (or fires event) that will execute the actual binding (just setting the DataSource property does do that). I did noticed that the DataBindingComplete is not fired in my code (I've removed it from the code snippet).

I'll try your way just to make sure that this is the difference.

Sharon.

0
Emanuel Varga
Top achievements
Rank 1
answered on 28 Jul 2010, 03:27 PM
Hi Sharon,

I've tried it again this time taking data from a grid that is displayed to a newly created grid, not shown on any form, everything was fine, after everything I've added it to the controls just to display the data, sadly i cannot attach my project here but here is the code from the form, please tell me if I've misunderstood something

public partial class Form1 : Form
 {
     //Create the new grid
     private RadGridView testGrid = new RadGridView();
     private CustomObjects customDataSource = new CustomObjects();
      
     public Form1()
     {
         InitializeComponent();
         //Load data source
         LoadDataIntoDataSource();
         //Set the data source for the displayed grid
         radGridView1.DataSource = customDataSource;
     }
 
     private void radButton1_Click(object sender, EventArgs e)
     {
         testGrid.DataSource = customDataSource;
         using (MemoryStream stream = new MemoryStream())
         {
             // Save the grid to the stream.
             radGridView1.SaveLayout(stream);
 
             stream.Seek(0, SeekOrigin.Begin);
 
             // Load the original grid layout to the new one.
             testGrid.LoadLayout(stream);
         }
 
         //Test if everything succeeded
         testGrid.Location = new Point(546, 43);
         this.Controls.Add(testGrid);
     }
}

Hope this helps,
Emanuel Varga
0
Sharon Eden
Top achievements
Rank 1
answered on 28 Jul 2010, 03:54 PM
Hi Emanuel,

I've tried your example as is and it did work. However, when i removed the last line of code in your snippet:
this.Controls.Add(testGrid);

it didn't work and that is my problem.

All I need the grid for is just as a temp copy of the displayed grid which I can modify, send to the RadGridReportingLite and then dispose it. I guess I could have an ugly workaround that the grid will be added to the form as invisible control, but I'm trying to find the method/event/whatever that the form is calling to.


Thanks,
Sharon
0
Emanuel Varga
Top achievements
Rank 1
answered on 28 Jul 2010, 04:34 PM
Hello Sharon,

Sorry for before i was just checking the column, and the columns were ok, after some more digging i found that the binding only occures when you add it to a control, as you are providing a handle for the grid. I will do some more digging, but it seems like a very normal behavior to have because it's preserving memory before it's really necessary.

I will keep you posted if i will find something else.
Until then as a workaround if i were you i would just add it as a control to your main grid and remove it on the following line, everything will work fine, and if by any chance some flickering will occur you can just suspend and resume the layout.

Hope this helps,
Emanuel Varga
0
Martin Vasilev
Telerik team
answered on 02 Aug 2010, 03:52 PM
Hi guys,

I would like to suggest that you use one different approach, which avoids creating a copy of RadGridView: using SuspendLayout:
this.radGridView1.GridElement.SuspendLayout();
//do grid preparations and export to Telerik Reporting
this.radGridView1.GridElement.ResumeLayout(false);

If that does not do the trick and still an invisible instance of RadGridView is needed, you can force loading its elements by calling the LoadElementTree method. This should be enough for the exporting and adding the grid to the Controls collection could be avoided:
this.radGridView1.LoadElementTree();

Sincerely yours,
Martin Vasilev
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
Tags
GridView
Asked by
Sharon Eden
Top achievements
Rank 1
Answers by
Emanuel Varga
Top achievements
Rank 1
Sharon Eden
Top achievements
Rank 1
Martin Vasilev
Telerik team
Share this question
or