ASP.NET GridView joins two fields into one column

I have an ASP.NET GridView in my web application and you want to link two fields into one column and use row formatting. I provide an example below, is it possible to implement it in a GridView?

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

Like this

 Name=DefaultParam 

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

I used Repeater and the code below for this, but now I decided to transfer the data display to the GridView

 <%#Eval("Name")%> <%# (!string.IsNullOrEmpty(Eval("DefaultParam").ToString())) ? "= " + Eval("DefaultParam"):"" %> 
+4
source share
2 answers

You can use TemplateField and place your logic there:

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

Another option is to use a property on your object to make this logic for you behind the scenes, and just use it like a BoundField, but you haven't specified which object is your binding.

+3
source

You can simply write server-side code between <%# ... %> , as you write in the 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 to learn how to customize formatting based on data using template fields.

0
source

Source: https://habr.com/ru/post/1383876/


All Articles