Answered by:
Gridview ItemTemplate with If condition of Database Data

Question
-
User836525179 posted
Dear Experts
In Gridview I would like to use show one of 3 result If condtion is true show text 1 else if show text 2 else show text 3
i know to use if else for display 2 condition result : <%#Eval("result").ToString().ToLower().Equals("0") ? "text 1" : "text 2" %>
but i need check result for three values : 0 and 1 and 2
and show message for each one but i dont know how to do
like below but i know it is wrong but i need correct model
<ItemTemplate>
<%# If Eval("result")=="0" { %>
text 1
<%# } else If Eval("result")=="1" Then { %>
text 2<%# }else{ %>
text 3
<%# } %>
</ItemTemplate>Monday, April 6, 2020 7:19 PM
Answers
-
User475983607 posted
It's a lot easier to solve this problem using SQL than within a GridView template using Eval(). SQL has a CASE statement which is designed to solve this exact problem.
SELECT CASE WHEN result = 0 THEN 'Text 0' WHEN result = 1 THEN 'Test 1' ELSE 'Test 2' END AS ColumnName FROM SomeTable
https://docs.microsoft.com/en-us/sql/t-sql/language-elements/case-transact-sql?view=sql-server-ver15
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Monday, April 6, 2020 9:06 PM -
User409696431 posted
Use your proper table name and field names, and replace the acceptStatus field with the case statement.
SELECT id, userid, accid, bardashtAmount, bankname, accnumber,shabanumber, recDate, CASE WHEN acceptStatus = 0 THEN 'Text for 0' WHEN acceptStatus = 1 THEN 'Text for 1' ELSE 'Text for 2' END AS [Accept Status] FROM YourTableName;
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Tuesday, April 7, 2020 4:36 AM
All replies
-
User475983607 posted
It's a lot easier to solve this problem using SQL than within a GridView template using Eval(). SQL has a CASE statement which is designed to solve this exact problem.
SELECT CASE WHEN result = 0 THEN 'Text 0' WHEN result = 1 THEN 'Test 1' ELSE 'Test 2' END AS ColumnName FROM SomeTable
https://docs.microsoft.com/en-us/sql/t-sql/language-elements/case-transact-sql?view=sql-server-ver15
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Monday, April 6, 2020 9:06 PM -
User836525179 posted
hi dear
no i can't do this since it is table on database and have more fields is each records and some records have text 1 and another text 2 and text 3 i can't do sql query like this
i need to check acceptStatus field in gridview and show appropriate Text in ItemTemplate
for example acceptStatus was number 0 it show number is zero or number was 1 it show number is one and number was 2 it show number is two in ItemTemplate
Monday, April 6, 2020 9:36 PM -
User475983607 posted
uniservice3
no i can't do this since it is table on database and have more fields is each records and some records have text 1 and another text 2 and text 3 i can't do sql query like thisI do not understand your response. A CASE statements affects a single column. This construct allows you to invoke a condition on a column and return a value for every record.
SQL is, by far, the most appropriate tool because a SQL query returns a data source that can be bound to the GridView while your approach returns a data source that you have to fix while binding to the GridView.
Can you explain why you are unable to modify the query?
Monday, April 6, 2020 9:54 PM -
User836525179 posted
ty
so can you write sql query for this table?
i need select all fields in each records with correct text for acceptStatus
Tuesday, April 7, 2020 2:40 AM -
User409696431 posted
Use your proper table name and field names, and replace the acceptStatus field with the case statement.
SELECT id, userid, accid, bardashtAmount, bankname, accnumber,shabanumber, recDate, CASE WHEN acceptStatus = 0 THEN 'Text for 0' WHEN acceptStatus = 1 THEN 'Text for 1' ELSE 'Text for 2' END AS [Accept Status] FROM YourTableName;
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Tuesday, April 7, 2020 4:36 AM