gpt4 book ai didi

c# - 如何在gridview中合并行

转载 作者:太空宇宙 更新时间:2023-11-03 21:12:38 25 4
gpt4 key购买 nike

我在 gridview 中有一些数据如下。

enter image description here

我想合并 gridview 中的行,如下所示。

enter image description here

我已经在 RowDataBound() 和 PreRender() 中尝试了以下代码,结果与我想要的不一样。

for (int rowIndex = GridView1.Rows.Count - 2; rowIndex >= 0; rowIndex--)
{
GridViewRow gvRow = GridView1.Rows[rowIndex];
GridViewRow gvPreviousRow = GridView1.Rows[rowIndex + 1];
for (int cellCount = 0; cellCount < gvRow.Cells.Count; cellCount++)
{
if (gvRow.Cells[cellCount].Text ==
gvPreviousRow.Cells[cellCount].Text)
{
if (gvPreviousRow.Cells[cellCount].RowSpan < 2)
{
gvRow.Cells[cellCount].RowSpan = 2;
}
else
{
gvRow.Cells[cellCount].RowSpan =
gvPreviousRow.Cells[cellCount].RowSpan + 1;
}
gvPreviousRow.Cells[cellCount].Visible = false;
}
}
}

在 aspx 中,

<asp:GridView ID="GridView1" runat="server" Width="100%"
AutoGenerateColumns = "false" AlternatingRowStyle-BackColor = "#fffccc" HeaderStyle-ForeColor ="#ffffff"
HeaderStyle-BackColor = "#333" AllowPaging ="true" OnRowDataBound="GridView1_RowDataBound" PageSize = "20"
OnPageIndexChanging="GridView1_PageIndexChanging" OnPreRender="GridView1_PreRender">
<HeaderStyle Height="30px" />
<Columns>
<asp:TemplateField HeaderText="Client Company">
<ItemTemplate>
<asp:Label ID="lblClientCompany" runat="server" Text='<%# Eval("ClientCompany")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Position">
<ItemTemplate>
<asp:Label ID="lblPosition" runat="server" Text='<%# Eval("Position")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Candidate">
<ItemTemplate>
<asp:Label ID="lblCandidate" runat="server" Text='<%# Eval("Candidate")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Status">
<ItemTemplate>
asp:Label ID="lblStatus" runat="server" Text='<%# Eval("Status")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<AlternatingRowStyle BackColor="#fffccc" />
</asp:GridView>

请帮助我,因为我不知道。谢谢。

最佳答案

您需要尝试 DataBound 中的方法而不是 RowDataBound

DataBound 在 GridView 控件绑定(bind)到数据源后(绑定(bind)所有行后)触发。

RowDataBound 在数据行绑定(bind)到 GridView 控件中的数据时为每一行触发。

ASPX

<asp:GridView ID="GridView1" runat="server" Width="100%"
AutoGenerateColumns = "false" AlternatingRowStyle-BackColor = "#fffccc" HeaderStyle-ForeColor ="#ffffff"
HeaderStyle-BackColor = "#333" AllowPaging ="true" OnDataBound="GridView1_DataBound1" PageSize = "20"
OnPageIndexChanging="GridView1_PageIndexChanging">
<HeaderStyle Height="30px" />
<Columns>
<asp:TemplateField HeaderText="Client Company">
<ItemTemplate>
<asp:Label ID="lblClientCompany" runat="server" Text='<%# Eval("ClientCompany")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Position">
<ItemTemplate>
<asp:Label ID="lblPosition" runat="server" Text='<%# Eval("Position")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Candidate">
<ItemTemplate>
<asp:Label ID="lblCandidate" runat="server" Text='<%# Eval("Candidate")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Status">
<ItemTemplate>
asp:Label ID="lblStatus" runat="server" Text='<%# Eval("Status")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<AlternatingRowStyle BackColor="#fffccc" />
</asp:GridView>

代码隐藏

protected void GridView1_DataBound1(object sender, System.EventArgs e)
{
for (int rowIndex = GridView1.Rows.Count - 2; rowIndex >= 0; rowIndex--)
{
GridViewRow gvRow = GridView1.Rows[rowIndex];
GridViewRow gvPreviousRow = GridView1.Rows[rowIndex + 1];
for (int cellCount = 0; cellCount < gvRow.Cells.Count; cellCount++)
{
if (gvRow.Cells[cellCount].Text ==
gvPreviousRow.Cells[cellCount].Text)
{
if (gvPreviousRow.Cells[cellCount].RowSpan < 2)
{
gvRow.Cells[cellCount].RowSpan = 2;
}
else
{
gvRow.Cells[cellCount].RowSpan =
gvPreviousRow.Cells[cellCount].RowSpan + 1;
}
gvPreviousRow.Cells[cellCount].Visible = false;
}
}
}
}

关于c# - 如何在gridview中合并行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36642431/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com