5

I have ASP.NET GridView in my web app and would like to bind two fields into one column and use string formatting. I am providing example below, is it possible to implement into GridView?

I have two data fields Name and DefaultParam and would like to display these fields into one GridView column.

Like this

Name=DefaultParam

If DefaultParam value is empty I would like to show only Name value and do not include =

I was using Repeater and code below to achieve this but now decided to move data display to GridView

<%#Eval("Name")%>
<%# (!string.IsNullOrEmpty(Eval("DefaultParam").ToString())) ? "= " + Eval("DefaultParam"):"" %>

2 Answers 2

4

You can use a TemplateField and put your logic in there:

<asp:TemplateField HeaderText="Header">
<ItemTemplate>
<%#Eval("Name") + (!string.IsNullOrEmpty(Eval("DefaultParam").ToString())) ? "= " + Eval("DefaultParam"):""%>
</ItemTemplate>
</asp:TemplateField>

Another option would be to use a Property on your object to do that logic for you behind the scenes and just use that as a BoundField, but you didn't mention what the object is your binding is.

Sign up to request clarification or add additional context in comments.

Comments

0

You can simple write server-side code between <%# ... %> as you write in code behind. just put it in '' (between single quotes) .

<asp:Lable id="lblxx" runat="server" 
Text='<%# Eval("Name") + (!string.IsNullOrEmpty(Convert.ToString(Eval("DefaultParam")))) ? "= " + Eval("DefaultParam"):"" %>' />

Follow this tutorial link to know that how to Custom Formatting Based Upon Data using Template Fields.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.