datagridview合併表頭

2021-05-22 18:29:55 字數 2523 閱讀 6375

1.功能說明:

將連續的多個列合併成乙個新列。

2.不足之處:

不能合併多層。比如下圖這樣的功能是沒有的。

3.使用參考.

在form的建構函式裡寫下如下**

utility.exgridview.isenlarged = false;

在datagridview的cellpaiting事件中寫如下**

utility.exgridview exg = new utility.exgridview();

list colnamecollection=new list();

for (int i = 0; i < 10; i++)

exg.mergeheader(sender, e, colnamecollection, "0-9中獎號碼分布圖");

4.效果截圖

5.原始檔(沒找到新增附件的地方,就貼出**了)

using system;

using system.collections.generic;

using system.text;

using system.windows.forms;

using system.drawing;

namespace utility

if (colnamecollection.contains(colname))

cwidth += datagridview1.columns[colnameitem].width;                           

}                       

} rectangle carea = new rectangle(cleft, ctop, cwidth, cheight);

//2.把區域設定為背景色,沒有列的分線及任何文字。

using (brush backcolorbrush = new solidbrush(e.cellstyle.backcolor))

//3.繪製新列頭的邊框

using (pen gridpen = new pen(datagridview1.gridcolor))

//3.4 下部邊框

e.graphics.drawline(gridpen, cleft, ctop + cheight - 1, cleft + cwidth, ctop + cheight - 1);

//3.5 右部邊框                        

e.graphics.drawline(gridpen, cleft + cwidth - 1, ctop, cleft + cwidth - 1, ctop + cheight);//(ctop+cheight)/2);

} //4.寫文字

if (colnamecollection.indexof(colname) == 0) }

//5 繪製子列背景

int fathercolheight = e.cellbounds.height / 2;//上面一行的高度 

using (brush backcolorbrush = new solidbrush(e.cellstyle.backcolor))

//5.1繪製子列的邊框                   

using (pen gridpen = new pen(datagridview1.gridcolor))

//5.3 下部邊框

e.graphics.drawline(gridpen, cleft, ctop + cheight - 1 + fathercolheight, cleft + cwidth, ctop + cheight - 1 + fathercolheight);

//5.4 右部邊框 

e.graphics.drawline(gridpen, e.cellbounds.x + e.cellbounds.width - 1, e.cellbounds.top + fathercolheight, e.cellbounds.x + e.cellbounds.width - 1, e.cellbounds.top + e.cellbounds.height + fathercolheight);//(ctop+cheight)/2);                    

} //5.5 寫子列的文字

int wstr = (int)(datagridview1.columns[e.columnindex].headertext.length * e.cellstyle.font.sizeinpoints);

int wcell = e.cellbounds.width;

int pleft = (wcell - wstr) / 2;//相對cell左邊框的左座標

using (brush forebrush = new solidbrush(e.cellstyle.forecolor))

#endregion

e.handled = true;

} }

}       

} }

datagridview合併單元格,行合併

windows forms datagridview 沒有提供合併單元格的功能,要實現合併單元格的功能就要在cellpainting事件中使用graphics.drawline和 graphics.drawstring 自己來 畫 下面的 可以對datagridview第1列內容相同的單元格進行合併...

datagridview單元格合併

最近遇到乙個比較棘手的問題那就是對datagridview進行單元格合併的處理,在網上找了很久合併的問題倒是解決了 不過合併後卻無法讓內容居中顯示!經過苦思冥想發現是這個合併的方法有問題,他緊緊是把邊緣線去掉並沒有完成真證意義上的合併!現在把此方法分享給大家,希望各位大蝦能補充補充!private ...

DataGridView 中合併單元格

windows forms datagridview 沒有提供合併單元格的功能,要實現合併單元格的功能就要在cellpainting事件中使用graphics.drawline和 graphics.drawstring 自己來 畫 下面的 可以對datagridview第1列內容相同的單元格進行合併...