Database binding

  • Thread starter Thread starter Maziar Aflatoun
  • Start date Start date
M

Maziar Aflatoun

Hi,

Is there a way to modify the data in a datagrid that is read from a database
before displaying it? For example, I have a field 'RecType' with can be
either 0 or 1 where 0 is for normal user and 1 for administrator. However,
when I want to display this in my datagrid I like to display 'Normal User'
instead of the number 0 and display 'Admin' instead of the number 1.



Thank you
Maz.
 
Maziar said:
Hi,

Is there a way to modify the data in a datagrid that is read from a
database before displaying it? For example, I have a field 'RecType'
with can be either 0 or 1 where 0 is for normal user and 1 for
administrator. However, when I want to display this in my datagrid I
like to display 'Normal User' instead of the number 0 and display
'Admin' instead of the number 1.

Apart from Steve's solution, and when using templates, you can
write a function (this is for VB):

Function FormatRecType(RecType As Object) As String
If(RecType.ToInt32()=0) Then
Return "Normal User"
Else
Return "Admin"
End If
End Function

Then, use this databinding syntax in your ItemTemplate:
<%# FormatRecType(DataBinder.Eval(Container.DataItem,"RecType")) %>
 
You can also change the query itself so that it changes your numbers to
text. Example...
SELECT Name, City,
'Gender' = CASE
WHEN Gender = 'F' THEN 'Female'
ELSE 'Male'
END,
Birthdate, Salary
FROM Employees
 
Back
Top