Format DataGrid output
Translation: nxyc_twz@163.com
In this forum, I saw a lot of questions about how to format display data in DataGrid. People want to know how to change the color, text or basic display value of a row in special cases. In this article, I will use two different technologies to demonstrate how to modify the output format of your data. I will show you how to use Helper functions to change the data data display format. After that, I will explain how to use the DataGrid's onItemDatabase event to modify the data data of the DataGrid and format it. Plan first, I have designed the data file in this example, which is a simple XML file for the contact. The display requirements in my DataGIRD include:
Merged first name. If the value of the Manager is 1 or Employee's value is 0, change the manager display mode. If the value of any line in the Manager field is 1, the background color of the row is changed. XML Version = "1.0" Standalone = "YES"?>
[C #] ProtectedVoid DGContacts_itemdatabase (Object Source, System.Web.ui.WebControls.DataGriditeMeventargs e) {// OK is a data row instead of page or page IF (E.Item.itemType == ListItemType.Item || E. Item.itemType == ListItemType.alternatingItem) {// Value of the Manager field string ismanager = (string) DataBinder.eval (E.Item.DataItem, "Manager"); if (ismanager == "1") {////// Set text and background color. E.Item.cells [2] .text = "manager"; E.Item.backcolor = system.drawing.color = system.drawing.color.AriceBlue;} else {// only set text. E.Item.cells [ 2] .Text = "Employee";}}} [VB] PrivateSub dgContacts_ItemDataBound (ByVal sender AsObject, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles dgContacts.ItemDataBound 'data line instead of determining whether the header or Tail if E.Item.itemType = ListItemType.ItemoreLse E.Item.itemType = ListiteMTYPE.AlternatingItem Then 'DIM ISMANAGER Astring = CType (DatabaseM.DataItem (E.ITEM.DataItem) "Manager", string) if ismanager = "1" THEN 'Sets text and background color. E.Item.cells (2) .text = "manager" E.drawing.color = system.drawing.color.AriceBlue else 'Setting only text. E.Item.cells (2) .Text = "Employee" endif endif endsub results Finally, we summarize it, see what has been created! If I have completed each job correctly, I will get a simple grid that contains 3 columns. The name is combined by Last Name and First Name. Then the email address, then the status column indicates whether this person is an administrator or employee. Finally, any row showing the manager has a background color, let's take a look at the final result! Summary As you can see, there are a variety of different ways to change the data output format in your DataGrid. The way you choose depends on the operation you need to perform or how you considered.