热门搜索 :
考研考公
您的当前位置:首页正文

DataGridView合并列

来源:伴沃教育
 \"合并单元格的测试\"#region\"合并单元格的测试\"

private int? nextrow = null; private int? nextcol = null; private void dataGridView1_CellFormatting(object System.Windows.Forms.DataGridViewCellFormattingEventArgs e)

sender,

...{

if (this.dataGridView1.Columns[\"description\"].Index == e.ColumnIndex && e.RowIndex >= 0) ...{

if (this.nextcol != null & e.ColumnIndex == this.nextcol) ...{

e.CellStyle.BackColor = Color.LightBlue; this.nextcol = null; }

if (this.nextrow != null & e.RowIndex == nextrow) ...{

e.CellStyle.BackColor = Color.LightPink; this.nextrow = null; }

if (e.RowIndex != this.dataGridView1.RowCount - 1) ...{

if (e.Value.ToString() == this.dataGridView1.Rows[e.RowIndex + 1].Cells[e.ColumnIndex].Value.ToString()) ...{

e.CellStyle.BackColor = Color.LightPink; nextrow = e.RowIndex + 1; } }

}

if (this.dataGridView1.Columns[\"name\"].Index == e.ColumnIndex && e.RowIndex >= 0) ...{ if

(e.Value.ToString()

==

this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex + 1].Value.ToString()) ...{

e.CellStyle.BackColor = Color.LightBlue; nextcol = e.ColumnIndex + 1; } } }

//==========================

//绘制单元格

private void dataGridView1_CellPainting(object sender,

System.Windows.Forms.DataGridViewCellPaintingEventArgs e) ...{

//纵向合并

if (this.dataGridView1.Columns[\"description\"].Index == e.ColumnIndex && e.RowIndex >= 0) ...{

using (

Brush gridBrush SolidBrush(this.dataGridView1.GridColor),

=

new

backColorBrush = new SolidBrush(e.CellStyle.BackColor)) ...{

using (Pen gridLinePen = new Pen(gridBrush)) ...{

// 擦除原单元格背景

e.Graphics.FillRectangle(backColorBrush, e.CellBounds); /**/////绘制线条,这些线条是单元格相互间隔的区分线条, ////因为我们只对列name做处理,所以datagridview自己会处理左侧和上边缘的线条

if (e.RowIndex != this.dataGridView1.RowCount - 1) ...{

if (e.Value.ToString() != this.dataGridView1.Rows[e.RowIndex + 1].Cells[e.ColumnIndex].Value.ToString()) ...{

e.Graphics.DrawLine(gridLinePen,

e.CellBounds.Left, e.CellBounds.Bottom - 1,

e.CellBounds.Right - 1, e.CellBounds.Bottom - 1);//下边缘的线

//绘制值

if (e.Value != null) ...{

e.Graphics.DrawString((String)e.Value, e.CellStyle.Font,

Brushes.Crimson, e.CellBounds.X + 2, e.CellBounds.Y StringFormat.GenericDefault); } } }

+

2,

else

...{

e.Graphics.DrawLine(gridLinePen, e.CellBounds.Left, e.CellBounds.Bottom - 1,

e.CellBounds.Right - 1, e.CellBounds.Bottom - 1);//下边缘的线

//绘制值

if (e.Value != null) ...{

e.Graphics.DrawString((String)e.Value, e.CellStyle.Font,

Brushes.Crimson, e.CellBounds.X + 2, e.CellBounds.Y StringFormat.GenericDefault); } }

//右侧的线

e.Graphics.DrawLine(gridLinePen, e.CellBounds.Right - 1, e.CellBounds.Top, e.CellBounds.Right - 1, e.CellBounds.Bottom - 1);

e.Handled = true; } } }

//横向合并

if (this.dataGridView1.Columns[\"name\"].Index == e.ColumnIndex && e.RowIndex >= 0) ...{

using (

Brush gridBrush SolidBrush(this.dataGridView1.GridColor),

=

new

+

2,

backColorBrush = new SolidBrush(e.CellStyle.BackColor)) ...{

using (Pen gridLinePen = new Pen(gridBrush)) ...{

// 擦除原单元格背景

e.Graphics.FillRectangle(backColorBrush, e.CellBounds);

if (e.Value.ToString() != this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex + 1].Value.ToString()) ...{

//右侧的线

e.Graphics.DrawLine(gridLinePen, e.CellBounds.Right - 1, e.CellBounds.Top,

e.CellBounds.Right - 1, e.CellBounds.Bottom - 1); //绘制值

if (e.Value != null)

...{

e.Graphics.DrawString((String)e.Value, e.CellStyle.Font,

Brushes.Crimson, e.CellBounds.X + 2, e.CellBounds.Y StringFormat.GenericDefault); } }

//下边缘的线

e.Graphics.DrawLine(gridLinePen, e.CellBounds.Bottom - 1,

e.CellBounds.Left,

-

1,

+

2,

e.CellBounds.Right e.CellBounds.Bottom - 1);

e.Handled = true; } }

}

}

#endregion

因篇幅问题不能全部显示,请点此查看更多更全内容

Top