I am trying to create a component in our application that encapsulates the UltraGridExcelExporter. This control works so well for grid data... I was looking to use it to simplify Excel file creation for any data source. Here is the code I would like to write, but it proves to be problematic in as much as getting the data to bind to the dynamically created instance of the UltraGrid. Debugging the below code I find that SetDataBinding does not bind to the grid. What am I missing?
_wb = new Workbook(); var ws = _wb.Worksheets.Add( sheetName );
var dt = GetDataTable(); var ultraGrid = new UltraGrid(); ultraGrid.DataBindings.Clear(); ultraGrid.SetDataBinding( dt, "" ); ultraGrid.DisplayLayout.Bands[0].Override.RowAlternateAppearance.BackColor = Color.LightGray; var excelExporter = new UltraGridExcelExporter(); excelExporter.Export( ultraGrid, ws ); excelExporter.Dispose();
_wb.Save( fileName );
Thanks in advance,
Kent
Well I found the problem. I took a guess as to the issue since the code works from a regular grid already on the form. This is a bug common to many controls in Microsoft. For some reason, you cannot render the control without being on a form AND the form must have been "Show"n before you can use the control correctly. This bug shows up for anyone doing unit testing of forms using nUnit. The code works fine as amended below. (Note, in place of calling form.Show(), form.Hide() works equally well).
var_wb = new Workbook(); var _form = new Form();
var ws = _wb.Worksheets.Add( sheetName );
var dt = GetDataTable(); var ultraGrid = new UltraGrid();
_form.Controls.Add(ultraGrid); _form.Hide();
ultraGrid.DataBindings.Clear(); ultraGrid.SetDataBinding( dt, "" ); ultraGrid.DisplayLayout.Bands[0].Override.RowAlternateAppearance.BackColor = Color.LightGray; var excelExporter = new UltraGridExcelExporter(); excelExporter.Export( ultraGrid, ws ); excelExporter.Dispose();
Thanks and regards,
Hello ,
I am glad to hear that you were able to solve your issue. I just want to make clarification, when UltraGrid is added to a Controls collection of the Form it gets the owner BindingContext which is responsible for notifying about changes of the data. So if the grid is not putted on the form it doesn’t have BindingContext and it cannot be notified about changes of the underlying data source. It is how binding works in Windows Forms.
Thank you for using Infragistics components.