【问题标题】:Database showing what gridview should, gridview not completely filling数据库显示 gridview 应该是什么,gridview 没有完全填充
【发布时间】:2014-08-15 13:56:45
【问题描述】:

只是为了澄清一下,让我的标题有意义:我在我的数据库中测试了我的查询,它确实有效,所有数据都显示了。当我把它放在 gridview 中时,它不会填充最后一个字段。我不确定是不是因为有两个数据字段被称为“笔记”,或者我只是遗漏了一些东西,一遍又一遍地阅读它并没有帮助。

这是我的查询(我不擅长格式化):

cmd.CommandText =

@"SELECT c.customer_id, c.customer_name, c.product_mgr, p.license_start_date, p.version, pd.processor, pd.notes, ci.f_name, ci.l_name, ci.phone, ci.email, ci.title, ci.notes FROM Customer c LEFT OUTER JOIN " + p + @" p ON c.customer_id = p.customer_id LEFT OUTER JOIN " + p + @"Details pd ON p.customer_id = pd.customer_id LEFT OUTER JOIN ContactInfo ci ON c.customer_id = ci.customer_id ORDER BY customer_id";

这是我的网格视图:

<asp:GridView id="gvProd" runat="server" EnableViewState="true" AutoGenerateColumns="false">

    <Columns>
        <asp:boundfield datafield="customer_id" headertext="Customer ID" />
        <asp:boundfield datafield="customer_name" headertext="Customer Name" />
        <asp:BoundField DataField="product_mgr" HeaderText="Product Mgr" />
        <asp:BoundField DataField="license_start_date" HeaderText="Start Date" DataFormatString="{0:d}" />
        <asp:BoundField DataField="version" headertext="Product Version" />
        <asp:BoundField DataField="processor" HeaderText="Payment Processor" /> 
        <asp:BoundField DataField="notes" HeaderText="Product Notes" />
        <asp:BoundField DataField="f_name" HeaderText="First Name" />
        <asp:BoundField DataField="l_name" HeaderText="Last Name" />
        <asp:BoundField DataField="phone" HeaderText="Phone" />
        <asp:BoundField DataField="email" HeaderText="Email" />
        <asp:BoundField DataField="title" HeaderText="Position/Title" />
        <asp:BoundField DataField="notes" HeaderText="Contact Notes" />
    </Columns>
    </asp:GridView>

该死,我认为最重要的部分我忽略了。第二个注释字段未填写。我知道里面有数据。但是其他联系人的东西已经填满了,所以我知道那是有效的

【问题讨论】:

    标签: c# sql asp.net gridview


    【解决方案1】:

    您在查询中有两个名为 notes 的字段。为其中一个添加别名并在网格视图中使用它。

    cmd.CommandText =
    @"SELECT c.customer_id, c.customer_name, c.product_mgr, p.license_start_date, p.version, 
    pd.processor, pd.notes AS product_notes, ci.f_name, ci.l_name, ci.phone, ci.email, 
    ci.title, ci.notes AS contact_notes FROM Customer c LEFT OUTER JOIN " + p + @" p ON c.customer_id = p.customer_id
    LEFT OUTER JOIN " + p + @"Details pd ON p.customer_id = pd.customer_id 
    LEFT OUTER JOIN ContactInfo ci ON c.customer_id = ci.customer_id ORDER BY customer_id";
    
    
    
    <asp:GridView id="gvProd" runat="server" EnableViewState="true" AutoGenerateColumns="false">
    
        <Columns>
            <asp:boundfield datafield="customer_id" headertext="Customer ID" />
            <asp:boundfield datafield="customer_name" headertext="Customer Name" />
            <asp:BoundField DataField="product_mgr" HeaderText="Product Mgr" />
            <asp:BoundField DataField="license_start_date" HeaderText="Start Date" DataFormatString="{0:d}" />
            <asp:BoundField DataField="version" headertext="Product Version" />
            <asp:BoundField DataField="processor" HeaderText="Payment Processor" /> 
            <asp:BoundField DataField="product_notes" HeaderText="Product Notes" />
            <asp:BoundField DataField="f_name" HeaderText="First Name" />
            <asp:BoundField DataField="l_name" HeaderText="Last Name" />
            <asp:BoundField DataField="phone" HeaderText="Phone" />
            <asp:BoundField DataField="email" HeaderText="Email" />
            <asp:BoundField DataField="title" HeaderText="Position/Title" />
            <asp:BoundField DataField="contact_notes" HeaderText="Contact Notes" />
        </Columns>
        </asp:GridView>
    

    【讨论】:

    • 我不知道这是一回事,谢谢。有空时我会标记为答案
    • 列的名称是软件(无论是 SQL Server 还是 .NET)区分列的方式。在这种情况下,SQL Server 可能会为第二个注释列提供一些生成的名称,因此它不匹配。顺便说一句,第一个注释列是否正确显示?
    • 如果不是以前,就是现在。该字段一开始就已经是空的(数据库仍然没有完全填充),但我添加了一些假人只是为了检查。
    • 是的,你可能也需要一个别名。
    【解决方案2】:

    您是否尝试为您的笔记(在查询中)提供别名,然后将别名用作该列的数据字段值?

    可能是这样的:

    cmd.CommandText = @"SELECT c.customer_id, c.customer_name, c.product_mgr, p.license_start_date, p.version, 
    pd.processor, pd.notes as Pd_Note, ci.f_name, ci.l_name, ci.phone, ci.email, 
    ci.title, ci.notes as CI_Note FROM Customer c LEFT OUTER JOIN " + p + @" p ON c.customer_id = p.customer_id
    LEFT OUTER JOIN " + p + @"Details pd ON p.customer_id = pd.customer_id 
    LEFT OUTER JOIN ContactInfo ci ON c.customer_id = ci.customer_id ORDER BY customer_id";
    

    然后:

    <Columns>
        <asp:boundfield datafield="customer_id" headertext="Customer ID" />
        <asp:boundfield datafield="customer_name" headertext="Customer Name" />
        <asp:BoundField DataField="product_mgr" HeaderText="Product Mgr" />
        <asp:BoundField DataField="license_start_date" HeaderText="Start Date" DataFormatString="{0:d}" />
        <asp:BoundField DataField="version" headertext="Product Version" />
        <asp:BoundField DataField="processor" HeaderText="Payment Processor" /> 
        <asp:BoundField DataField="Pd_Note" HeaderText="Product Notes" />
        <asp:BoundField DataField="f_name" HeaderText="First Name" />
        <asp:BoundField DataField="l_name" HeaderText="Last Name" />
        <asp:BoundField DataField="phone" HeaderText="Phone" />
        <asp:BoundField DataField="email" HeaderText="Email" />
        <asp:BoundField DataField="title" HeaderText="Position/Title" />
        <asp:BoundField DataField="CI_Note" HeaderText="Contact Notes" />
    </Columns>
    </asp:GridView>
    

    【讨论】:

      猜你喜欢
      • 2013-02-17
      • 2011-08-18
      • 2014-09-07
      • 2020-12-12
      • 1970-01-01
      • 1970-01-01
      • 2014-03-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多