简体   繁体   English

如何将2个DataSet表结果放在一个GridView列中?

[英]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. 我想将两个结果都添加到一个gridview列中。 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". 现在,我将返回“ 04/04/2009”。 I would like to return "04/04/2009-PASSED" 我想返回“ 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: 如果这样不起作用,您应该能够为合并的行设置一个TemplateField,如下所示:

<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. 我还没有测试过,但是我认为在SELECT中进行串联将是最快的,其次是在后面的代码中进行数据集的串联,然后在ASP.Net页面上进行组合将是最慢的。

另一个选择是使用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. @ Gary.Ray有一个非常好的解决方案,但是您可能会发现更好的方法是使用linq从现有数据集中构建单个数据对象,这样您就可以只包含所需内容,而数据则更容易隔离,请考虑一下。单一责任模式,如果您可以将处理数据的代码移到仅处理数据的地方,那么将来您的应用程序将很容易维护。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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