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
310
SetDataBinding is not working on adhoc UltraGrid
posted

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

Parents
  • 310
    Verified Answer
    Offline posted

    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();
          _wb.Save( fileName );
    
    

    Thanks and regards,

    Kent

Reply Children
No Data