สร้างเงือนไขในการ Merge
private void dataGrid_Paint(object sender, PaintEventArgs e) { bool lbConv; int DivCode; int check = 0; if (dataGrid.Rows.Count > 0) { if (dataGrid.Rows.Count > 0) { int startRow = 0, endRow = 0; DataGridViewRow dvgRowCurr, dvgRowNext; for (int i = 0; i < dataGrid.Rows.Count; i++) { dvgRowCurr = dataGrid.Rows[i]; if ((i + 1) < dataGrid.Rows.Count) { dvgRowNext = dataGrid.Rows[i + 1]; if (dvgRowCurr.Cells["DivCode"].Value.ToString() == dvgRowNext.Cells["DivCode"].Value.ToString()) { lbConv = int.TryParse(dvgRowNext.Cells["DivCode"].Value.ToString(), out DivCode); if (check == 0) { check = DivCode; startRow = i; } if (check != DivCode) { check = DivCode; startRow = i; } endRow = i + 1; if (dvgRowCurr.Cells["DivCode"].Value.ToString() == dataGrid.Rows[endRow+1].Cells["DivCode"].Value.ToString()) { continue; } else { this.MergeCells(startRow, endRow, dataGrid.Columns["DivName"].Index, true); } } } else { //endRow = i; //startRow = i; //this.MergeCells(startRow, endRow, dataGrid.Columns["DivName"].Index, true); } } } } }
ฟังก์ชัน Merge
private void MergeCells(int RowId1, int RowId2, int Column, bool isSelected) { Graphics g = dataGrid.CreateGraphics(); Pen gridPen = new Pen(dataGrid.GridColor); DataGridViewCellStyle styleNew = new DataGridViewCellStyle(); styleNew.Font = new Font(dataGrid.Font.FontFamily,14, FontStyle.Italic); //Cells Rectangles Rectangle CellRectangle1 = dataGrid.GetCellDisplayRectangle(Column, RowId1, true); Rectangle CellRectangle2 = dataGrid.GetCellDisplayRectangle(Column, RowId2, true); int rectHeight = 0; string MergedRows = String.Empty; for (int i = RowId1; i <= RowId2; i++) { rectHeight += dataGrid.GetCellDisplayRectangle(Column, i, false).Height; } Rectangle newCell = new Rectangle(CellRectangle1.X, CellRectangle1.Y, CellRectangle1.Width, rectHeight); g.FillRectangle(new SolidBrush(isSelected ? dataGrid.DefaultCellStyle.SelectionBackColor : dataGrid.DefaultCellStyle.BackColor), newCell); g.DrawRectangle(gridPen, newCell); //g.DrawString(chSeqBoardDataGridViews.Rows[RowId1].Cells[Column].Value.ToString(), chSeqBoardDataGridViews.DefaultCellStyle.Font, new SolidBrush(isSelected ? chSeqBoardDataGridViews.DefaultCellStyle.SelectionForeColor : chSeqBoardDataGridViews.DefaultCellStyle.ForeColor), newCell.X + newCell.Width / 3, newCell.Y + newCell.Height / 3); //g.DrawString(chSeqBoardDataGridViews.Rows[RowId1].Cells[Column].Value.ToString(), styleNew.Font, new SolidBrush(isSelected ? chSeqBoardDataGridViews.DefaultCellStyle.SelectionForeColor : chSeqBoardDataGridViews.DefaultCellStyle.ForeColor), newCell.X + newCell.Width / 3, newCell.Y + newCell.Height / 3); g.DrawString(dataGrid.Rows[RowId1].Cells[Column].Value.ToString(), dataGrid.DefaultCellStyle.Font, new SolidBrush(isSelected ? dataGrid.DefaultCellStyle.SelectionForeColor : dataGrid.DefaultCellStyle.ForeColor), newCell.X + 2, newCell.Y + newCell.Height / 3); // g.DrawString(dataGrid.Rows[RowId1].Cells[Column].Value.ToString(), dataGrid.DefaultCellStyle.Font, new SolidBrush(isSelected ? dataGrid.DefaultCellStyle.SelectionForeColor : dataGrid.DefaultCellStyle.ForeColor), newCell.X + 2, newCell.Y + newCell.Height / 3); }