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.
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.
Re: Need logic on gridview column
Slight correction:
Code:
SELECT
CASE cog
WHEN NULL THEN 'INDEX'
ELSE dsc
END
.........
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? :confused:
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. :sick:
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 :sick: 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.