Results 1 to 5 of 5

Thread: Need logic on gridview column

  1. #1

    Thread Starter
    PowerPoster MMock's Avatar
    Join Date
    Apr 2007
    Location
    My Mustang GT
    Posts
    4,563

    Need logic on gridview column

    Currently in my gridview I have this:
    Code:
    <asp:BoundField DataField="dsc" HeaderText="Component" />
    However, what the logic needs to be is if a field in my database table called "cog" is null I want to put the word "INDEX" in the gridview Component column and if "cog" is not null that is when I want to put the value of "dsc" from my database table in the gridview (what it is doing now).

    I'd rather not do this in the code-behind (but I could if that is the recommended way) so can anyone help me write the logic in the gridview? I know I need a TemplateField but I'm not sure how to write a conditional statement in a TemplateField. I assume call a piece of javascript somehow...

    Thanks.

  2. #2
    Frenzied Member
    Join Date
    Nov 2001
    Location
    Mass USA
    Posts
    1,674

    Re: Need logic on gridview column

    You can do this in your SQL statement.

    SELECT (CASE dsc IS NULL THEN 'INDEX' ELSE dsc END) FROM cog


    something similar to that.

  3. #3
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: Need logic on gridview column

    Slight correction:

    Code:
    SELECT
        CASE cog
            WHEN NULL THEN 'INDEX'
            ELSE dsc
        END
    .........

  4. #4

    Thread Starter
    PowerPoster MMock's Avatar
    Join Date
    Apr 2007
    Location
    My Mustang GT
    Posts
    4,563

    Re: Need logic on gridview column

    Are you guys serious?

    All along everyone's been telling me to keep the data layer separate from the business layer.

    Shouldn't the stored proc just hand me data and my code should process them to the requirements?

  5. #5
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: Need logic on gridview column

    99 ways to skin a cat.

    But you're right, this can be done in your layers. Slap on the wrist for us.

    You'll need to create a method in your codebehind, which takes two values - the cog and the dsc. This method should check cog for null and if it's null, return a string 'INDEX' else return the dsc value.

    You then call this codebehind method from your inline code in the gridview.

    It will look something like

    <%# MethodName(Container.DataItem("cog"), Container.DataItem("dsc")) %>

    Not sure about the exact syntax, but it's a matter of calling a method, passing the values and having the MethodName() return a string.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width