Programmatically Change Individual Cell Borders in a WinForms DataGridView

 ̄綄美尐妖づ 提交于 2019-12-13 18:18:28

问题


I need to programmatically change the border of individual cells in a WinForms DataGridView. When searching on the 'net, I found this link (http://bytes.com/groups/net-vb/501128-changing-datagridview-cell-borders-runtime) which is the same thing that I am trying to do; however, there isn't a code example there of the solution, which is

"So you can inherit from the DataGridViewCell class and overrides AdjustCellBorderStyle method to get a customized version of DataGridViewCell. Then you can use this customized DataGridViewCell in your DataGridView. Note: In your customized DataGridViewCell, you should expose a DataGridViewAdvancedBorderStyle public member so that DataGridView code can set this member border style information to the cell. Then in the AdjustCellBorderStyle implementation, you should check the this DataGridViewAdvancedBorderStyle public member and return corresponding DataGridViewAdvancedBorderStyle. Then DataGridView PaintCells can use it to paint your cell.".

I'm having a hard time understanding implementing this solution. Could someone please translate the above into working VB.Net code and provide an example of calling it to change an individual cell's borders?


回答1:


Here is a ready made example which does what you need, just hidden amongst the extra functionality of setting the background colour.

http://www.codeproject.com/KB/grid/hulihui_CustomDataGridVie.aspx

Look for the lines

// must draw border for grid scrolling horizontally 
e.Graphics.DrawRectangle(gridPenColor, rect1);  

That line draws a cells border, so to change an individual cells border change the Event args (CellBackColorEventArgs class) to include whatever properties you want to describe the border. Then in the DrawCellBackColor method draw the border based on these passed in properties (and whatever else you want to draw in the cell)



来源:https://stackoverflow.com/questions/995327/programmatically-change-individual-cell-borders-in-a-winforms-datagridview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!