Hi hafifiw,
Check this example. Now please take its reference and correct your code.
HTML
<asp:GridView ID="dgEventHistory" runat="server" BorderColor="#006600" BorderStyle="Solid" BorderWidth="1px"
Font-Bold="False" Font-Names="verdana" Font-Size="12px" Width="50%" AutoGenerateColumns="False"
OnRowDataBound="dgEventHistory_RowDataBound" OnDataBound="dgEventHistory_DataBound">
<AlternatingRowStyle BackColor="#DDFFDD" BorderColor="#006600" BorderStyle="Solid" BorderWidth="1px"></AlternatingRowStyle>
<Columns>
<asp:BoundField DataField="AOI DATE_Shift" HeaderText="Date" ItemStyle-HorizontalAlign="Center" ItemStyle-Width="6%" />
<asp:BoundField DataField="plate_line" HeaderText="Plate Line" ItemStyle-HorizontalAlign="Center" ItemStyle-Width="3%" />
<asp:BoundField DataField="plate_tank" HeaderText="Plate Tank" ItemStyle-HorizontalAlign="Center" ItemStyle-Width="3%" />
<asp:BoundField DataField="trigger" HeaderText="Trigger" ItemStyle-HorizontalAlign="Center" ItemStyle-Width="1%" />
<asp:BoundField DataField="remark" HeaderText="Remark" ItemStyle-HorizontalAlign="Center" ItemStyle-Width="4%" />
<asp:BoundField DataField="trigger_type" HeaderText="Trigger Type" ItemStyle-HorizontalAlign="Center" ItemStyle-Width="3%" />
<asp:BoundField DataField="flag" Visible="false" HeaderText="Flag" ItemStyle-HorizontalAlign="Center" ItemStyle-Width="3%" />
</Columns>
<HeaderStyle BackColor="#99FFCC" Font-Bold="True" Font-Names="Verdana" ForeColor="#006600"></HeaderStyle>
</asp:GridView>
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
System.Data.DataTable dt = new System.Data.DataTable();
dt.Columns.AddRange(new System.Data.DataColumn[]
{
new System.Data.DataColumn("AOI DATE_Shift"),
new System.Data.DataColumn("plate_line"),
new System.Data.DataColumn("plate_tank"),
new System.Data.DataColumn("trigger"),
new System.Data.DataColumn("remark"),
new System.Data.DataColumn("trigger_type"),
new System.Data.DataColumn("flag")
});
dt.Rows.Add("2020-10-02_Day", 1, "20", "Yes", "1-20AJ19-075", "Trigger", "");
dt.Rows.Add("2020-10-02_Day", 1, "21", "No", "-", "-", "");
dt.Rows.Add("2020-10-02_Day", 1, "22", "No", "-", "-", "");
dt.Rows.Add("2020-10-02_Day", 1, "23", "No", "-", "-", "");
dt.Rows.Add("2020-10-02_Day", 1, "24", "Yes", "1-20AJ19-088", "Fail", "");
dt.Rows.Add("2020-10-02_Day", 1, "25", "Yes", "1-20AJ19-006", "Fail", "");
dt.Rows.Add("2020-10-02_Day", 1, "26", "Yes", "1-20AJ19-045", "Fail", "");
dt.Rows.Add("2020-10-02_Day", 1, "26", "Yes", "1-20AJ19-047", "Fail", "");
dt.Rows.Add("2020-10-02_Day", 1, "27", "Yes", "1-20AJ19-067", "Trigger", "");
dt.Rows.Add("2020-10-02_Day", 1, "28", "No", "-", "-", "");
dt.Rows.Add("2020-10-02_Day", 1, "29", "No", "-", "-", "");
this.dgEventHistory.DataSource = dt;
this.dgEventHistory.DataBind();
}
}
protected void dgEventHistory_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (!e.Row.Cells[4].Text.StartsWith("-"))
{
HyperLink link = new HyperLink()
{
ID = "lnk" + e.Row.RowIndex,
Text = e.Row.Cells[4].Text.ToString(),
NavigateUrl = "#"
};
e.Row.Cells[4].Controls.Add(link);
}
}
}
protected void dgEventHistory_DataBound(object sender, EventArgs e)
{
for (int i = dgEventHistory.Rows.Count - 1; i >= 1; i += -1)
{
GridViewRow row = dgEventHistory.Rows[i];
GridViewRow previousRow = dgEventHistory.Rows[i - 1];
for (int j = 0; j <= row.Cells.Count - 1; j++)
{
if (row.Cells[j].Text == previousRow.Cells[j].Text)
{
if (previousRow.Cells[j].RowSpan == 0)
{
if (row.Cells[j].RowSpan == 0)
{
previousRow.Cells[j].RowSpan += 2;
}
else
{
previousRow.Cells[j].RowSpan = row.Cells[j].RowSpan + 1;
}
row.Cells[j].Visible = false;
}
}
}
}
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
Dim dt As Data.DataTable = New Data.DataTable()
dt.Columns.AddRange(New Data.DataColumn() {
New Data.DataColumn("AOI DATE_Shift"),
New Data.DataColumn("plate_line"),
New Data.DataColumn("plate_tank"),
New Data.DataColumn("trigger"),
New Data.DataColumn("remark"),
New Data.DataColumn("trigger_type"),
New Data.DataColumn("flag")})
dt.Rows.Add("2020-10-02_Day", 1, "20", "Yes", "1-20AJ19-075", "Trigger", "")
dt.Rows.Add("2020-10-02_Day", 1, "21", "No", "-", "-", "")
dt.Rows.Add("2020-10-02_Day", 1, "22", "No", "-", "-", "")
dt.Rows.Add("2020-10-02_Day", 1, "23", "No", "-", "-", "")
dt.Rows.Add("2020-10-02_Day", 1, "24", "Yes", "1-20AJ19-088", "Fail", "")
dt.Rows.Add("2020-10-02_Day", 1, "25", "Yes", "1-20AJ19-006", "Fail", "")
dt.Rows.Add("2020-10-02_Day", 1, "26", "Yes", "1-20AJ19-045", "Fail", "")
dt.Rows.Add("2020-10-02_Day", 1, "26", "Yes", "1-20AJ19-047", "Fail", "")
dt.Rows.Add("2020-10-02_Day", 1, "27", "Yes", "1-20AJ19-067", "Trigger", "")
dt.Rows.Add("2020-10-02_Day", 1, "28", "No", "-", "-", "")
dt.Rows.Add("2020-10-02_Day", 1, "29", "No", "-", "-", "")
Me.dgEventHistory.DataSource = dt
Me.dgEventHistory.DataBind()
End If
End Sub
Protected Sub dgEventHistory_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
If e.Row.RowType = DataControlRowType.DataRow Then
If Not e.Row.Cells(4).Text.StartsWith("-") Then
Dim link As HyperLink = New HyperLink() With {
.ID = "lnk" & e.Row.RowIndex,
.Text = e.Row.Cells(4).Text.ToString(),
.NavigateUrl = "#"
}
e.Row.Cells(4).Controls.Add(link)
End If
End If
End Sub
Protected Sub dgEventHistory_DataBound(ByVal sender As Object, ByVal e As EventArgs)
For i As Integer = dgEventHistory.Rows.Count - 1 To 1 Step -1
Dim row As GridViewRow = dgEventHistory.Rows(i)
Dim previousRow As GridViewRow = dgEventHistory.Rows(i - 1)
For j As Integer = 0 To row.Cells.Count - 1
If row.Cells(j).Text = previousRow.Cells(j).Text Then
If previousRow.Cells(j).RowSpan = 0 Then
If row.Cells(j).RowSpan = 0 Then
previousRow.Cells(j).RowSpan += 2
Else
previousRow.Cells(j).RowSpan = row.Cells(j).RowSpan + 1
End If
row.Cells(j).Visible = False
End If
End If
Next
Next
End Sub
Screenshot