Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET GridView bind two field into one column

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"):"" %>
like image 834
Tomas Avatar asked Oct 10 '22 08:10

Tomas


1 Answers

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.

like image 191
Doozer Blake Avatar answered Oct 13 '22 11:10

Doozer Blake