以编程方式更改WinForms DataGridView中的单个单元格边框

时间:2021-05-31 22:22:14

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

我需要以编程方式更改WinForms DataGridView中单个单元格的边框。在'网上搜索时,我发现了这个链接(http://bytes.com/groups/net-vb/501128-changing-datagridview-cell-borders-runtime),这与我想做的事情是一样的;但是,没有解决方案的代码示例,即

"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.".

“所以你可以从DataGridViewCell类继承并覆盖AdjustCellBorderStyle方法来获得DataGridViewCell的自定义版本。然后你可以在你的DataGridView中使用这个自定义的DataGridViewCell。注意:在你的自定义DataGridViewCell中,你应该公开一个DataGridViewAdvancedBorderStyle公共成员,以便DataGridView代码可以将此成员边框样式信息设置为单元格。然后在AdjustCellBorderStyle实现中,您应该检查此DataGridViewAdvancedBorderStyle公共成员并返回相应的DataGridViewAdvancedBorderStyle。然后DataGridView PaintCells可以使用它来绘制您的单元格。“

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?

我很难理解实施这个解决方案。有人可以将上述内容翻译成工作的VB.Net代码并提供一个调用它来更改单个单元格边框的示例吗?

1 个解决方案

#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)

该行绘制单元格边框,因此要更改单个单元格边框,请更改Event args(CellBackColorEventArgs类)以包含要描述边框的任何属性。然后在DrawCellBackColor方法中根据传入的属性(以及您想要在单元格中绘制的任何其他内容)绘制边框

#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)

该行绘制单元格边框,因此要更改单个单元格边框,请更改Event args(CellBackColorEventArgs类)以包含要描述边框的任何属性。然后在DrawCellBackColor方法中根据传入的属性(以及您想要在单元格中绘制的任何其他内容)绘制边框