I'm trying to do something a little perverse: I am creating a class that converts a delimited text document into an Excel spreadsheet, with several flags and options that give it a lot of power.
I created a windows forms app to do this, using ultragrid and ultragridexcelexporter, so that I can create spreadsheets on a machine that doesn't have Excel on it, and now I want to do it in a class which has no form at all.
How can I (can I?) instantiate a grid programmatically in this class, fill it, export it, without a form?
Alternatively, I guess I could add a form to the class, but ideally I'd rather not.
Thanks.
Hi Samuel,
There's no reason why you can't do this. The grid doesn't need a form in order to work, nor does the exporter.
However, there is one caveat - the grid does use the Form in order to get a BindingContext. So if you create a grid control in code, it will have no BindingContext and binding the grid to a data source will result in an empty grid with no rows. So you need to assign a BindingContext to the grid before you set it's DataSource. This is very easy to do:
UltraGrid grid = new UltraGrid(); // Without this, the grid will hav 0 rows. grid.BindingContext = new BindingContext(); DataTable dt = DummyDataCreator.GetData(); grid.SetDataBinding(dt, null); Debug.WriteLine(grid.Rows.Count);
UltraGrid grid = new UltraGrid();
// Without this, the grid will hav 0 rows. grid.BindingContext = new BindingContext();
DataTable dt = DummyDataCreator.GetData(); grid.SetDataBinding(dt, null);
Debug.WriteLine(grid.Rows.Count);
There might be other cases where the grid doesn't update because it never paints. For example, if you apply Filtering, the grid typically processes the filters Asynchronously on a paint. But in most cases, it should also update when someone (like the exporter) asks for the rows. So you probably won't have to do anything. But if you run into a weird case where you sorted or filtered or did something to the grid and the export doesn't reflect that, you might have to force the grid to update by calling grid.Refresh or grid.Update, or something along those lines. But I suspect you won't need to.
SoHello.
I've done this:
Ultragrid1 = New UltraGrid UltraGridExcelExporter1 = New UltraGridExcelExporter
grid.BindingContext = new BindingContext() - doesn't 'work' - i.e. VB.Net/VS doesn't recognize it.
Quite obviously, I'm missing something.
Your class library probably doesn't reference System.Windows.Forms.dll by default, so you just need to add the reference to that assembly.
That worked, thanks. Still working through some issues, though. I'd stolen a lot of code from the stand-alone winforms prog, and moved it into this DLL, and this is causing me a problem:
Private Sub UltraGrid1_InitializeLayout(sender As Object, e As Infragistics.Win.UltraWinGrid.InitializeLayoutEventArgs) Handles Ultragrid1.InitializeLayout
The error is: "Handles clause requires a WithEvents variable deined in the containing type or one of its base types."
As you can see, I'm ever so slightly out of my dept here :-)
This error message is pretty self-explanatory. In VB, in order to be able to hook events on a control, you have to declare it WithEvents.
Change:
Ultragrid1 = New UltraGrid
To
WithEvents Ultragrid1 = New UltraGrid