GridView在输出中不可见

时间:2014-03-17 12:39:04

标签: c# asp.net gridview left-join sqldatasource

好! 我试图合并4个表,我在SelectCommand中有别名。
问题 - > GridView在最终输出中不可见 猜测 - >我甚至试过没有别名(还没有工作)。所以我有点迷失,因为我出错了

告诉我是否可以提供更多信息?

    <asp:HiddenField runat="server" ID="FirstName_HiddenField" Value="s"/>
    <div id="StudentDetails">
        <div id="StudentDetailsBanner">
            Student Details
        </div>
        <div id="StudentDetailsContent">
            <asp:SqlDataSource runat="server"
                 ID="SqlDataSource_FirstName"
                 ConnectionString="Data Source=NIRMIT-PC\SQLEXPRESS;Initial Catalog=KohinoorDatabaseTrial3;Integrated Security=True" 
                 SelectCommand="SELECT SD.enq_id, SD.reg_id, SD.first_name, SD.middle_name, SD.last_name, SED.enq_date, POD.name, SD.DOB, SD.contact_no1, SCD.course, SD.admission_type
                                    FROM StudentDetails AS SD
                                    LEFT OUTER JOIN StudentEnquiryDetails AS SED
                                    ON SD.enq_id = SED.enq_id
                                    LEFT OUTER JOIN StudentCourseDetails AS SCD 
                                    ON SD.enq_id = SCD.enq_id
                                    LEFT OUTER JOIN ParentOccupationDetails AS POD 
                                    ON SD.enq_id = POD.enq_id
                                    WHERE SD.first_name LIKE '%@FirstName%'">
                <SelectParameters>
                    <asp:ControlParameter ControlID="FirstName_HiddenField" Name="FirstName" />
                </SelectParameters>
            </asp:SqlDataSource>
            <asp:GridView runat="server"
                ID="StudentDetailsGridView" 
                OnSelectedIndexChanged="StudentDetailsGridView_SelectedIndexChanged" 
                AutoGenerateColumns="false"
                Width="100%" 
                AllowPaging="true" 
                PageSize="10"
                CssClass="GridViewTable" ViewStateMode="Enabled" Visible="true">

                <Columns>
                    <asp:BoundField DataField="SD.enq_id" HeaderText="Enq No" />
                    <asp:BoundField DataField="SD.reg_id" HeaderText="Reg No" />
                    <asp:TemplateField>
                        <ItemTemplate>
                            <asp:Label runat="server" ID="Name" Text='<%# Bind("SD.first_name") + " " + Bind("SD.middle_name") + " " + Bind("SD.last_name") %>'></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:BoundField DataField="SED.enq_date" HeaderText="Enq Date" />
                    <asp:BoundField DataField="POD.name" HeaderText="Guardian" />
                    <asp:BoundField DataField="SD.DOB" HeaderText="Date of Birth" />
                    <asp:BoundField DataField="SDcontact_no1" HeaderText="Contact No" />
                    <asp:BoundField DataField="SCD.course" HeaderText="Course" />
                    <asp:BoundField DataField="SD.admission_type" HeaderText="Admission Type" />
                </Columns>
                <HeaderStyle CssClass="TableHeadingRow" />
                <RowStyle CssClass="TableRow" />
            </asp:GridView>                 
        </div>
    </div>

背后的代码:

protected void Page_Load(object sender, EventArgs e)
{
    StudentDetailsGridView.DataSourceID = SqlDataSource_FirstName.ID;
    StudentDetailsGridView.DataBind();
}

2 个答案:

答案 0 :(得分:1)

不应该

StudentDetailsGridView.DataSource = SqlDataSource_FirstName;

...

在代码背后?我认为你没有正确地将你的SqlDataSource绑定到gridview,这是你的问题...

答案 1 :(得分:1)

哇!!知道了!

错误1)不要在DataField属性中使用Alias (我的理论 - DataField使用的是最终结果表的列名,它没有别名)
错误2)&#39;%&#39; + @FirstName +&#39;%&#39; 而不是%@ FirstName%

所以只是为了获得概述并让这个问题对其他人有所帮助

<asp:HiddenField runat="server" ID="FirstName_HiddenField" Value="s"/>
    <asp:HiddenField runat="server"/>
    <div id="StudentDetails">
        <div id="StudentDetailsBanner">
            Student Details
        </div>
        <div id="StudentDetailsContent">
            <asp:SqlDataSource runat="server"
                 ID="SqlDataSource_FirstName"
                 ConnectionString="Data Source=NIRMIT-PC\SQLEXPRESS;Initial Catalog=KohinoorDatabaseTrial3;Integrated Security=True" 
                 SelectCommand="SELECT SD.enq_id, SD.reg_id, SD.first_name, SD.middle_name, SD.last_name, SED.enq_date, POD.name, SD.DOB, SD.contact_no1, SCD.course, SD.admission_type
                                    FROM StudentDetails AS SD
                                    LEFT OUTER JOIN StudentEnquiryDetails AS SED
                                    ON SD.enq_id = SED.enq_id
                                    LEFT OUTER JOIN StudentCourseDetails AS SCD 
                                    ON SD.enq_id = SCD.enq_id
                                    LEFT OUTER JOIN ParentOccupationDetails AS POD 
                                    ON SD.enq_id = POD.enq_id
                                    WHERE SD.first_name LIKE '%' + @FirstName + '%'" >
                <SelectParameters>
                    <asp:ControlParameter ControlID="FirstName_HiddenField"  Name="FirstName" />
                </SelectParameters>
            </asp:SqlDataSource>
            <asp:GridView runat="server"
                ID="StudentDetailsGridView" 
                OnSelectedIndexChanged="StudentDetailsGridView_SelectedIndexChanged" 
                AutoGenerateColumns="false"
                Width="100%" 
                AllowPaging="true" 
                PageSize="10"
                CssClass="GridViewTable" ViewStateMode="Enabled" Visible="true" EmptyDataText="No Result Found">

                <Columns>
                    <asp:BoundField DataField="enq_id" HeaderText="Enq No" />
                    <asp:BoundField DataField="reg_id" HeaderText="Reg No" />
                    <asp:TemplateField HeaderText="Name">
                        <ItemTemplate>
                            <asp:Label runat="server" ID="Name" Text='<%# Bind("first_name") + " " + Bind("middle_name") + " " + Bind("last_name") %>'></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:BoundField DataField="enq_date" HeaderText="Enq Date" />
                    <asp:BoundField DataField="name" HeaderText="Guardian" />
                    <asp:BoundField DataField="DOB" HeaderText="Date of Birth" />
                    <asp:BoundField DataField="contact_no1" HeaderText="Contact No" />
                    <asp:BoundField DataField="course" HeaderText="Course" />
                    <asp:BoundField DataField="admission_type" HeaderText="Admission Type" />
                </Columns>
                <HeaderStyle CssClass="TableHeadingRow" />
                <RowStyle CssClass="TableRow" />
            </asp:GridView>                 
        </div>

    </div>

干杯们:)