COLUMN INDEX

How to get Field name from Column Index No. ?

How to get current column's index no. ?

How to exclude columns for exporting to pdf/excel when the said columns was hidden in sfdatagrid ?






1 Reply

FP Farjana Parveen Ayubb Syncfusion Team August 13, 2018 11:26 AM UTC

Hi Deepak, 
 
Thanks for contacting Syncfusion support. 
 
Please find the updated in the below table, 
 
Query 
Response 
How to get Field name from Column Index No. ? 
 
You can get the field name of the column by using the SfDataGrid.Columns collection and resolve the column index based on View by using ResolveToGridVisibleColumnIndex method. Please refer the below code example, 
 
Code Example 
 
private void FiledName(object sender, System.EventArgs e) 
{ 
    var index = sfDataGrid.TableControl.ResolveToGridVisibleColumnIndex(5); 
    if (index >= 0) 
        MessageBox.Show(sfDataGrid.Columns[index].HeaderText.ToString()); 
} 
 
 
How to get current column's index no. ? 
 
You can get the current column index by using SfDataGrid.CurrentCell.ColumnIndex, please refer the below code example 
 
Code Example 
private void CurrentColumnIndex(object sender, System.EventArgs e) 
{ 
    if (sfDataGrid.CurrentCell != null) 
        MessageBox.Show(sfDataGrid.CurrentCell.ColumnIndex.ToString()); 
} 
 
 
How to exclude columns for exporting to pdf/excel when the said columns was hidden in sfdatagrid ? 
 
Export To PDF 
 
You can exclude the hidden columns when export the SfDataGrid to Pdf document by adding the columns in PdfExportingOptions.ExcludeColumns collection, please refer the below code and UG link 
 
Code Example 
private void ExportToPDF(object sender, System.EventArgs e) 
{ 
    PdfExportingOptions options = new PdfExportingOptions(); 
    foreach (var columns in sfDataGrid.Columns) 
    { 
        if (!columns.Visible) 
            options.ExcludeColumns.Add(columns.MappingName); 
    } 
 
    var document = sfDataGrid.ExportToPdf(options); 
    SaveFileDialog saveFileDialog = new SaveFileDialog 
    { 
        Filter = "PDF Files(*.pdf)|*.pdf" 
    }; 
    if (saveFileDialog.ShowDialog() == DialogResult.OK) 
    { 
        using (Stream stream = saveFileDialog.OpenFile()) 
            document.Save(stream); 
        if (MessageBox.Show("Do you want to view the Pdf file?", "Pdf file has been created", MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.Yes) 
            System.Diagnostics.Process.Start(saveFileDialog.FileName); 
    }  
} 
 
 
Export To Excel 
 
You can exclude the hidden columns when export the SfDataGrid to excel document by adding the columns in ExcelExportingOptions.ExcludeColumns collection, please refer the below code and UG link 
 
 
Code Example 
private void ExportToExcel(object sender, System.EventArgs e) 
{ 
    var options = new ExcelExportingOptions(); 
    foreach (var columns in sfDataGrid.Columns) 
    { 
        if (!columns.Visible) 
            options.ExcludeColumns.Add(columns.MappingName); 
    } 
    var excelEngine = sfDataGrid.ExportToExcel(sfDataGrid.View, options); 
    var workBook = excelEngine.Excel.Workbooks[0]; 
 
    SaveFileDialog saveFilterDialog = new SaveFileDialog 
    { 
        FilterIndex = 2, 
        Filter = "Excel 97 to 2003 Files(*.xls)|*.xls|Excel 2007 to 2010 Files(*.xlsx)|*.xlsx|Excel 2013 File(*.xlsx)|*.xlsx" 
    }; 
 
    if (saveFilterDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK) 
    { 
        using (Stream stream = saveFilterDialog.OpenFile()) 
        { 
            if (saveFilterDialog.FilterIndex == 1) 
                workBook.Version = ExcelVersion.Excel97to2003; 
            else if (saveFilterDialog.FilterIndex == 2) 
                workBook.Version = ExcelVersion.Excel2010; 
            else 
                workBook.Version = ExcelVersion.Excel2013; 
            workBook.SaveAs(stream); 
        } 
 
        if (MessageBox.Show(this.sfDataGrid, "Do you want to view the workbook?", "Workbook has been created", 
                            MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.Yes) 
            System.Diagnostics.Process.Start(saveFilterDialog.FileName); 
    } 
} 
 
 
 
 
  
Regards, 
Farjana Parveen A 


Loader.
Up arrow icon