Skip to main content
 首页 » 编程设计

c#之在 GridView 的绑定(bind)字段中显示多个数据字段

2024年02月20日19zfyouxi

我有以下 sql 查询,我想在 gridview 中显示,基本上是连接的字段。

我该怎么做?

SELECT  
--RTRIM(C.CustomerFirstName) + ' ' + LTRIM(C.CustomerLastName) as CustomerFullName, 
C.CustomerCompany, 
C.CustomerPosition,  
C.CustomerCountry,  
C.CustomerProvince,  
C.CustomerContact, 
CP.ActionDate,  
CP.ProductCode, 
CP.CustomerEmail 
     FROM tblCustomers C 
    INNER JOIN  
    tblCustomerProducts CP ON  
C.CustomerEmail = CP.CustomerEmail 
     ORDER BY ActionDate DESC 

这是 GridView 的 html。基本上我想在一个字段中使用 CustomerFirstName 和 CustomerLastName

<asp:GridView ID="GridView1" runat="server" CellPadding="4" ForeColor="#333333"  
        GridLines="None" Width="231px" AutoGenerateColumns="False"> 
        <RowStyle BackColor="#EFF3FB" /> 
        <Columns> 
            **<asp:BoundField DataField="CustomerFirstName" HeaderText="Name" /> 
            <asp:BoundField DataField="CustomerLastName" HeaderText="Last Name" />** 
            <asp:BoundField DataField="CustomerCompany" HeaderText="Company/Organisation" /> 
            <asp:BoundField DataField="CustomerPosition" HeaderText="Position" /> 
            <asp:BoundField DataField="CustomerCountry" HeaderText="Country" /> 
            <asp:BoundField DataField="CustomerProvince" HeaderText="Province" /> 
            <asp:BoundField DataField="CustomerContact" HeaderText="Contact" /> 
            <asp:BoundField DataField="CustomerEmail" HeaderText="Email Address" /> 
            <asp:BoundField DataField="ActionDate" HeaderText="Action Date" /> 
            <asp:BoundField DataField="ProductCode" HeaderText="Product code" /> 
        </Columns> 
        <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" /> 
        <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" /> 
        <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" /> 
        <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" /> 
        <EditRowStyle BackColor="#2461BF" /> 
        <AlternatingRowStyle BackColor="White" /> 
    </asp:GridView> 

请您参考如下方法:

你想要什么is to use an <asp:templatefield>而不是 <asp:boundfield>

上面链接中的第 2 步显示了使用模板字段的一种方法。这是另一个:

<columns> 
    <asp:TemplateField  HeaderText="Name"> 
    <ItemTemplate> 
        <div><%#Eval("CustomerFirstName")%>, <%#Eval("CustomerLastName")%></div> 
    </ItemTemplate> 
<asp:BoundField> 
.... 
</columns> 

我添加了 <div>和 ,展示您可以在 <ItemTemplate> 中使用基本文本和 html .