Helper Function
page 1 of 1
Published: 20 Oct 2003
Unedited - Community Contributed
Abstract
DataGrid is a powerful and useful control in WebForm programming, while lots of questions about customizing the data returned and displayed in this control. A Helper Function work like an intermediate function which can do a lots of powerful manipulation of data before the data actually display on a DataGrid. In this article, I'll show a demo about the most commonly used technique in customizing the data and let it display properly on screen. ;)
by Colt Kwong
Feedback
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 25391/ 51

Helper Function
Helper Function, work like an intermediate function, which will accept values returned from database as parameter(s), and then do some user defined processing before returning and displaying the data on the screen.
Now, I would like to write a simple Web Form with DataGrid control by using Web Matrix and show the frequently asked questions in customizing the output of DataGrid control.

The Helper Functions extracted from the example include:

1. Display the ID / Index of each row / Find how many rows are returned in the DataGrid : This question... different approaches are available to give the same result, the most easy way would be select the PrimaryKey/Identity column of your table from database and then display this BoundColumn on the DataGrid. (But how about no such column in your Table?)
Or, you may create a DataColumn, set its Type as Integer and AutoIncrement = True..., and then add this column to your DataTable for databinding of the grid. (DataReader?) Well... all are Okay, but involved lots of codes/typing...
With the use of Helper Function, you can do so easily!
As you can just declare an integer object, increment it and do a little calculation depends on the which pages do user currently viewing. That's it!


Protected Function GetIndex() As String
       
ItemIndex += 1
Return (MyDataGrid.CurrentPageIndex * MyDataGrid.PageSize) + ItemIndex
End Function


2. Substring and display "..." if the text returned is too long
Yes, you have different approaches for this purpose. E.g. You can do it in your SQL data select statement or check the length of data in the ItemDataBound/ItemCreated event handlers.
In a Helper Function, the value returned from DataBase will be input as arguments in it, and then this string will be evaluated and substring accordingly if its length over a defined length of characters.


Protected Function CheckLength(ProductName As String) As String
       
If ProductName.ToString().Length > 10 Then
Return ProductName.SubString(0,10) & "..."
Else
Return ProductName.ToString()
End If
End Function


3. Display image in a DataGrid and dynamically determine to display an image.
A TemplateColumn with Image server control is used in a DataGrid, so that the ImageUrl of this image will be dynamically filled by the value returned from Database.


Protected Function WhichImage(QuantityPerUnit As Object) As String
       
If QuantityPerUnit Is DBNull.Value Then
Return "~/Images/Angry.gif"
Else
Return "~/Images/Smile.gif"
End If
End Function


4. Display a user defined text if the returned value is 0.
Normally, we want to display a more descriptive message if the returned value is 0 or Null... I.e. We don't want to see (Nullable in Database) blank data in a single column.


Protected Function CheckUnit(UnitsOnOrder As Object) As String
       
If Integer.Parse(UnitsOnOrder) = 0 Then
Return "Out Of Stock"
Else
Return UnitsOnOrder.ToString()
End If
End Function


5. Format and display currency with 2 decimal points in a BoundColumn.
Apart from formatting into a Currency type of a column, we can defined the no. of numbers after the decimal point, say 2 or 3... Well.. the point is:


DataFormatString="${0:N2}"


6. Change font color (Highlight) data based on a predefined rule/negative in value.
If some data returned from Database is... negative! We may probably want to highlight it and display it in other warning forecolor.


Protected Function CheckPrice(UnitPrice As Object) As String
       
If Double.Parse(UnitPrice) <= 50.00 Then
Return "<Font Color=Red>" & UnitPrice.ToString() & "</Font>"
Else
Return "<Font Color=Blue>" & UnitPrice.ToString() & "</Font>"
End If
End Function


7. Display CheckBox in a DataGrid and dynamically (un)check it based on a Bit value returned from Database.
A TemplateColumn with CheckBox server control was used in a DataGrid and it will be Checked or UnChecked before display to users according to the returned value.


<asp:CheckBox id="chk_Discontinued" RunAt="Server"
Checked=<%# Container.DataItem("Discontinued") %>/>


8. Concatenate 2 fields (FirstName and LastName) and display in a single column.
A common way to do so would be: "modify your SQL select statement for DataBinding first..." E.g. "select FirstName + ' ' + LastName As FullName from Employees" So, we can bind the DataField to this "FullName" column.
Moreover, Helper Function can do exactly the same thing but you don't have to modify your SQL statement! You can just pass in the 2 data fields to a Helper Function, and let it process for you and wait for the result of concatenated string whcih will be output on screen.


Protected Function Change2FullName(FirstName As Object, LastName As Object) As String
       
Return FirstName.ToString() + " " + LastName.ToString()
End Function


OK... Let's see the source codes and Demo now!



User Comments

Title: A good Artical   
Name: paresh patel
Date: 2007-07-19 2:35:11 PM
Comment:
that was good logic and simple code
Title: A Very Good Article   
Name: Nikitta
Date: 2007-05-18 2:59:21 AM
Comment:
That was a very good piece of informations..
Title: Good   
Name: ismail tutumluer
Date: 2007-01-05 8:43:04 AM
Comment:
Congr.Good Source

Product Spotlight
Product Spotlight 





Community Advice: ASP | SQL | XML | Regular Expressions | Windows


©Copyright 1998-2017 ASPAlliance.com  |  Page Processed at 2017-11-21 9:00:54 AM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search