GridView - how do i modify bound data column by column?

Discussion in 'ASP .Net Web Controls' started by TS, Aug 10, 2007.

  1. TS

    TS Guest

    i have a custom object list for my data source. i want to be able to
    override some method so that whenever a column in row is bound, i can take
    that value that gets bound, which is an integer, and decode that into it's
    code table's textual value.

    so if i have a collection of Car objects who have a property called Color,
    which is an integer that points to a table that i can covert to its text
    form:

    ColorTable
    1 = 'blue'
    2 = 'red'

    Where can i go to do this? I figured if i override OnDataBound, i could loop
    thru all the rows and then thru all the columns and convert the values then,
    but that means i have to do a looping operation - i figured i should be able
    to do it as it gets bound???

    thanks
     
    TS, Aug 10, 2007
    #1
    1. Advertising

  2. TS

    Teemu Keiski Guest

    If you handle RowDataBound event, it is automatically called for every row
    in the data source, so you don't need to loop rows since it's already
    provided to you by handling the event. Only thing to do then is tyo deal
    with the columns in the data source. If they are fixed, you could do it
    fixed column by column and you don't have to loop.

    --
    Teemu Keiski
    AspInsider, ASP.NET MVP
    http://blogs.aspadvice.com/joteke
    http://teemukeiski.net


    "TS" <> wrote in message
    news:...
    >i have a custom object list for my data source. i want to be able to
    > override some method so that whenever a column in row is bound, i can take
    > that value that gets bound, which is an integer, and decode that into it's
    > code table's textual value.
    >
    > so if i have a collection of Car objects who have a property called Color,
    > which is an integer that points to a table that i can covert to its text
    > form:
    >
    > ColorTable
    > 1 = 'blue'
    > 2 = 'red'
    >
    > Where can i go to do this? I figured if i override OnDataBound, i could
    > loop
    > thru all the rows and then thru all the columns and convert the values
    > then,
    > but that means i have to do a looping operation - i figured i should be
    > able
    > to do it as it gets bound???
    >
    > thanks
    >
    >
     
    Teemu Keiski, Aug 11, 2007
    #2
    1. Advertising

  3. Hi TS,

    There're two approaches for your objective. One is mentioned by Teemu:
    change the data after each row is bound.

    Another approach is to directly convert the data to your desired one using
    a code-behind function and do this in the declarative markup with a
    TemplateField

    <asp:TemplateField>
    <ItemTemplate>
    <asp:Label ID="lblColor" runat="server"
    BackColor=<%#
    System.Drawing.Color.FromName(GetColor(DataBinder.Eval(Container.DataItem,
    "ColorIndex")))%> Text=<%# GetColor(DataBinder.Eval(Container.DataItem,
    "ColorIndex")) %>></asp:Label>
    </ItemTemplate>
    </asp:TemplateField>


    protected string GetColor(object colorIndex)
    {
    switch ((int) colorIndex) {
    case 1:
    return "blue";
    case 2:
    return "red";
    case 3:
    return "yellow";
    case 4:
    return "purple";
    }

    return "white";
    }



    Hope this helps.


    Regards,
    Walter Wang (, remove 'online.')
    Microsoft Online Community Support

    ==================================================
    When responding to posts, please "Reply to Group" via your newsreader so
    that others may learn and benefit from your issue.
    ==================================================

    This posting is provided "AS IS" with no warranties, and confers no rights.
     
    Walter Wang [MSFT], Aug 13, 2007
    #3
  4. Hi TS,

    I'm writing to check the status of this post. Please feel free to let me
    know if there's anything else I can help. Thanks.



    Regards,
    Walter Wang (, remove 'online.')
    Microsoft Online Community Support

    ==================================================
    When responding to posts, please "Reply to Group" via your newsreader so
    that others may learn and benefit from your issue.
    ==================================================

    This posting is provided "AS IS" with no warranties, and confers no rights.
     
    Walter Wang [MSFT], Aug 16, 2007
    #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. C Newby
    Replies:
    7
    Views:
    600
    C Newby
    Apr 2, 2004
  2. sunil panda

    Lower bound & Upper bound

    sunil panda, Dec 25, 2003, in forum: Java
    Replies:
    9
    Views:
    7,327
    thushara wijeratna
    Oct 7, 2008
  3. Rhiner Dan
    Replies:
    1
    Views:
    763
    Mike Wahler
    Mar 27, 2005
  4. Mario Krsnic
    Replies:
    0
    Views:
    392
    Mario Krsnic
    Jun 23, 2006
  5. Phillip Vong
    Replies:
    0
    Views:
    428
    Phillip Vong
    Jul 27, 2006
Loading...

Share This Page