datagridview單元格合併

2021-05-22 17:25:28 字數 2096 閱讀 9033

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

private sub datagridview1_cellpainting(byval sender as system.object, byval e as datagridviewcellpaintingeventargs) handles datagridview1.cellpainting

' 合併

if e.columnindex >= 0 andalso e.rowindex <> -1 then

using gridbrush as brush = new solidbrush(me.datagridview1.gridcolor), backcolorbrush as brush = new solidbrush(e.cellstyle.backcolor)

using gridlinepen as new pen(gridbrush)

' 清除單元格

e.graphics.fillrectangle(backcolorbrush, e.cellbounds)

' 畫 grid 邊線(僅畫單元格的底邊線和右邊線)

' 如果下一行和當前行的資料不同,則在當前的單元格畫一條底邊線

if e.rowindex < datagridview1.rows.count - 1 andalso datagridview1.rows(e.rowindex + 1).cells(e.columnindex).value.tostring() <> e.value.tostring() then

e.graphics.drawline(gridlinepen, e.cellbounds.left, e.cellbounds.bottom - 1, e.cellbounds.right - 1, e.cellbounds.bottom - 1)

wx = 0

else

wx = wx + e.cellbounds.width

end if

' 畫右邊線

if e.columnindex < datagridview1.columns.count - 1 andalso datagridview1.rows(e.rowindex).cells(e.columnindex + 1).value.tostring() <> e.value.tostring() then

e.graphics.drawline(gridlinepen, e.cellbounds.right - 1, e.cellbounds.top, e.cellbounds.right - 1, e.cellbounds.bottom)

hy = 0

else

hy = hy + e.cellbounds.height

end if

' 畫(填寫)單元格內容,相同的內容的單元格只填寫第乙個

if e.value isnot nothing then

if (e.rowindex > 0 andalso datagridview1.rows(e.rowindex - 1).cells(e.columnindex).value.tostring() = e.value.tostring()) or (e.columnindex > 0 andalso datagridview1.rows(e.rowindex).cells(e.columnindex - 1).value.tostring() = e.value.tostring) then

else

e.graphics.drawstring(directcast(e.value, [string]), e.cellstyle.font, brushes.black, e.cellbounds.x + 50, e.cellbounds.y + 5, stringformat.genericdefault)

end if

end if

e.handled = true

end using

end using

end if

end sub

DataGridView 單元格驗證

定義單元格驗證 要求 驗證錯誤後焦點不離開。實現 單元格的驗證可以使用dgv details cellvalidating事件。驗證不通過時呼叫e.cancel true 終止事件鏈,單元格將保持編輯狀態。呼叫dgv details.canceledit 可以使單元格的內容會滾到修改前的值。使用sy...

DataGridView單元格美化

region 重繪column row int rowheadwidth 41 重繪column row private void gdvpersoninfo cellpainting object sender,datagridviewcellpaintingeventargs e else if...

DataGridView單元格驗證

驗證這個問題,本以為可以用canceledit 輕鬆搞定。誰知道沒那麼容易。據說函式執行順序是 自 下面列出validation,enter le e和begin end這些事件的順序 當editmode為editonenter時 當從乙個單元格移動至另一單元格 在同一行內 1 cell le e ...