Question

I need to format the cell of an UltraGrid.

Like: Making the cell to format a DateTime.

I have done for a column,

   UltraGridColumn.Format = "d"

likewise is there any option to format individual cells?

UltraGridRow.Cells("abc")= ?

Note: Using the Infragistics version 12.1

Était-ce utile?

La solution

The trick to force the UltraGrid to use different editors for cells in the same column is based on a set of programming objects and patterns offered by the infrastructure of the control.

The first thing is to consider is the InitializeRow event. This event is called for each row when you set the initial DataSource of the grid or when you change something in the underlying DataSource. You can differentiate between the two situations thanks to the e.Reinitialize flag. If it is false, then the whole datasource is applied to the grid, if it is true then only a subset of rows are reinitialized usually because the user has edited the cell or the code has changed a value in the datasource.

The second thing to consider is the Editor property present in every UltraGridCell or UltraGridColumn. It is an object that is built automatically by the UltraGrid to allow the cell editing. The UltraGrid code set this object based on the datatype of the column and, obviously, sets the same editor for every cell of the column. You could set your own editor at the column level (usually in the InitializeLayout event) or on cell by cell basis looking at your formatting rule.

Let's try an example (Big parts of this is taken from the example code suggested in its comments by Alhalama)

Suppose you have a DataTable with only two columns: First column is called CONDITION and contains a string. This string is my formatting requirement.
The second column is called DATEINFO and contains the dates that should be formatted differently accordingly to the value in the column CONDITION.
So if CONDITION = 'TEST1' then the DATEINFO cell is formatted with Day/Month/Year pattern, if the CONDITION='TEST2' then the DATEINFO cell should be formatted with Hour/Minute/Seconds.

private void ultraGrid1_InitializeRow(object sender, InitializeRowEventArgs e)
{
   if (e.ReInitialize == false)
   {
       DefaultEditorOwnerSettings editorSettings;
       DateTimeEditor datetime_editor;
       string condition = e.Row.GetCellValue("Condition").ToString();
       switch (condition)
       {
            case "TEST1":
                editorSettings = new DefaultEditorOwnerSettings()
                editorSettings.DataType = typeof(DateTime);
                editorSettings.MaskInput = "mm/dd/yyyy";
                datetime_editor = new DateTimeEditor(new DefaultEditorOwner(editorSettings));
                e.Row.Cells["DateInfo"].Editor = datetime_editor;
                break;
            case "TEST2":
                editorSettings = new DefaultEditorOwnerSettings()
                editorSettings.DataType = typeof(DateTime);
                editorSettings.MaskInput = "hh:mm:ss";
                datetime_editor = new DateTimeEditor(new DefaultEditorOwner(editorSettings));
                e.Row.Cells["DateInfo"].Editor = datetime_editor;
                break;
       }
   }
}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top