【发布时间】:2016-03-29 18:18:23
【问题描述】:
我有一个 gridview 包含 30 列,其中几列我必须在其顶部设置单列,即我有 Leaf Name,Leaf Code,Leaf Value,Leaf Vendor Code,Leaf Id,Leaf ZOnal,Leaf Leveller 列,我需要在其顶部提供单列作为 Leaves Details。我做了以下代码在 rowdatabound 中没有成功,新的单列是最重要的。你能告诉我我做错了什么吗。我做的更重要的事情是,我设置了 gridview 属性PagerSettings Position top,这样它就可以在网格的顶部索引值 1,2,3 等。
这是我的网格视图
<asp:GridView ID="grdcell" runat="server" AutoGenerateColumns="False" AllowPaging="true" PageSize="13" DataKeyNames="firstname,location"
CellPadding="4" ForeColor="#333333" OnPageIndexChanging="grdNewcells_PageIndexChanging" OnRowDataBound="grdcell_RowDataBound" >
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
<Columns>
<asp:TemplateField HeaderText="Insert" HeaderStyle-HorizontalAlign="Left">
<HeaderTemplate>
<asp:CheckBox ID="insertall" Text="Insert All" onclick="checkAll(this);" runat="server" />
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox ID="insertChk" runat="server" />
</ItemTemplate>
<HeaderStyle HorizontalAlign="Left"></HeaderStyle>
</asp:TemplateField>
<asp:BoundField DataField="Leaf_NAME" HeaderText="Leaf Name" SortExpression="Leaf_NAME"></asp:BoundField>
<asp:BoundField DataField="Leaf_CODE" HeaderText="Leaf Code" SortExpression="Leaf_CODE"></asp:BoundField>
<asp:BoundField DataField="Leaf_Value" HeaderText="Leaf Value" SortExpression="Leaf_Value"></asp:BoundField>
<asp:BoundField DataField="LeafVENDOR_CODE" HeaderText="Leaf Vendor Code" SortExpression="LeafVENDOR_CODE"></asp:BoundField>
<asp:BoundField DataField="Leaf_ID" HeaderText="Leaf Id" SortExpression="Leaf_ID"></asp:BoundField>
<asp:BoundField DataField="Leaf_ZOnal" HeaderText="Leaf ZOnal" SortExpression="Leaf_ZOnal"></asp:BoundField>
<asp:BoundField DataField="Leaf_Leveller" HeaderText="Leaf Leveller" SortExpression="Leaf_Leveller"></asp:BoundField>
<asp:BoundField DataField="Loaction" HeaderText="Loaction" SortExpression="Loaction"></asp:BoundField>
以及我在row data bound 中所做的代码如下所示
GridViewRow gvRow = e.Row;
if (gvRow.RowType == DataControlRowType.Header)
{
if (gvRow.Cells[1].Text == "Leaf_NAME")
{
gvRow.Cells.Remove(gvRow.Cells[1]);
GridViewRow gvHeader = new GridViewRow(0, 0, DataControlRowType.DataRow, DataControlRowState.Insert);
TableCell headerCell0 = new TableCell()
{
Text = "Leaves Details",
HorizontalAlign = HorizontalAlign.Center,
RowSpan = 6
};
gvHeader.Cells.Add(headerCell0);
grdcell.Controls[0].Controls.AddAt(0, gvHeader);
}
}
【问题讨论】:
-
我和@peter 有同样的问题。在我目前的工作中,我不得不在很多地方做类似的事情,但我使用了相反的方法:一个带有包含子列的 TemplateField 的单列。
-
是同一个问题吗??你有代码吗?
-
哎呀,对不起,我很困惑。我以为彼得是评论你帖子的人,但你自己评论了。
-
我可以在答案中告诉你我的代码的想法。顺便说一句,我也被你的一行代码弄糊涂了:
RowSpan = 6。不应该是ColumnSpan = 6吗?如果是这样,您不应该删除标题行中的 5 个原始单元格吗? -
我认为我的代码有问题,你能发布你的建议代码吗,记住我的 PagerSettings 位置置顶