简体   繁体   中英

How do i put 2 DataSet Table Results in one GridView Column?

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"

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.

另一个选择是使用DataColumn.Expression属性在DataTable中创建一个计算列。

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM