格式化UltragridRow细胞 [英] Format UltragridRow Cells

查看:143
本文介绍了格式化UltragridRow细胞的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要格式化UltraGrid的单元格.

I need to format the cell of an UltraGrid.

赞:使单元格格式化DateTime.

我已经完成了专栏工作

   UltraGridColumn.Format = "d"

是否可以格式化单个单元格?

likewise is there any option to format individual cells?

UltraGridRow.Cells("abc")= ?

注意:使用Infragistics版本12.1

Note: Using the Infragistics version 12.1

推荐答案

强制UltraGrid对同一列中的单元格使用不同的编辑器的技巧是基于控件基础结构提供的一组编程对象和模式.

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.

首先要考虑的是InitializeRow事件.当您设置网格的初始DataSource或更改基础DataSource中的某些内容时,将针对每一行调用此事件.您可以通过e.Reinitialize标志来区分这两种情况.如果为false,则将整个数据源应用于网格,如果为true,则通常仅初始化一部分行,因为用户已编辑单元格或代码已更改数据源中的值.

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.

要考虑的第二件事是每个UltraGridCell或UltraGridColumn中存在的Editor属性.它是UltraGrid自动构建的对象,允许编辑单元格. UltraGrid代码根据列的数据类型设置此对象,并且显然为列的每个单元格设置相同的编辑器.您可以在列级别(通常在InitializeLayout事件中)或在逐个单元的基础上设置格式规则来设置自己的编辑器.

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.

让我们尝试一个示例(其中大部分来自Alhalama在其注释中建议的示例代码)

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

假设您有一个只有两列的数据表: 第一列称为CONDITION,其中包含一个字符串.此字符串是我的格式要求.
第二列称为DATEINFO,其中包含的日期格式应与CONDITION列中的值相对应.
因此,如果CONDITION = 'TEST1',则将DATEINFO单元格设置为日/月/年格式,如果CONDITION='TEST2',则将DATEINFO单元格设置为时/分/秒.

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;
       }
   }
}

这篇关于格式化UltragridRow细胞的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆