数据库显示gridview应该是什么,gridview没有完全填充

时间:2014-08-15 13:56:45

标签: c# sql asp.net gridview

只是为了清理事情,所以我的标题是有道理的:我在我的数据库中测试了我的查询,它确实有效,所有数据都显示出来。当我把它放在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";

这是我的gridview:

<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>

我觉得我离开了最重要的部分。第二个备注字段不会填写。我知道里面有数据。但其他联系人的东西填补,所以我知道这是工作

2 个答案:

答案 0 :(得分:3)

在查询中有两个名为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>

答案 1 :(得分:2)

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

也许是这样的:

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>