Hi Mehram,
As per your code use GridView OnDataBound event and change the GridView cell index to 1.
Loop should be till gvUserRole.Rows.Count - 1 instead of gvUserRole.Rows.Count - 2.
Note: For this sample i have used temporary DataTable. For more details refer How to create Temporary Table in ASP.Net using C# and VB.Net.
HTML
<asp:GridView ID="gvDetails" runat="server" AutoGenerateColumns="false" OnDataBound="OnDataBound" DataKeyNames="MenuName">
<Columns>
<asp:TemplateField>
<HeaderTemplate>
<input type="checkbox" id="chkAll" class="header" />
</HeaderTemplate>
<ItemTemplate>
<input type="checkbox" id="chkItem" class="item" />
</ItemTemplate>
<ItemStyle Width="20px" />
</asp:TemplateField>
<asp:BoundField DataField="Id" HeaderText="Id"></asp:BoundField>
<asp:BoundField DataField="GroupName" HeaderText="Role"></asp:BoundField>
<asp:BoundField DataField="MenuName" HeaderText="Details"></asp:BoundField>
</Columns>
</asp:GridView>
Namespace
C#
using System.Data;
VB.Net
Imports System.Data
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
this.BindGridView();
}
}
protected void OnDataBound(object sender, EventArgs e)
{
for (int rowIndex = gvDetails.Rows.Count - 1; rowIndex > 0; rowIndex--)
{
GridViewRow gvRow = gvDetails.Rows[rowIndex];
GridViewRow gvPreviousRow = gvDetails.Rows[rowIndex - 1];
if (gvRow.Cells[2].Text == gvPreviousRow.Cells[2].Text)
{
if (gvPreviousRow.Cells[2].RowSpan == 0)
{
if (gvRow.Cells[2].RowSpan == 0)
{
gvPreviousRow.Cells[2].RowSpan += 2;
}
else
{
gvPreviousRow.Cells[2].RowSpan = gvRow.Cells[2].RowSpan + 1;
}
gvRow.Cells[2].Visible = false;
}
}
}
}
private void BindGridView()
{
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[3] {
new DataColumn("Id"),
new DataColumn("GroupName"),
new DataColumn("MenuName")
});
dt.Rows.Add(1, "Admin", "User");
dt.Rows.Add(2, "Admin", "Company");
dt.Rows.Add(3, "Admin", "User Role");
gvDetails.DataSource = dt;
gvDetails.DataBind();
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
Me.BindGridView()
End If
End Sub
Protected Sub OnDataBound(ByVal sender As Object, ByVal e As EventArgs)
For rowIndex As Integer = gvDetails.Rows.Count - 1 To 0 + 1 Step -1
Dim gvRow As GridViewRow = gvDetails.Rows(rowIndex)
Dim gvPreviousRow As GridViewRow = gvDetails.Rows(rowIndex - 1)
If gvRow.Cells(2).Text = gvPreviousRow.Cells(2).Text Then
If gvPreviousRow.Cells(2).RowSpan = 0 Then
If gvRow.Cells(2).RowSpan = 0 Then
gvPreviousRow.Cells(2).RowSpan += 2
Else
gvPreviousRow.Cells(2).RowSpan = gvRow.Cells(2).RowSpan + 1
End If
gvRow.Cells(2).Visible = False
End If
End If
Next
End Sub
Private Sub BindGridView()
Dim dt As DataTable = New DataTable()
dt.Columns.AddRange(New DataColumn(2) {
New DataColumn("Id"),
New DataColumn("GroupName"),
New DataColumn("MenuName")})
dt.Rows.Add(1, "Admin", "User")
dt.Rows.Add(2, "Admin", "Company")
dt.Rows.Add(3, "Admin", "User Role")
gvDetails.DataSource = dt
gvDetails.DataBind()
End Sub
Screenshot