Windows Forms DataGridView 中合并单元格
生活随笔
收集整理的這篇文章主要介紹了
Windows Forms DataGridView 中合并单元格
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
Windows Forms DataGridView 沒(méi)有提供合并單元格的功能,要實(shí)現(xiàn)合并單元格的功能就要在CellPainting事件中使用Graphics.DrawLine和 Graphics.DrawString 自己來(lái)“畫”。
下面的代碼可以對(duì)DataGridView第1列內(nèi)容相同的單元格進(jìn)行合并:
????????private?void?dataGridView1_CellPainting(object?sender,?DataGridViewCellPaintingEventArgs?e)
????????{
????????????//?對(duì)第1列相同單元格進(jìn)行合并
????????????if?(e.ColumnIndex?==?0?&&?e.RowIndex?!=?-1)
????????????{
????????????????using
????????????????????(
????????????????????Brush?gridBrush?=?new?SolidBrush(this.dataGridView1.GridColor),
????????????????????backColorBrush?=?new?SolidBrush(e.CellStyle.BackColor)
????????????????????)
????????????????{
????????????????????using?(Pen?gridLinePen?=?new?Pen(gridBrush))
????????????????????{
????????????????????????//?清除單元格
????????????????????????e.Graphics.FillRectangle(backColorBrush,?e.CellBounds);
????????????????????????//?畫?Grid?邊線(僅畫單元格的底邊線和右邊線)
????????????????????????//???如果下一行和當(dāng)前行的數(shù)據(jù)不同,則在當(dāng)前的單元格畫一條底邊線
????????????????????????if?(e.RowIndex?<?dataGridView1.Rows.Count?-?1?&&
????????????????????????dataGridView1.Rows[e.RowIndex?+?1].Cells[e.ColumnIndex].Value.ToString()?!=?
????????????????????????e.Value.ToString())
????????????????????????????e.Graphics.DrawLine(gridLinePen,?e.CellBounds.Left,
????????????????????????????e.CellBounds.Bottom?-?1,?e.CellBounds.Right?-?1,
????????????????????????????e.CellBounds.Bottom?-?1);
????????????????????????//?畫右邊線
????????????????????????e.Graphics.DrawLine(gridLinePen,?e.CellBounds.Right?-?1,
????????????????????????????e.CellBounds.Top,?e.CellBounds.Right?-?1,
????????????????????????????e.CellBounds.Bottom);
????????????????????????//?畫(填寫)單元格內(nèi)容,相同的內(nèi)容的單元格只填寫第一個(gè)
????????????????????????if?(e.Value?!=?null)
????????????????????????{
????????????????????????????if?(e.RowIndex?>?0?&&
????????????????????????????dataGridView1.Rows[e.RowIndex?-?1].Cells[e.ColumnIndex].Value.ToString()?==?
??????????????????????????? e.Value.ToString())
????????????????????????????{?}
????????????????????????????else
????????????????????????????{
????????????????????????????????e.Graphics.DrawString((String)e.Value,?e.CellStyle.Font,
????????????????????????????????????Brushes.Black,?e.CellBounds.X?+?2,
????????????????????????????????????e.CellBounds.Y?+?5,?StringFormat.GenericDefault);
????????????????????????????}
????????????????????????}
????????????????????????e.Handled?=?true;
????????????????????}
????????????????}
????????????}
????????} 本文地址:http://www.cnblogs.com/anjou/archive/2007/01/08/615056.html
下面的代碼可以對(duì)DataGridView第1列內(nèi)容相同的單元格進(jìn)行合并:
????????private?void?dataGridView1_CellPainting(object?sender,?DataGridViewCellPaintingEventArgs?e)
????????{
????????????//?對(duì)第1列相同單元格進(jìn)行合并
????????????if?(e.ColumnIndex?==?0?&&?e.RowIndex?!=?-1)
????????????{
????????????????using
????????????????????(
????????????????????Brush?gridBrush?=?new?SolidBrush(this.dataGridView1.GridColor),
????????????????????backColorBrush?=?new?SolidBrush(e.CellStyle.BackColor)
????????????????????)
????????????????{
????????????????????using?(Pen?gridLinePen?=?new?Pen(gridBrush))
????????????????????{
????????????????????????//?清除單元格
????????????????????????e.Graphics.FillRectangle(backColorBrush,?e.CellBounds);
????????????????????????//?畫?Grid?邊線(僅畫單元格的底邊線和右邊線)
????????????????????????//???如果下一行和當(dāng)前行的數(shù)據(jù)不同,則在當(dāng)前的單元格畫一條底邊線
????????????????????????if?(e.RowIndex?<?dataGridView1.Rows.Count?-?1?&&
????????????????????????dataGridView1.Rows[e.RowIndex?+?1].Cells[e.ColumnIndex].Value.ToString()?!=?
????????????????????????e.Value.ToString())
????????????????????????????e.Graphics.DrawLine(gridLinePen,?e.CellBounds.Left,
????????????????????????????e.CellBounds.Bottom?-?1,?e.CellBounds.Right?-?1,
????????????????????????????e.CellBounds.Bottom?-?1);
????????????????????????//?畫右邊線
????????????????????????e.Graphics.DrawLine(gridLinePen,?e.CellBounds.Right?-?1,
????????????????????????????e.CellBounds.Top,?e.CellBounds.Right?-?1,
????????????????????????????e.CellBounds.Bottom);
????????????????????????//?畫(填寫)單元格內(nèi)容,相同的內(nèi)容的單元格只填寫第一個(gè)
????????????????????????if?(e.Value?!=?null)
????????????????????????{
????????????????????????????if?(e.RowIndex?>?0?&&
????????????????????????????dataGridView1.Rows[e.RowIndex?-?1].Cells[e.ColumnIndex].Value.ToString()?==?
??????????????????????????? e.Value.ToString())
????????????????????????????{?}
????????????????????????????else
????????????????????????????{
????????????????????????????????e.Graphics.DrawString((String)e.Value,?e.CellStyle.Font,
????????????????????????????????????Brushes.Black,?e.CellBounds.X?+?2,
????????????????????????????????????e.CellBounds.Y?+?5,?StringFormat.GenericDefault);
????????????????????????????}
????????????????????????}
????????????????????????e.Handled?=?true;
????????????????????}
????????????????}
????????????}
????????} 本文地址:http://www.cnblogs.com/anjou/archive/2007/01/08/615056.html
總結(jié)
以上是生活随笔為你收集整理的Windows Forms DataGridView 中合并单元格的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 得到 yyyy/mm/dd 格式时间
- 下一篇: 调试js 试用火狐的firebug