Reputation: 499
I have some data in gridview as below.
I want to merge the rows in gridview as below.
I already tried with below code in RowDataBound() and PreRender(), the result is not the same as I want.
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;
}
}
}
In 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>
Please help me becasue I have no idea. Thanks.
Upvotes: 2
Views: 13859
Reputation: 911
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()
Upvotes: 0
Reputation: 339
I have created for first column in vb.net code behind:
Note: Column should be BoundField.
<asp:BoundField DataField="ProductID" HeaderText="ProductID">
<ItemStyle Width="70px" HorizontalAlign="Center" />
</asp:BoundField>
VB.Net code behind:
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
Upvotes: 0
Reputation: 73791
As suggested by Vignesh Kumar, the processing can be done in the DataBound
event, after all the rows have been populated.
In order to retrieve the fields values, I suggest adding them to the DataKeyNames
attribute of the GridView:
<asp:GridView ID="GridView1" runat="server" DataKeyNames="ClientCompany,Position" OnDataBound="GridView1_DataBound" ... />
Here is the code to combine the cells:
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;
}
}
}
}
Upvotes: 0
Reputation: 28423
You need to try the method in DataBound
not RowDataBound
DataBound
fires after the GridView control binds to a data source(after all rows bound).
RowDataBound
fires for each row, when a data row is bound to data in a GridView control.
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>
Code Behind
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;
}
}
}
}
Upvotes: 5