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
{
}
}
}
}