Visual cue between groupings in DataGridView

I want to give a visual cue to distinguish groups of rows with the same CompanyID in a DataGridView. Is it possible to insert a separator on a break in CompanyID? Or would alternating background color be better/easier? What is the best way to achieve? Thanks.
[259 byte] By [AlC.] at [2007-12-23]
# 1
Take a look at the datagridviewadvancedborderstyle class
KenTucker at 2007-8-30 > top of Msdn Tech,Windows Forms,Windows Forms Data Controls and Databinding...
# 2
Thanks Ken. I took a look at look at the datagridviewadvancedborderstyle class. I see that the CustomRow has a property called isFirstDisplayedRow that can be used to change the border style, but I did not see a way to create a visual separation between groups of rows based on a change in a key value such as ProductID.
AlC. at 2007-8-30 > top of Msdn Tech,Windows Forms,Windows Forms Data Controls and Databinding...
# 3

Maybe this will work for you.

VB 2005

Imports System.Data.SqlClient

Public Class Form1

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim dt As New DataTable
Dim strConn As String
Dim da As SqlDataAdapter
Dim conn As SqlConnection

strConn = "Server = .;Database = NorthWind; Integrated Security = SSPI;"
conn = New SqlConnection(strConn)

da = New SqlDataAdapter("Select CategoryID, ProductName, UnitPrice from Products Order By CategoryID", conn)
da.Fill(dt)
DataGridView1.DataSource = dt
DataGridView1.Rows(0).DefaultCellStyle.BackColor = Color.SkyBlue
End Sub

Private Sub DataGridView1_CellFormatting(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellFormattingEventArgs) Handles DataGridView1.CellFormatting
Try
If e.RowIndex > 0 And e.ColumnIndex = 0 Then
If DataGridView1.Item(0, e.RowIndex - 1).Value = e.Value Then
e.Value = ""
ElseIf e.RowIndex < DataGridView1.Rows.Count - 1 Then
DataGridView1.Rows(e.RowIndex).DefaultCellStyle.BackColor = Color.SkyBlue
End If
End If
Catch ex As Exception

End Try
End Sub
End Class

C#

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace CSDGVGrouping
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
DataTable dt = new DataTable();
String strConn = "Server = .;Database = NorthWind; Integrated Security = SSPI;";
SqlConnection conn = new SqlConnection(strConn);
SqlDataAdapter da = new SqlDataAdapter("Select CategoryID, ProductName, UnitPrice from Products Order By CategoryID", conn);
da.Fill(dt);
dataGridView1.DataSource = dt;
dataGridView1.Rows[0].DefaultCellStyle.BackColor = Color.SkyBlue;
}

private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
try
{
if (e.RowIndex > 0 && e.ColumnIndex == 0 && e.RowIndex < dataGridView1.Rows.Count)
{
System.Diagnostics.Trace.WriteLine(e.Value + " " + dataGridView1[0, e.RowIndex - 1].Value);
if (dataGridView1[0, e.RowIndex - 1].Value.ToString() == e.Value.ToString())
{
e.Value = "";
}
else
{
if (e.RowIndex < dataGridView1.Rows.Count - 1)
{
dataGridView1.Rows[e.RowIndex].DefaultCellStyle.BackColor = Color.SkyBlue;
}
}
}
}
catch
{
}

}
}
}

KenTucker at 2007-8-30 > top of Msdn Tech,Windows Forms,Windows Forms Data Controls and Databinding...
# 4
Thanks Ken for posting that code. I haven't had a chance to try it yet due to a higher priority project. Hope to get some time soon.
AlC. at 2007-8-30 > top of Msdn Tech,Windows Forms,Windows Forms Data Controls and Databinding...