将多条记录数据绑定到 asp.net gridview 中的单个垂直列 table
Databinding multiple records to a single vertical-column table in asp.net gridview
我使用选择方法获取数据并且工作正常,问题是我不想为每条插入的记录创建一个新的 table。我想在现有 table 中添加一个新列。类似下图
<asp:Content ID="Content1" ContentPlaceHolderID="MainContent" runat="server">
<asp:GridView ID="FormView1" runat="server" ShowHeader="False" ItemType="MieleRepresentative.SteamBoiler" SelectMethod="GetSteamBoilerModels" RenderOuterTable="false" AutoGenerateColumns="False">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<table class="table-striped table-bordered">
<tr style="background-color: thistle!important;">
<td style="font-weight: bold;">Model</td>
<td><%# Eval("Model")%></td>
</tr>
<tr>
<td style="font-weight: bold">WorkingPressureLimit</td>
<td><%# Eval("WorkingPressureLimit")%></td>
</tr>
<tr>
<td style="font-weight: bold">TestPressureLimit</td>
<td><%# Eval("TestPressureLimit")%></td>
</tr>
<tr>
<td style="font-weight: bold">Electrical</td>
<td><%# Eval("Electrical")%></td>
</tr>
<tr>
<td style="font-weight: bold">Diameter</td>
<td><%# Eval("Diameter")%></td>
</tr>
<tr>
<td style="font-weight: bold">Height</td>
<td><%# Eval("Height")%></td>
</tr>
<tr>
<td style="font-weight: bold">Weight</td>
<td><%# Eval("Weight")%></td>
</tr>
</table>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
public IQueryable<SteamBoiler> GetSteamBoilerModels([QueryString("productID")] Guid productId)
{
var _mieleDbEntities = new MieleDBEntities();
return _mieleDbEntities.SteamBoilers.Where(boiler => boiler.FKProduct == productId);
}
如果您想要 table 具有未定义的列数(每条记录对应一个列),GridView 可能不是您的最佳选择。
在我看来,您应该以这种方式在 .aspx 页面中动态构建 table:
<% var steamBoilersList = GetSteamBoilerModels(...params...); %>
<table>
<!-- first row -->
<tr>
<% foreach (var singleSteamBoiler in steamBoilersList) { %>
<td>
<%= singleSteamBoiler.FIELD1 %>
</td>
</tr>
<!-- second row and so on -->
<tr>
<% foreach (var singleSteamBoiler in steamBoilersList) { %>
<td>
<%= singleSteamBoiler.FIELD2 %>
</td>
</tr>
</table>
我使用选择方法获取数据并且工作正常,问题是我不想为每条插入的记录创建一个新的 table。我想在现有 table 中添加一个新列。类似下图
<asp:Content ID="Content1" ContentPlaceHolderID="MainContent" runat="server">
<asp:GridView ID="FormView1" runat="server" ShowHeader="False" ItemType="MieleRepresentative.SteamBoiler" SelectMethod="GetSteamBoilerModels" RenderOuterTable="false" AutoGenerateColumns="False">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<table class="table-striped table-bordered">
<tr style="background-color: thistle!important;">
<td style="font-weight: bold;">Model</td>
<td><%# Eval("Model")%></td>
</tr>
<tr>
<td style="font-weight: bold">WorkingPressureLimit</td>
<td><%# Eval("WorkingPressureLimit")%></td>
</tr>
<tr>
<td style="font-weight: bold">TestPressureLimit</td>
<td><%# Eval("TestPressureLimit")%></td>
</tr>
<tr>
<td style="font-weight: bold">Electrical</td>
<td><%# Eval("Electrical")%></td>
</tr>
<tr>
<td style="font-weight: bold">Diameter</td>
<td><%# Eval("Diameter")%></td>
</tr>
<tr>
<td style="font-weight: bold">Height</td>
<td><%# Eval("Height")%></td>
</tr>
<tr>
<td style="font-weight: bold">Weight</td>
<td><%# Eval("Weight")%></td>
</tr>
</table>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
public IQueryable<SteamBoiler> GetSteamBoilerModels([QueryString("productID")] Guid productId)
{
var _mieleDbEntities = new MieleDBEntities();
return _mieleDbEntities.SteamBoilers.Where(boiler => boiler.FKProduct == productId);
}
如果您想要 table 具有未定义的列数(每条记录对应一个列),GridView 可能不是您的最佳选择。
在我看来,您应该以这种方式在 .aspx 页面中动态构建 table:
<% var steamBoilersList = GetSteamBoilerModels(...params...); %>
<table>
<!-- first row -->
<tr>
<% foreach (var singleSteamBoiler in steamBoilersList) { %>
<td>
<%= singleSteamBoiler.FIELD1 %>
</td>
</tr>
<!-- second row and so on -->
<tr>
<% foreach (var singleSteamBoiler in steamBoilersList) { %>
<td>
<%= singleSteamBoiler.FIELD2 %>
</td>
</tr>
</table>