ComponentOne List for WinForms
In This Topic
    Format
    In This Topic

    In many cases, the information that List control receives from its data source is raw data, which is not ideal for end-user display. For example, date fields may need to be converted to a specific international format; currency fields may contain too many insignificant digits after the decimal point. List allows you to alter the format of data fields in various ways as described in the following sections.

    Number Field Format

    List supports a variety of data formatting options through the NumberFormat property of the C1DataColumn class. The NumberFormat property reconfigures the data format of the list's data. It can alter most types of numeric values for a particular column. For example, to display all date values within a column, you can use the Medium Date setting (mm/dd/yyyy) provided by the NumberFormat property as demonstrated in the following code.

    C#
    Copy Code
    this.c1List1.Columns["HireDate"].NumberFormat = "Medium Date";
    

    Note that if you change the NumberFormat property of a column at run time, you do not need to refresh the display, as List handles this automatically.

    The following table provides a description of the formatting options for numeric data:

    Formatting Option Description
    Standard Display number with thousands separator, at least one digit to the left and two digits to the right of the decimal separator.
    General Number Display number as is, with no thousand separators.
    Currency Display number with thousand separator, if appropriate; display two digits to the right of the decimal separator. Note that output is based on system local settings.
    Percent Display number multiplied by 100 with a percent sign (%) appended to the right; always display two digits to the right of the decimal separator.
    Fixed Display at least one digit to the left and two digits to the right of the decimal separator.
    Scientific Use standard scientific notation.
    Yes/No Display No if number is 0; otherwise, display Yes.
    True/False Display False if number is 0; otherwise, display True.
    On/Off Display Off if number is 0; otherwise, display On.
    0% Display number multiplied by 100, then rounded to the nearest integer, with a percent sign (%) appended to the right.
    0.00% Same as Percent.

    For date and time data, the following predefined options are available:

    Formatting Option Description
    General Date Display a date and/or time. For real numbers, display a date and time (for example, 4/3/93 05:34 PM); if there is no fractional part, display only a date (for example, 4/3/93); if there is no integer part, display only a time (for example, 05:34 PM). Date display is determined by your system settings.
    Long Date Display a date using your system's long date format.
    Medium Date Display a date using the medium date format appropriate for the language version of Visual Studio.
    Short Date Display a date using your system's short date format.
    Long Time Display a time using your system's long time format: includes hours, minutes, seconds.
    Medium Time Display a time in 12-hour format using hours and minutes and the AM/PM designator.
    Short Time Display a time using the 24-hour format (for example, 17:45).

    Custom Format

    Sometimes, you may find that your current formatting options do not suit your particular needs and may need a custom formatting option. In such cases, you can use FormatText event of the C1List class to override the default formatting on a specific column. The FormatText event has fewer restrictions than other formatting techniques, and, hence, allows you to gain full control over the textual content of any value displayed in the list. You can reformat, translate, indent, or do anything you want to the data just prior to display.

    You can apply custom formatting on specific columns by subscribing to FormatText event. Choosing this option for a column causes the FormatText event to fire each time data is about to be displayed in that column.

    The following example demonstrates how to apply custom formatting in the List control. In this example, the numeric data (ranging from 1-30) stored in Column 1 is displayed as roman numerals.

    1. Subscribe to FormatText event of the C1List class and specify the FormatText event option for the column to which you want to apply custom formatting by setting the value of NumberFormat property to "FormatText Event".
      C#
      Copy Code
      //subscribe to the FormatText event
      c1List1.FormatText += C1List1_FormatText;
      //set the NumberFormat property of Column to "FormatText Event" to enable firing of FormatText event for that column
      c1List1.Columns["Column 1"].NumberFormat = "FormatText Event";
      
    2. Add the following code to the C1List1_FormatText event handler to apply custom formatting.
      C#
      Copy Code
      private void C1List1_FormatText(object sender, C1.Win.List.FormatTextEventArgs e)
      {
          
          string result= null;
          if (e.ColIndex == 0)
          {
              var val = int.Parse(e.Value);
              // Determine how many X's.
              while (val >= 10)
              {
                  result = result + "X";
                  e.Value = (val- 10).ToString();
              }
      
              // Append "digits" 1-9.
              switch (val)
              {
                  case 1:
                      result = result + "I";
                      break;
                  case 2:
                      result = result + "II";
                      break;
                  case 3:
                      result = result + "III";
                      break;
                  case 4:
                      result = result + "IV";
                      break;
                  case 5:
                      result = result + "V";
                      break;
                  case 6:
                      result = result + "VI";
                      break;
                  case 7:
                      result = result + "VII";
                      break;
                  case 8:
                      result = result + "VIII";
                      break;
                  case 9:
                      result = result + "IX";
                      break;
              }
      
              // Change the actual format.
              e.Value = result;
          }