Hi smile,
You need to use the CellPainting and the CellFormatting event and set the BorderStyle and the cell value.
Plese refer below sample.
Code
C#
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[4]{new DataColumn ("Id"),
new DataColumn ("Test"),
new DataColumn ("Subject"),
new DataColumn ("Question") });
dt.Rows.Add(1, "Test1", "English", "Words William is a poet");
dt.Rows.Add(2, "Test1", "English", "Dafodial written by?");
dt.Rows.Add(3, "Test2", "Math", "a+b = c");
dt.Rows.Add(4, "Test2", "Math", "b+c= a");
dt.Rows.Add(5, "Test2", "Science", "Science is practical activity");
dt.Rows.Add(6, "Test3", "Computer Science", "Keyboard is an input device");
dt.Rows.Add(7, "Test3", "Computer Science", "Printer is an output device");
dgvStudents.DataSource = dt;
dgvStudents.AllowUserToAddRows = false;
}
private void OnCellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
e.AdvancedBorderStyle.Bottom = DataGridViewAdvancedCellBorderStyle.None;
if (e.RowIndex < 1 || e.ColumnIndex < 0)
{
return;
}
if (this.IsCellValueSame(e.ColumnIndex, e.RowIndex))
{
e.AdvancedBorderStyle.Top = DataGridViewAdvancedCellBorderStyle.None;
}
else
{
e.AdvancedBorderStyle.Top = dgvStudents.AdvancedCellBorderStyle.Top;
}
}
private void OnCellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
if (e.RowIndex > 0)
{
if (this.IsCellValueSame(e.ColumnIndex, e.RowIndex))
{
e.Value = "";
}
}
}
protected bool IsCellValueSame(int column, int row)
{
DataGridViewCell cell1 = dgvStudents[column, row];
DataGridViewCell cell2 = dgvStudents[column, row - 1];
if (cell1.Value == null || cell2.Value == null)
{
return false;
}
return cell1.Value.ToString() == cell2.Value.ToString();
}
VB.Net
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Dim dt As DataTable = New DataTable()
dt.Columns.AddRange(New DataColumn(3) {New DataColumn("Id"),
New DataColumn("Test"),
New DataColumn("Subject"),
New DataColumn("Question")})
dt.Rows.Add(1, "Test1", "English", "Words William is a poet")
dt.Rows.Add(2, "Test1", "English", "Dafodial written by?")
dt.Rows.Add(3, "Test2", "Math", "a+b = c")
dt.Rows.Add(4, "Test2", "Math", "b+c= a")
dt.Rows.Add(5, "Test2", "Science", "Science is practical activity")
dt.Rows.Add(6, "Test3", "Computer Science", "Keyboard is an input device")
dt.Rows.Add(7, "Test3", "Computer Science", "Printer is an output device")
dgvStudents.DataSource = dt
dgvStudents.AllowUserToAddRows = False
End Sub
Private Sub OnCellPainting(ByVal sender As Object, ByVal e As DataGridViewCellPaintingEventArgs) Handles dgvStudents.CellPainting
e.AdvancedBorderStyle.Bottom = DataGridViewAdvancedCellBorderStyle.None
If e.RowIndex < 1 OrElse e.ColumnIndex < 0 Then
Return
End If
If Me.IsCellValueSame(e.ColumnIndex, e.RowIndex) Then
e.AdvancedBorderStyle.Top = DataGridViewAdvancedCellBorderStyle.None
Else
e.AdvancedBorderStyle.Top = dgvStudents.AdvancedCellBorderStyle.Top
End If
End Sub
Private Sub OnCellFormatting(ByVal sender As Object, ByVal e As DataGridViewCellFormattingEventArgs) Handles dgvStudents.CellFormatting
If e.RowIndex > 0 Then
If Me.IsCellValueSame(e.ColumnIndex, e.RowIndex) Then
e.Value = ""
End If
End If
End Sub
Protected Function IsCellValueSame(ByVal column As Integer, ByVal row As Integer) As Boolean
Dim cell1 As DataGridViewCell = dgvStudents(column, row)
Dim cell2 As DataGridViewCell = dgvStudents(column, row - 1)
If cell1.Value Is Nothing OrElse cell2.Value Is Nothing Then
Return False
End If
Return cell1.Value.ToString() = cell2.Value.ToString()
End Function
Screenshot