数据库显示gridview应该,gridview不完全填充
本文关键字:gridview 不完全 填充 应该 显示 数据库 | 更新日期: 2023-09-27 18:18:04
只是为了澄清事情,所以我的标题是有意义的:我在我的数据库中测试了我的查询,它确实工作,所有的数据显示。当我把它放在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>
我想我漏掉了最重要的部分。第二个备注字段没有填充。我知道里面有数据。但是其他联系人的内容填充了所以我知道这是有效的
查询中有两个名为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>
您是否尝试过为Notes(在查询中)提供别名,然后使用别名作为该列的Datafield值?
也许像这样:
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>