如何仅在 GridView 中的交替行上显示数据。我需要将奇数行:1、3、5、7 等...留空。
最佳答案
您可以选择不显示 AlternatingItemTemplate
中的数据像这样。
标记
<asp:GridView ID="SimpleGrid" runat="server"
AutoGenerateColumns="false"
AlternatingRowStyle-BackColor="Gray"
AlternatingRowStyle-Height="10px"
RowStyle-Height="10px">
<Columns>
<asp:TemplateField HeaderText="Demo">
<ItemTemplate><%# Container.DataItem %></ItemTemplate>
<AlternatingItemTemplate></AlternatingItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
代码
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindSimpleGrid();
}
}
private void BindSimpleGrid()
{
var items =
new List<string>{ "Item 0", "Item 1", "Item 2", "Item 3", "Item 4"};
SimpleGrid.DataSource = items;
SimpleGrid.DataBind();
}
更新:如果您想添加空白行并仍显示所有项目,asp:ListView
就是最佳选择。
标记。
<asp:ListView ID="SimpleGrid" runat="server">
<LayoutTemplate>
<table>
<thead>
<tr>
<th>Heading</th>
</tr>
</thead>
<tbody id="itemPlaceHolder" runat="server"></tbody>
</table>
</LayoutTemplate>
<ItemTemplate>
<tr style="height: 20px;"><td><%# Container.DataItem %></td></tr>
<tr style="background-color: #ddd; height: 20px;"><td></td></tr>
</ItemTemplate>
</asp:ListView>
关于c# - 在gridview中,如何仅在交替行上显示数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37703304/