Exporting range to datatable & Customizing FillSeries Event

Good day,

I am looking to select a range of cells and export that range to a datatable (much like the spreadsheet control can do). Is there a function to do this? I see nothing in the documentation about the subject.

Also, I would like to customize the fill series event slightly by having the cells highlight with a certain color when a series is filled. Is it possible to customize this event?

3 Replies

SN Sindhu Nagarajan Syncfusion Team April 11, 2018 01:16 PM UTC

Hi Travis, 

Thanks for contacting Syncfusion support. 


Query  
Response  
To select a range of cells and export that range to a datatable 
Inorder to export the selected range of cells to excel, SelectedExport() method is used. Please refer to the below code, 
 
Code Example 
GridExcelConverterControl ExcelAdv = new GridExcelConverterControl(); 
SaveFileDialog saveFileDialog = new SaveFileDialog(); 
saveFileDialog.Filter = "Files(*.xlsx)|*.xlsx|Files(*.xls)|*.xls"; 
saveFileDialog.DefaultExt = ".xlsx"; 
 
if (saveFileDialog.ShowDialog() == DialogResult.OK) 
{ 
    ExcelAdv.SelectedExport(this.gridControl1.Model, saveFileDialog.FileName, ConverterOptions.Default); 
} 
 
Please refer to the dashboard sample that is available in your local machine from the below location, 
 
Sample Location: <Installed Location>\Syncfusion\EssentialStudio\<Installed version>\Windows\Grid.Windows\Samples\Exporting\Exporting Demo\CS 
 
Please refer to the below UG link to know about exporting selected range of cells to excel, 
 To customize the fill series event slightly by having the cells highlight with a certain color when a series is filled. 
As per our current  support, there is no event to customize the selected cells  after FillSeries is applied to the Gridcells. However, you can set the back color of the selected  range of cells before the FillSeries is applied, by handling MouseUp event. Please make use of the below code and sample, 

Code Example 
private void GridControl1_MouseUp(object sender, MouseEventArgs e) 
{ 
     int row, col; 
    gridControl1.PointToRowCol(new Point(e.X, e.Y), out row, out col); 
     GridRangeInfo r = gridControl1.Selections.Ranges.ActiveRange; 
     if (r.Bottom != row && r.Right != col) 
     { 
         int top = r.Top; int right = r.Right; 
         for (int i = top; i <= row; i++) 
         { 
             for (int j = right; j <= col; j++) 
             { 
                 gridControl1[i, j].BackColor = Color.Red; 
             } 
         } 
     } 


Please let us know if you have any other queries. 

Regards, 
Sindhu  



TC Travis Chambers April 11, 2018 05:37 PM UTC

Thank you, however, I am not interested in exporting the grid to Excel, but importing the selected range to a dataTable so I can then import it into another business object, such as another girdcontrol, chart, etc. The spreadsheet control has a build in method for this and I would like to do the same in the gridcontrol if possible.


SN Sindhu Nagarajan Syncfusion Team April 12, 2018 03:15 PM UTC

Hi Travis, 
 
Thanks for the update. 
 
 
GridControl does not have any direct function to export selected data in the grid to datatable like exporting the data to excelsheet. However, the selected range of values can be retrieved by iterating. The selected range is obtained by using SelectedRanges.ActiveRange property.  The values thus obtained are exported to the datatable. We have prepared the sample as per your requirement. Please refer to the below code and sample, 
 
Code Example 
  private void button1_Click(object sender, EventArgs e) 
  { 
      var range = this.gridControl1.Model.SelectedRanges.ActiveRange; 
      int col = range.Right - range.Left + 1; 
      int row = range.Bottom - range.Top + 1; 
      int top = range.Top; 
      int left = range.Left; 
      int Bottom = range.Bottom; 
      int right = range.Right; 
 
      int l = left; 
      int t = top; 
      DataTable dt = new DataTable(); 
      for (int c = 0; c < col; c++) 
      { 
          string colName = gridControl1.Model[0, l].Text; 
          l++; 
          dt.Columns.Add(colName); 
      } 
      l = left; 
      for (int i = 0; i < row; i++) 
      { 
          DataRow dr = dt.NewRow(); 
          for (int j = 0; j < col; j++) 
          { 
              dr[j] = this.gridControl1.Model[t, l].Text; 
              l++; 
          } 
          dt.Rows.Add(dr); 
          t++; 
          l = left; 
      } 
      gridControl1.RowCount = dt.Rows.Count; 
      gridControl1.ColCount = dt.Columns.Count;             
  } 
 
 
 
Please let us know if you have any other queries. 
 
 
Regards, 
Sindhu  


Loader.
Up arrow icon