How to present data on datagrid in another pattern

A

ad

I have a employee table, with a filed sexID
The field store 1 for male and 2 for female.
I want to display this field in a DataGrid with male or female , not 1 or 2.
How can I do that?
 
M

Mike Douglas

Hi,

You need to capture the ItemDataBound event which is fired for each
row when it is being bound and change the value. Here's the code.
This only works if it is a DataBound Column. If its a Template Column
then you will need to pull the label control out of cell, cast it, and
then set the value.

Private Sub DataGrid1_ItemDataBound(ByVal sender As Object, ByVal
e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles
DataGrid1.ItemDataBound
Dim sexIDColumn As Integer = 5 ' whatever the column number is
If e.Item.ItemType = ListItemType.EditItem Or e.Item.ItemType
= ListItemType.AlternatingItem Then
' Get sexID
If e.Item.Cells(sexIDColumn).Text = "1" Then
e.Item.Cells(sexIDColumn).Text = "male"
Else
e.Item.Cells(sexIDColumn).Text = "female"
End If
End If
End Sub

I hope this helps.

Mike Douglas
 
A

ad

Thank!
Can we use Template to do that?


Mike Douglas said:
Hi,

You need to capture the ItemDataBound event which is fired for each
row when it is being bound and change the value. Here's the code.
This only works if it is a DataBound Column. If its a Template Column
then you will need to pull the label control out of cell, cast it, and
then set the value.

Private Sub DataGrid1_ItemDataBound(ByVal sender As Object, ByVal
e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles
DataGrid1.ItemDataBound
Dim sexIDColumn As Integer = 5 ' whatever the column number is
If e.Item.ItemType = ListItemType.EditItem Or e.Item.ItemType
= ListItemType.AlternatingItem Then
' Get sexID
If e.Item.Cells(sexIDColumn).Text = "1" Then
e.Item.Cells(sexIDColumn).Text = "male"
Else
e.Item.Cells(sexIDColumn).Text = "female"
End If
End If
End Sub

I hope this helps.

Mike Douglas
 
M

Mike Douglas

Do you mean if you are using a Template Column and want to do it in
code behind or do you want to do it in the markup page?

I think this code should work for the code behind. If you want to do
it in the markup page create a vbfunction using the <script>that takes
in the <%# Databinder... stuff %> and returns the "male" or "female"
based on the 1 or 2.

Dim sexIDColumn As Integer = 5 ' whatever the column number is
If e.Item.ItemType = ListItemType.EditItem Or e.Item.ItemType
= ListItemType.AlternatingItem Then
' Get sexID
Dim sexID As Label
' control(0) is a blank literal control
sexID = CType(e.Item.Cells(sexIDColumn).Controls(1),
Label)
If sexID.Text = "1" Then
sexID.Text = "male"
Else
sexID.Text = "female"
End If
End If
End Sub

I hope that takes care of it. Let me know if it doesn't.

Mike Douglas
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top