最近發現這方面在社區內提問的不少,發個源代碼,嘿嘿
private void Form1_Load(object sender, EventArgs e)
{
DataTable dt = new DataTable();
dt.Columns.Add("A");
dt.Columns.Add("B");
dt.Columns.Add("C");
dt.Columns.Add("D");
dt.Rows.Add(new object[] { "A1", "B1", "C1", "D1" });
dt.Rows.Add(new object[] { "A2", "B2", "C2", "D2" });
dt.Rows.Add(new object[] { "A3", "B3", "C3", "D3" });
this.dataGridVIEw1.DataSource = dt;
}
private void dataGridView1_CellPainting(object sender, DataGridVIEwCellPaintingEventArgs e)
{
if (e.RowIndex < 0 || e.RowIndex >= this.dataGridVIEw1.Rows.Count - 1 || e.ColumnIndex == -1)
{
return;
}
// 假定需要將C列值為C3的單元格與前一單元格合並
if (e.ColumnIndex == 1 && this.dataGridVIEw1[e.RowIndex, e.ColumnIndex + 1].Value.ToString() == "C3")