How to present data on datagrid in another pattern

Discussion in 'ASP .Net' started by ad, Mar 27, 2005.

  1. ad

    ad Guest

    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?
    ad, Mar 27, 2005
    #1
    1. Advertising

  2. ad

    Mike Douglas Guest

    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

    On Sun, 27 Mar 2005 09:01:06 +0800, "ad" <> wrote:

    >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?
    >
    Mike Douglas, Mar 27, 2005
    #2
    1. Advertising

  3. ad

    ad Guest

    Thank!
    Can we use Template to do that?


    "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
    >
    > On Sun, 27 Mar 2005 09:01:06 +0800, "ad" <> wrote:
    >
    > >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?
    > >

    >
    ad, Mar 27, 2005
    #3
  4. ad

    Mike Douglas Guest

    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

    On Sun, 27 Mar 2005 09:56:49 +0800, "ad" <> wrote:

    >Thank!
    >Can we use Template to do that?
    >
    >
    >"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
    >>
    >> On Sun, 27 Mar 2005 09:01:06 +0800, "ad" <> wrote:
    >>
    >> >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?
    >> >

    >>

    >
    Mike Douglas, Mar 27, 2005
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. ad
    Replies:
    2
    Views:
    480
  2. Gabriel_k
    Replies:
    0
    Views:
    403
    Gabriel_k
    Aug 12, 2003
  3. Ralf Wahner
    Replies:
    5
    Views:
    629
    Bob Foster
    Dec 24, 2003
  4. Richard Lionheart
    Replies:
    12
    Views:
    230
    Richard Lionheart
    Jul 10, 2004
  5. Charanya Nagarajan
    Replies:
    6
    Views:
    130
    7stud --
    Apr 30, 2009
Loading...

Share This Page