Mostrar varios campos de datos en BoundField de Gridview

Tengo una asp:GridView which is bound. Within this I have multiple columns, I'm trying to get the data from two database fields concatenated into one field.

¿Como hacer esto?

¿Algo como esto?

asp:BoundField DataField="field1 + ' ' + field2" HeaderText="Status" SortExpression="info"

preguntado el 01 de febrero de 12 a las 14:02

Proper capitalization and spelling is always a plus =) -

4 Respuestas

Pretty sure you need to use a TemplateField instead of BoundField for this.

In your GridView Columns Block:

    <asp:TemplateField HeaderText="Name">
            <%# Eval("FirstName") + " " + Eval("LastName")%>

respondido 15 mar '17, 16:03

Just for completeness, cause I searched a solution and came first here...

You have more flexibility by using string.Format()

<asp:TemplateField HeaderText="Status">
     <%# string.Format("{0} {1}", Eval("field1") ,Eval("field2"))%>

Here you can also use the power of string.Format() to format date and number types as descriped here:


<%# String.Format("{0:MM/dd/yyyy} - {1:N2}", Eval("dateValue1"), Eval("decimalValue2")) %>

Another option is to do it in a custom method with code behind


<asp:TemplateField HeaderText="Status">
      <asp:Label runat="server" Text='<%#GetStatus(Eval("Status1"),Eval("Status2")) %>'>

Código detrás:

public string GetStatus(object Status1, object Status2)
    return (string)Status1 + " " + (string)Status2;

Respondido 04 Feb 19, 17:02

ToolTip='<%# Eval("LastName") & "-" & Eval("FirstName") %>'

Respondido 30 ago 13, 02:08

Try like this. If u are using two datasets make to one datatable and bind it to gridview.

<asp:BoundField DataField="<%# DataBinder.Eval(Container.DataItem, "f1")%>+ ' ' + <%# DataBinder.Eval(Container.DataItem, "f2")%>" HeaderText="Status" SortExpression="info"/>

Respondido 01 Feb 12, 18:02

Don't you need to use template files for that? Not bound fields? - Arion

@pseudocoder: it does work, Container.DataItem is the current item, f1, f2 are the proprierties to be read from the current item. - user365268

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.