views:

256

answers:

3

I have two results from a data set. I want to add both results in one gridview column. I do not want to merge in the code behind. Is there another way to do that?

<asp:TemplateField HeaderText="Entered Date" SortExpression="Date">
                    <ItemTemplate>
                    <div style="text-align: center;">
                        <asp:Label ID="Label3" runat="server" Text='<%# formatDisplayDate(Eval("Date").ToString()) %>'></asp:Label>
                    </div>
                    </ItemTemplate>                    
                </asp:TemplateField>

Now i would return "04/04/2009". I would like to return "04/04/2009-PASSED"

+4  A: 

Have you tried:

formatDisplayDate(Eval("Date").ToString()) & "-" & Eval("OtherField").ToString()

If that doesn't work you should be able to set up a TemplateField for the combined rows like this:

<asp:TemplateField HeaderText="CombinedFieldName" SortExpression="CombinedFieldName">
    <ItemTemplate>
       <asp:Label ID="Label1" runat="server" 
       Text='<%# DataBinder.Eval(Container,"DataItem.FirstField") %>' >
       </asp:Label>
       -
       <asp:Label ID="Label2" runat="server" 
       Text='<%# DataBinder.Eval(Container,"DataItem.SecondField") %>' >
       </asp:Label>
        </ItemTemplate>
</asp:TemplateField>

That said - it is usually much more efficient to do the field concatenation in the database operation if possible. I haven't tested, but I would assume that concatenating in the SELECT would be fastest, followed by concatenating in the data set in the codebehind, and combining them on the ASP.Net page would be slowest.

Gary.Ray
A: 

Another option would be to create a calculated column in the DataTable, using the DataColumn.Expression property

Thomas Levesque
A: 

My first question is why don't you want to merge in the code behind? The more content you bind to a control the more data that gets put into that controls view state.

@Gary.Ray has a really good solution, but something you might find better is to use linq to build a single data object from your existing data sets that way you only include what you need and the data is a lot easier to isolate, think single responsibility pattern here, if you can move the code that deals with getting your data to somewhere that only deals with getting data your application will be MUCH easy to maintain in the future.

Bob The Janitor