我在gridview中有一些数据如下。
我想合并gridview中的行,如下所示。
我已经尝试使用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>
请帮助我,因为我不知道。感谢。
答案 0 :(得分:4)
您需要尝试DataBound
而不是RowDataBound
DataBound
触发。
RowDataBound
将触发每一行。
<强> 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;
}
}
}
}
答案 1 :(得分:0)
正如Vignesh Kumar所建议的那样,在填充了所有行之后,可以在DataBound
事件中完成处理。
为了检索字段值,我建议将它们添加到GridView的DataKeyNames
属性中:
<asp:GridView ID="GridView1" runat="server" DataKeyNames="ClientCompany,Position" OnDataBound="GridView1_DataBound" ... />
以下是组合单元格的代码:
protected void GridView1_DataBound(object sender, EventArgs e)
{
for (int currentRowIndex = 0; currentRowIndex < GridView1.Rows.Count; currentRowIndex++)
{
GridViewRow currentRow = GridView1.Rows[currentRowIndex];
CombineColumnCells(currentRow, 0, "ClientCompany");
CombineColumnCells(currentRow, 1, "Position");
}
}
private void CombineColumnCells(GridViewRow currentRow, int colIndex, string fieldName)
{
TableCell currentCell = currentRow.Cells[colIndex];
if (currentCell.Visible)
{
Object currentValue = GridView1.DataKeys[currentRow.RowIndex].Values[fieldName];
for (int nextRowIndex = currentRow.RowIndex + 1; nextRowIndex < GridView1.Rows.Count; nextRowIndex++)
{
Object nextValue = GridView1.DataKeys[nextRowIndex].Values[fieldName];
if (nextValue.ToString() == currentValue.ToString())
{
GridViewRow nextRow = GridView1.Rows[nextRowIndex];
TableCell nextCell = nextRow.Cells[colIndex];
currentCell.RowSpan = Math.Max(1, currentCell.RowSpan) + 1;
nextCell.Visible = false;
}
else
{
break;
}
}
}
}
答案 2 :(得分:0)
我已经在vb.net代码的第一列后面创建了
注意:列应为BoundField。
<asp:BoundField DataField="ProductID" HeaderText="ProductID">
<ItemStyle Width="70px" HorizontalAlign="Center" />
</asp:BoundField>
后面的VB.Net代码:
Private Sub gvGridView_DataBound(sender As Object, e As EventArgs) Handles gvGridView.DataBound
Dim rowCount As Integer = gvTranscript1.Rows.Count
Dim RowIndex As Integer = 0
Dim gvRow As GridViewRow
Dim gvPrevRow As GridViewRow
Dim cellIndex As Integer = 0
For RowIndex = rowCount - 2 To 0 Step -1
gvRow = gvTranscript1.Rows(RowIndex) 'second last
gvPrevRow = gvTranscript1.Rows(RowIndex + 1) 'last
'lblmsg.Text += gvRow.Cells(0).Text & " - " & gvPrevRow.Cells(0).Text & "<br>"
If gvRow.Cells(0).Text = gvPrevRow.Cells(0).Text Then
If gvPrevRow.Cells(cellIndex).RowSpan < 2 Then
gvRow.Cells(cellIndex).RowSpan = 2
Else
gvRow.Cells(cellIndex).RowSpan = gvPrevRow.Cells(cellIndex).RowSpan + 1
End If
gvPrevRow.Cells(0).Visible = False
End If
Next
End Sub
答案 3 :(得分:0)
Dim rows = GridView1.Rows.Cast(Of GridViewRow).Union(GridView2.Rows.Cast(Of GridViewRow)).ToArray().Select(Function(f) New With {.YourColumn1 = f.Cells(0).Text, .YourColumn2 = f.Cells(1).Text, .YourColumnN = f.Cells(n).Text})
GridView1.DataSource = rows
GridView1.DataBind()
答案 4 :(得分:0)
移除ItemTemplate并取boundField Columns