Syncfusion.XlsIO.Base
Example 


Key Property (ISortField)

Represents the column to be sorted on.
Syntax
'Declaration
 
Property Key As Integer
'Usage
 
Dim instance As ISortField
Dim value As Integer
 
instance.Key = value
 
value = instance.Key
int Key {get; set;}
Example
The following code illustrates how the the column to be sorted can be set using the Key property. Here for example, we set 2 to the Key property.
using Syncfusion.XlsIO;
using System.Drawing;
            
class Program
{
    static void Main()
    {
        using (ExcelEngine excelEngine = new ExcelEngine())
        {
            //Create worksheet
            IApplication application = excelEngine.Excel;
            application.DefaultVersion = ExcelVersion.Excel2013;
            IWorkbook workbook = application.Workbooks.Create(1);
            IWorksheet worksheet = workbook.Worksheets[0];
            
            //Load data
            worksheet["A1"].Text = "Products";
            worksheet["A2"].Text = "Alfreds Futterkiste";
            worksheet["A3"].Text = "Antonio Moreno Taqueria";
            worksheet["A4"].Text = "Around the Horn";
            worksheet["A5"].Text = "Bon app";
            worksheet["A6"].Text = "Eastern Connection";
            worksheet["A7"].Text = "Ernst Handel";
            worksheet["B1"].Text = "Qtr1";
            worksheet["B2"].Number = 744.6;
            worksheet["B3"].Number = 5079.6;
            worksheet["B4"].Number = 1267.5;
            worksheet["B5"].Number = 1418;
            worksheet["B6"].Number = 4728;
            worksheet["B7"].Number = 943.89;
            worksheet["C1"].Text = "Qtr2";
            worksheet["C2"].Number = 162.56;
            worksheet["C3"].Number = 1249.2;
            worksheet["C4"].Number = 1062.5;
            worksheet["C5"].Number = 756;
            worksheet["C6"].Number = 4547.92;
            worksheet["C7"].Number = 349.6;
            
            //Create style for table number format
            IStyle style1 = workbook.Styles.Add("CurrencyFormat");
            style1.NumberFormat = "_($* #,##0.00_);_($* (#,##0.00);_($* \" - \"??_);_(@_)";
            
            //Apply number format
            worksheet["B2:C8"].CellStyleName = "CurrencyFormat";
            
            //Set colors
            worksheet["C2:C3"].CellStyle.Color = Color.Red;
            worksheet["C4:C5"].CellStyle.Color = Color.Blue;
            worksheet["C6:C7"].CellStyle.Color = Color.Green;
            
            //Create sorter
            IDataSort sorter = workbook.CreateDataSorter();
            
            //Set sort range
            sorter.SortRange = worksheet["A1:C7"];
            
            //Add sortfield
            ISortField sortField = sorter.SortFields.Add(0, SortOn.Values, OrderBy.Ascending);
            
            //Set sortfield properties
            sortField.SortOn = SortOn.CellColor;
            sortField.Order = OrderBy.Descending;
            sortField.Key = 2;
            sortField.Color = Color.Green;
            
            //Perform sort
            sorter.Sort();
            
            //Apply AutoFit
            worksheet.UsedRange.AutofitColumns();
            
            //Save and Dispose
            workbook.SaveAs("Datasort.xlsx");
            workbook.Close();
        }
    }
}
AssemblyVersion

Syncfusion.XlsIO.Base: 17.2460.0.46

See Also