Headerstyle Filter Icon return error

I was following this in the help file.

sfDataGrid.Columns[1].HeaderStyle.FilterIcon = new Bitmap(Image.FromFile(@"..\..\FilterIcon.png"));
sfDataGrid.Columns[0].HeaderStyle.FilteredIcon = new Bitmap(Image.FromFile(@"..\..\FilteredIcon.png"));

but it give me error like out of range. could you please provide a working example?

5 Replies 1 reply marked as answer

VS Vijayarasan Sivanandham Syncfusion Team June 14, 2021 01:28 PM UTC

Hi Mark Jayvee,

Thank you for contacting Syncfusion Support.

Based on provided information we have prepared the sample for achieve your requirement. 
Sample Link: https://www.syncfusion.com/downloads/support/forum/166327/ze/Sample790819377

Please let us know if you have any concerns in this. 

Regards, 
Vijayarasan S 



MJ Mark Jayvee replied to Vijayarasan Sivanandham June 29, 2021 12:40 AM UTC

Hi Thank you for the response.


following the same example and below are my requirements.


  1. the data is save in a txt format.
  2. In forms, I have a button to insert the txt file.
  3. I will load the txt file data into sfdatagrid.
  4. when the button is click, it will load the txt file, put it into sfdatagrid with custom headerstyle filter icon.
will that be possible?


VS Vijayarasan Sivanandham Syncfusion Team June 29, 2021 01:22 PM UTC

Hi Mark Jayvee,

Thanks for the update.

Please find answer for your queries below 
Queries 
Solutions 
 
the data is save in a txt format. 
 
Your requirement can be achieved export the SfDataGrid into Excel. The exported workbook can be saved as txt file by using the SaveAs method. Please refer the below code snippet, 
private void btnSave_Click(object sender, System.EventArgs e) 
{ 
            var options = new ExcelExportingOptions(); 
            options.ExcelVersion = ExcelVersion.Excel2013; 
            var excelEngine = sfDataGrid.ExportToExcel(sfDataGrid.View, options); 
            var workBook = excelEngine.Excel.Workbooks[0]; 
            workBook.SaveAs("Sample.txt", "\t"); 
} 

For more information related to SaveAs exported excel file, please refer the user guide documentation,

UG Link: https://help.syncfusion.com/windowsforms/datagrid/exporttoexcel#export-to-csv 
 

 
In forms, I have a button to insert the txt file. 
 

You can get the txt file via OpenFileDialog and pass the path value to LoadUserListFromFile extension method in application. Please refer the below code snippet, 


private void btnLoadData_Click(object sender, System.EventArgs e) 
{ 
            OpenFileDialog openFileDialog = new OpenFileDialog(); 
            openFileDialog.Filter = "Text File|*.txt"; 
            var result = openFileDialog.ShowDialog(); 
 
            if (result != DialogResult.OK) 
                return; 
            try 
            { 
                //get the file path as string from OpenFileDialog.FileName 
                sfDataGrid1.DataSource = OrderInfo.LoadUserListFromFile(openFileDialog.FileName); 
            } 
            catch (Exception ex) 
            { 
                MessageBox.Show("Error: Could not read file from disk.  Original error: " + ex.Message); 
            }             
 } 

 


I will load the txt file data into sfdatagrid.
 
 

Your requirement can be achieved by underline model or object (OrderInfo) defined you can add a static method to it that loads the data to a list of OrderInfo and bind it to the SfDataGrid. Please refer the below code snippet, 

public class OrderInfo : INotifyPropertyChanged 
{ 
        public static List<OrderInfo> LoadUserListFromFile(string path) 
        { 
            var users = new List<OrderInfo>(); 
            decimal temp; 
            int i = 0; 
            foreach (var line in File.ReadAllLines(path)) 
            { 
                var columns = line.Split('\t'); 
                //skip the first line because its contains the Column Header details 
                if (i != 0) 
                { 
                    users.Add(new OrderInfo 
                    { 
                        OrderID = decimal.TryParse(columns[0], out temp) ? temp : default(decimal?), 
                        CustomerID = columns[1], 
                        CustomerName = columns[2], 
                        Country = columns[3], 
                        ShipCity = columns[4], 
                        IsShipped = Convert.ToBoolean(columns[5]), 
                    }); 
                } 
                i = 1; 
            } 
 
            return users; 
        } 
    } 


 
when the button is click, it will load the txt file, put it into sfdatagrid with custom headerstyle filter icon. 
 

In SfDataGrid loaded with txt file we need apply apply custom filtered icon for Column in SfDataGrid. Please refer the below code snippet, 
private void btnLoadData_Click(object sender, System.EventArgs e) 
{ 
            OpenFileDialog openFileDialog = new OpenFileDialog(); 
            openFileDialog.Filter = "Text File|*.txt"; 
            var result = openFileDialog.ShowDialog(); 
 
            if (result != DialogResult.OK) 
                return; 
            try 
            { 
                //get the file path as string from OpenFileDialog.FileName 
                sfDataGrid1.DataSource = OrderInfo.LoadUserListFromFile(openFileDialog.FileName); 
 
                //apply custom filtered icon for first Column in SfDataGrid 
                //FilteredIcon only changed for OrderId Column in SfDataGrid 
                sfDataGrid1.Columns[0].HeaderStyle.FilteredIcon = new Bitmap(Image.FromFile(@"..\..\FilteredIcon.png")); 
 
                //apply custom filter icon for Secod Column in SfDataGrid 
                //FilterIcon only changed for CustomerId Column in SfDataGrid 
                sfDataGrid1.Columns[1].HeaderStyle.FilterIcon = new Bitmap(Image.FromFile(@"..\..\FilterIcon.png")); 
 
                //incase you need to apply filter and filtered icon for same column please refer below 
                //Filter and Filtered both icon changed for CustomerName column in SfDataGrid 
                sfDataGrid1.Columns[2].HeaderStyle.FilterIcon = new Bitmap(Image.FromFile(@"..\..\FilterIcon.png")); 
                sfDataGrid1.Columns[2].HeaderStyle.FilteredIcon = new Bitmap(Image.FromFile(@"..\..\FilteredIcon.png")); 
                sfDataGrid1.TableControl.Invalidate(); 
            } 
            catch (Exception ex) 
            { 
                MessageBox.Show("Error: Could not read file from disk.  Original error: " + ex.Message); 
            }            
 
} 



Please let us know if you have any concerns in this. 

Regards, 
Vijayarasan S 


Marked as answer

MJ Mark Jayvee July 3, 2021 06:14 AM UTC

thank you, all works.



VS Vijayarasan Sivanandham Syncfusion Team July 5, 2021 05:07 AM UTC

Hi Mark Jayvee,

Thanks for the update.

We are glad to know that the reported problem has been resolved at your end. Please let us know if you have any further queries on this. We are happy to help you😊.

Regards,
Vijayarasan S

Loader.
Up arrow icon