Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
910
Using a grid in a class library
posted

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.

Parents
No Data
Reply
  • 469350
    Suggested Answer
    Offline posted

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

    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.

Children