如何在gridview中的单行中打印名字和姓氏?

时间:2014-05-24 20:52:44

标签: c# asp.net gridview

我有一个项目,其中First Name和Last Name存储在不同的变量中,但是在查询和检索值时,它们会单独显示。

如何加入他们?

非常感谢任何人和所有人的帮助。这是我坚持的最后一点,因此需要纠正它。

我是否在代码末尾正确使用按钮实现了整页打印?

以下是代码:

<%@ Page Title="" Language="C#" MasterPageFile="~/Main.master" AutoEventWireup="true" CodeFile="Confirmation.aspx.cs" Inherits="Confirmation" %>

<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
     <h3>Passenger Info</h3>
    <p>
        <asp:DetailsView ID="DetailsView1" runat="server" AutoGenerateRows="False" CellPadding="4" DataKeyNames="Passenger_Id" DataSourceID="ObjectDataSource1" EnableModelValidation="True" ForeColor="#333333" GridLines="None" Height="50px" Width="125px">
            <AlternatingRowStyle BackColor="White" />
            <CommandRowStyle BackColor="#D1DDF1" Font-Bold="True" />
            <EditRowStyle BackColor="#2461BF" />
            <FieldHeaderStyle BackColor="#DEE8F5" Font-Bold="True" />
            <Fields>
                <asp:BoundField DataField="PNR_Id" HeaderText="PNR" InsertVisible="False" ReadOnly="True" SortExpression="PNR_Id" />
                <asp:BoundField DataField="Title" HeaderText="Title" SortExpression="Title" />
                <asp:BoundField DataField="FirstName" HeaderText="FirstName" SortExpression="FirstName" />
                <asp:BoundField DataField="LastName" HeaderText="LastName" SortExpression="LastName" />
                <asp:BoundField DataField="Nationality" HeaderText="Nationality" SortExpression="Nationality" />
                <asp:BoundField DataField="Phone" HeaderText="Phone" SortExpression="Phone" />
                <asp:BoundField DataField="Email" HeaderText="Email" SortExpression="Email" />
                <asp:BoundField DataField="TotalFare" HeaderText="TotalFare" SortExpression="TotalFare" />
            </Fields>
            <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
            <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
            <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
            <RowStyle BackColor="#EFF3FB" />
        </asp:DetailsView>
        <asp:ObjectDataSource ID="ObjectDataSource1" runat="server" DeleteMethod="Delete" InsertMethod="Insert" OldValuesParameterFormatString="original_{0}" SelectMethod="GetDataByPNR" TypeName="FlightReservationTableAdapters.PassengerTableAdapter" UpdateMethod="Update">
            <DeleteParameters>
                <asp:Parameter Name="Original_Passenger_Id" Type="Int32" />
            </DeleteParameters>
            <InsertParameters>
                <asp:Parameter Name="Title" Type="String" />
                <asp:Parameter Name="FirstName" Type="String" />
                <asp:Parameter Name="LastName" Type="String" />
                <asp:Parameter Name="Nationality" Type="String" />
                <asp:Parameter Name="Phone" Type="String" />
                <asp:Parameter Name="Email" Type="String" />
            </InsertParameters>
            <SelectParameters>
                <asp:QueryStringParameter Name="PNRId" QueryStringField="PNRId" Type="Int32" />
            </SelectParameters>
            <UpdateParameters>
                <asp:Parameter Name="Title" Type="String" />
                <asp:Parameter Name="FirstName" Type="String" />
                <asp:Parameter Name="LastName" Type="String" />
                <asp:Parameter Name="Nationality" Type="String" />
                <asp:Parameter Name="Phone" Type="String" />
                <asp:Parameter Name="Email" Type="String" />
                <asp:Parameter Name="Original_Passenger_Id" Type="Int32" />
            </UpdateParameters>
        </asp:ObjectDataSource>
    </p>
    <h3>Flight Info</h3>
    <p>
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="4" DataKeyNames="Flight_Id" DataSourceID="ObjectDataSource2" EnableModelValidation="True" ForeColor="#333333" GridLines="None">
            <AlternatingRowStyle BackColor="White" />
            <Columns>
                <%--<asp:BoundField DataField="Flight_Id" HeaderText="Flight_Id" InsertVisible="False" ReadOnly="True" SortExpression="Flight_Id" />--%>
                <asp:BoundField DataField="Number" HeaderText="Number" SortExpression="Number" />
                <asp:BoundField DataField="Origin" HeaderText="Origin" SortExpression="Origin" />
                <asp:BoundField DataField="Destinetion" HeaderText="Destinetion" SortExpression="Destinetion" />
                <asp:BoundField DataField="DepartureDate" HeaderText="DepartureDate" SortExpression="DepartureDate" />
                <asp:BoundField DataField="ArrivalDate" HeaderText="ArrivalDate" SortExpression="ArrivalDate" />
                <asp:BoundField DataField="DepartureTime" HeaderText="DepartureTime" SortExpression="DepartureTime" />
                <asp:BoundField DataField="ArrivalTime" HeaderText="ArrivalTime" SortExpression="ArrivalTime" />
                <asp:BoundField DataField="Stops" HeaderText="Stops" SortExpression="Stops" />
                <asp:BoundField DataField="BookingStatus" HeaderText="Status" SortExpression="BookingStatus" />
                <asp:BoundField DataField="Fare" HeaderText="Fare" SortExpression="Fare" />
            </Columns>
            <EditRowStyle BackColor="#2461BF" />
            <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
            <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
            <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
            <RowStyle BackColor="#EFF3FB" />
            <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
        </asp:GridView>
        <asp:ObjectDataSource ID="ObjectDataSource2" runat="server" DeleteMethod="Delete" InsertMethod="Insert" OldValuesParameterFormatString="original_{0}" SelectMethod="GetDataByPNR" TypeName="FlightReservationTableAdapters.FlightTableAdapter" UpdateMethod="Update">
            <DeleteParameters>
                <asp:Parameter Name="Original_Flight_Id" Type="Int32" />
            </DeleteParameters>
            <InsertParameters>
                <asp:Parameter Name="Number" Type="String" />
                <asp:Parameter Name="Origin" Type="String" />
                <asp:Parameter Name="Destinetion" Type="String" />
                <asp:Parameter Name="DepartureDate" Type="DateTime" />
                <asp:Parameter Name="ArrivalDate" Type="DateTime" />
                <asp:Parameter Name="DepartureTime" Type="String" />
                <asp:Parameter Name="ArrivalTime" Type="String" />
                <asp:Parameter Name="Stops" Type="Decimal" />
                <asp:Parameter Name="Status" Type="String" />
                <asp:Parameter Name="Fare" Type="Decimal" />
            </InsertParameters>
            <SelectParameters>
                <asp:QueryStringParameter Name="PNRId" QueryStringField="pnrId" Type="Int32" />
            </SelectParameters>
            <UpdateParameters>
                <asp:Parameter Name="Number" Type="String" />
                <asp:Parameter Name="Origin" Type="String" />
                <asp:Parameter Name="Destinetion" Type="String" />
                <asp:Parameter Name="DepartureDate" Type="DateTime" />
                <asp:Parameter Name="ArrivalDate" Type="DateTime" />
                <asp:Parameter Name="DepartureTime" Type="String" />
                <asp:Parameter Name="ArrivalTime" Type="String" />
                <asp:Parameter Name="Stops" Type="Decimal" />
                <asp:Parameter Name="Status" Type="String" />
                <asp:Parameter Name="Fare" Type="Decimal" />
                <asp:Parameter Name="Original_Flight_Id" Type="Int32" />
            </UpdateParameters>
        </asp:ObjectDataSource>
    </p>
     <h3>
         Payment Info</h3>
     <p>
         <asp:DetailsView ID="DetailsView2" runat="server" AutoGenerateRows="False" CellPadding="4" DataKeyNames="Payment_Id" DataSourceID="ObjectDataSource3" EnableModelValidation="True" ForeColor="#333333" GridLines="None" Height="50px" Width="125px">
             <AlternatingRowStyle BackColor="White" />
             <CommandRowStyle BackColor="#D1DDF1" Font-Bold="True" />
             <EditRowStyle BackColor="#2461BF" />
             <FieldHeaderStyle BackColor="#DEE8F5" Font-Bold="True" />
             <Fields>
                 <asp:BoundField DataField="Payment_Id" HeaderText="Payment_Id" InsertVisible="False" ReadOnly="True" SortExpression="Payment_Id" />
                 <asp:BoundField DataField="PNR_Id" HeaderText="PNR_Id" SortExpression="PNR_Id" />
                 <asp:BoundField DataField="CardType" HeaderText="CardType" SortExpression="CardType" />
                 <asp:BoundField DataField="CardNumber" HeaderText="CardNumber" SortExpression="CardNumber" />
                 <asp:BoundField DataField="SecurityCode" HeaderText="SecurityCode" SortExpression="SecurityCode" />
                 <asp:BoundField DataField="ExpiryDateMonth" HeaderText="ExpiryDateMonth" SortExpression="ExpiryDateMonth" />
                 <asp:BoundField DataField="ExpiryDateYear" HeaderText="ExpiryDateYear" SortExpression="ExpiryDateYear" />
                 <asp:BoundField DataField="PaymentStatus" HeaderText="PaymentStatus" SortExpression="PaymentStatus" />
             </Fields>
             <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
             <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
             <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
             <RowStyle BackColor="#EFF3FB" />
         </asp:DetailsView>
        <asp:ObjectDataSource ID="ObjectDataSource3" runat="server" DeleteMethod="Delete" InsertMethod="Insert" OldValuesParameterFormatString="original_{0}" SelectMethod="GetDataByPNR" TypeName="FlightReservationTableAdapters.PaymentTableAdapter" UpdateMethod="Update">
             <DeleteParameters>
                 <asp:Parameter Name="Original_Payment_Id" Type="Int32" />
             </DeleteParameters>
             <InsertParameters>
                 <asp:Parameter Name="PNR_Id" Type="Int32" />
                 <asp:Parameter Name="CardType" Type="String" />
                 <asp:Parameter Name="CardNumber" Type="String" />
                 <asp:Parameter Name="SecurityCode" Type="String" />
                 <asp:Parameter Name="ExpiryDateMonth" Type="String" />
                 <asp:Parameter Name="ExpiryDateYear" Type="String" />
                 <asp:Parameter Name="PaymentStatus" Type="String" />
             </InsertParameters>
             <SelectParameters>
                 <asp:QueryStringParameter Name="PNRId" QueryStringField="pnrId" Type="Int32" />
             </SelectParameters>
             <UpdateParameters>
                 <asp:Parameter Name="PNR_Id" Type="Int32" />
                 <asp:Parameter Name="CardType" Type="String" />
                 <asp:Parameter Name="CardNumber" Type="String" />
                 <asp:Parameter Name="SecurityCode" Type="String" />
                 <asp:Parameter Name="ExpiryDateMonth" Type="String" />
                 <asp:Parameter Name="ExpiryDateYear" Type="String" />
                 <asp:Parameter Name="PaymentStatus" Type="String" />
                 <asp:Parameter Name="Original_Payment_Id" Type="Int32" />
             </UpdateParameters>
         </asp:ObjectDataSource>

        </p><p>   <h3>
         Card Holder Info</h3>

            <asp:DetailsView ID="DetailsView3" runat="server" AutoGenerateRows="False" CellPadding="4" DataKeyNames="CardHolder_Id" DataSourceID="ObjectDataSource4" EnableModelValidation="True" ForeColor="#333333" GridLines="None" Height="50px" Width="125px">
             <AlternatingRowStyle BackColor="White" />
             <CommandRowStyle BackColor="#D1DDF1" Font-Bold="True" />
             <EditRowStyle BackColor="#2461BF" />
             <FieldHeaderStyle BackColor="#DEE8F5" Font-Bold="True" />
             <Fields>
                 <asp:BoundField DataField="CardHolder_Id" HeaderText="CardHolder_Id" InsertVisible="False" ReadOnly="True" SortExpression="CardHolder_Id" />
                 <asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
                 <asp:BoundField DataField="IsTraveling" HeaderText="IsTraveling" SortExpression="IsTraveling" />
                 <asp:BoundField DataField="Street" HeaderText="Street" SortExpression="Street" />
                 <asp:BoundField DataField="City" HeaderText="City" SortExpression="City" />
                 <asp:BoundField DataField="PostalCode" HeaderText="PostalCode" SortExpression="PostalCode" />
                 <asp:BoundField DataField="Country" HeaderText="Country" SortExpression="Country" />
                 <asp:BoundField DataField="Payment_Id" HeaderText="Payment_Id" SortExpression="Payment_Id" />
             </Fields>
             <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
             <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
             <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
             <RowStyle BackColor="#EFF3FB" />
         </asp:DetailsView>
         <asp:ObjectDataSource ID="ObjectDataSource4" runat="server" DeleteMethod="Delete" InsertMethod="Insert" OldValuesParameterFormatString="original_{0}" SelectMethod="GetDataByPNR" TypeName="FlightReservationTableAdapters.CardHolderTableAdapter" UpdateMethod="Update">
             <DeleteParameters>
                 <asp:Parameter Name="Original_CardHolder_Id" Type="Int32" />
             </DeleteParameters>
             <InsertParameters>
                 <asp:Parameter Name="Name" Type="String" />
                 <asp:Parameter Name="IsTraveling" Type="String" />
                 <asp:Parameter Name="Street" Type="String" />
                 <asp:Parameter Name="City" Type="String" />
                 <asp:Parameter Name="PostalCode" Type="String" />
                 <asp:Parameter Name="Country" Type="String" />
                 <asp:Parameter Name="Payment_Id" Type="Int32" />
             </InsertParameters>
             <SelectParameters>
                 <asp:QueryStringParameter Name="PNRId" QueryStringField="pnrId" Type="Int32" />
             </SelectParameters>
             <UpdateParameters>
                 <asp:Parameter Name="Name" Type="String" />
                 <asp:Parameter Name="IsTraveling" Type="String" />
                 <asp:Parameter Name="Street" Type="String" />
                 <asp:Parameter Name="City" Type="String" />
                 <asp:Parameter Name="PostalCode" Type="String" />
                 <asp:Parameter Name="Country" Type="String" />
                 <asp:Parameter Name="Payment_Id" Type="Int32" />
                 <asp:Parameter Name="Original_CardHolder_Id" Type="Int32" />
             </UpdateParameters>
         </asp:ObjectDataSource>

            </p>
    <p>
        &nbsp;</p>
        <input type=button name=print value="Print" onclick="javascript:window.print()">
</asp:Content>

4 个答案:

答案 0 :(得分:2)

我不确定我是否正确理解了您的问题,但这是一种可以在数据绑定控件中显示多个字段的方法。

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

我建议将来不要使用ObjectDataSource,因为它在代码中无法做很多事情,并且当你需要自定义行为时它会受到阻碍。

答案 1 :(得分:0)

只需创建一个功能

CREATE FUNCTION [dbo].[Func_GetFullName]
(
    @PatID nvarchar(50) = NULL
)
RETURNS nvarchar(50)
AS
BEGIN
Declare @PatientName nvarchar(100)

Select @PatientName= (isnull(FName,'')+' '+isnull(MName,'') + ' '+isnull(LName,''))   FROM IE_Patients where PatID = @PatID
RETURN @PatientName
END

在查询中调用您想要获取名称的位置。使用此功能,您将获得一个名称的列。

答案 2 :(得分:0)

将以下代码放入.aspx页面

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"  >
    <Columns>
        <asp:TemplateField HeaderText="Name" SortExpression="FirstName">
        <ItemTemplate>
           <asp:Label ID="lblName" runat="server" Text='<%# Eval("FirstName") +" "+ Eval("LastName")%>' ></asp:Label>
        </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

试试这个。这肯定会对你产生影响。

答案 3 :(得分:0)

使用此代码

<asp:TemplateField HeaderText="Name" SortExpression="Name">
            <ItemTemplate>
                <asp:Label ID="Name" runat="server" Text='<%#Eval("FirstName")+ "-" + Eval("LastName")%>' ></asp:Label>
            </ItemTemplate>