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.
OK... Let's see the complete codes and Demo now!