In case you need to update values which were returned from database table or set some specific formatting before sending them to client side, you can use the ItemPrerender event.
Common use-case will be similar to next
dhtmlxGridConnector connector = new dhtmlxGridConnector(/*...*/); connector.ItemPrerender += new EventHandler<ItemPrerenderEventArgs<dhtmlxGridDataItem>>(connector_ItemPrerender); void connector_ItemPrerender(object sender, ItemPrerenderEventArgs<dhtmlxGridDataItem> e) { if (e.DataItem.Index % 2 == 0) e.DataItem.BgColor = "gray"; }
Here, we have event handler attached to ItemPrerender event that sets each second row's background color. Each connector manipulates its own data item objects inherited from dhtmlxDataItem class. E.g. dhtmlxGridConnector uses dhtmlxGridDataItem, dhtmlxTreeConnector uses dhtmlxTreeDataItem etc. Each data item class has its own specific properties you can change. E.g. FolderClosedImage, FolderOpenedImage for dhtmlxTreeDataItem, Selected for dhtmlxComboItem or ChildRows for dhtmlxTreeGridDataItem.
The same way you change appearance of the items you can change data it will display. The following example will add row indexes to countries grid:
public override IdhtmlxConnector CreateConnector(HttpContext context) { dhtmlxGridConnector connector = new dhtmlxGridConnector( "Country", "ISO, PrintableName", "UID", dhtmlxDatabaseAdapterType.SqlServer2005, ConfigurationManager.ConnectionStrings["SamplesDatabase"].ConnectionString ); connector.ItemPrerender += new EventHandler<ItemPrerenderEventArgs<dhtmlxGridDataItem>>(connector_ItemPrerender); return connector; } void connector_ItemPrerender(object sender, ItemPrerenderEventArgs<dhtmlxGridDataItem> e) { e.DataItem.DataFields["PrintableName"] = (e.DataItem.Index + 1).ToString() + ". " + e.DataItem.DataFields["PrintableName"]; }
DataFields dictionary contains collection of selected fields for current data item. Index property gets position of the item in all items collection.
More complex formating rules can be defined by using “extra” fields in configuration, the fields that will not be outputed to client but can be used inside events.
public override IdhtmlxConnector CreateConnector(HttpContext context) { dhtmlxTreeGridConnector connector = new dhtmlxTreeGridConnector( "Folders", "item_nm,item_order,item_desc", "item_id", "item_parent_id", dhtmlxDatabaseAdapterType.SqlServer2005, ConfigurationManager.ConnectionStrings["SamplesDatabase"].ConnectionString "is_hidden"//extra field ); connector.RootItemRelationIDValue = "0";//Set ParentID value for root items connector.ItemPrerender += new EventHandler<ItemPrerenderEventArgs<dhtmlxTreeGridDataItem>>(connector_ItemPrerender); return connector; } void connector_ItemPrerender(object sender, ItemPrerenderEventArgs<dhtmlxTreeGridDataItem> e) { if (e.DataItem.DataFields["is_hidden"] == "1") e.DataItem.Skip = true;//do not render folders that have is_hidden attribute }
In addition to all other fields we request “is_hidden” field as extra one. This one will not be rendered, but we can access it via DataFields collection. Skip property is available in all dhtmlxDataItem class descendants and being set to true indicates that item will not be rendered.
dhtmlxComboDataItem has only 3 properties available for changing: Value, Text and Selected
Both grid and treeGrid has CssClass, BgColor, Style properties available for customization in Prerender event. CssClass allows to assign row's CSS class name, whereas Style property allows separate style properties assignment. BgColor allows you to set row's background color. Additionally, dthmlxTreeGridDataItem has Image, HasChildren and ChildRows. Last two of properties allow child rows manipulation. First one allows to assign custom image to the first column.
void connector_ItemPrerender(object sender, ItemPrerenderEventArgs<dhtmlxTreeGridDataItem> e) { if (e.DataItem.HasChildren)//tree grid row has child rows - output it like a folder e.DataItem.Image = "folder.gif"; }
As well as dhtmlxTreeGridDataItem, dhtmlxTreeDataItem has HasChildren and ChildNodes properties. What's more, tree allows to assign FolderOpenedImage, FolderClosedImage and LeafImage images, Checked, Text and Value properties.
In the example below we analyze data passed into data item and assign “lock.gif” image to item if is has “is_hidden” value.
void connector_ItemPrerender(object sender, ItemPrerenderEventArgs<dhtmlxTreeDataItem> e) { if (e.DataItem.DataFields["is_hidden"] == "1") e.DataItem.LeafImage = "lock.gif"; }
ItemPrerender event also might be used to check if Tree or TreeGrid item has child items. Assigning to HasChildren property true or false values changes the way item will be rendered.