My DGV has row BackColors set to various colours based on business logic. When a user selects the row the colour changes to the selected row BackColor thus obscuring the pre-set colour. I would like to preserve the original colour when a row is selected and indicate selection with (perhaps) a bold border around the selected row. Is this possible? Is there an easy alternative I'm missing?
我的DGV根据业务逻辑将行BackColors设置为各种颜色。当用户选择该行时,颜色变为所选择的行BackColor,从而模糊了预设颜色。我想在选择行时保留原始颜色,并指示选择行(可能)围绕所选行的粗体边框。这可能吗?我有一个简单的替代方案吗?
EDIT To make it clear, this is a WinForms app.
编辑为了说清楚,这是一个WinForms应用程序。
3 个解决方案
#1
Another possibility is to set the selection color to be a darker shade of the normal back color. This would be much simpler than having to reimplement the drawing of borders of selected rows.
另一种可能性是将选择颜色设置为正常背面颜色的较暗色调。这比必须重新实现所选行的边界绘制要简单得多。
So when you add a row with, say, a yellow background
因此,当您添加一行时,例如黄色背景
Dim backColor as Color = Color.Yellow
row.DefaultCellStyle.BackColor = backColor
row.DefaultCellStyle.SelectionBackColor = Color.FromArgb(backColor.R * 3 / 4, backColor.G * 3 / 4, backColor.B * 3 / 4)
Multiplying each color component by the same number < 1 serves to darken the color, you could play around with different values here to see what pleases you the most aesthetically.
将每个颜色分量乘以相同的数字<1可以使颜色变暗,您可以在这里使用不同的值来看看最美观的东西。
#2
In order to change the default behavior of the DataGridView you typically need to derive a child class based off the DataGridView class. I had to do this when I wanted to change the default behavior of the DataGridView's table cells as far as input goes, ie. pressing the up and down arrow, enter key, that sort of stuff.
为了更改DataGridView的默认行为,通常需要基于DataGridView类派生子类。当我想要改变DataGridView的表格单元格的默认行为时,我必须这样做,即输入。按向上和向下箭头,输入键,那种东西。
What you're asking seems simliar to creating a custom column, I believe you should follow the same steps. However, I'm not sure how you could change the background color.
你问的问题似乎与创建自定义列相似,我相信你应该遵循相同的步骤。但是,我不知道你怎么能改变背景颜色。
I would suggest you start there. In fact, when you want to do more with the datagridview than Microsoft intended, I've found you need to do this.
我建议你从那里开始。事实上,当你想要使用datagridview做更多的事情时,我发现你需要这样做。
I would like to include a few links:
我想包括几个链接:
MSDN on creating custom columns for the DataGridView
MSDN为DataGridView创建自定义列
Blog about creating custom input behavior for DataGridView
有关为DataGridView创建自定义输入行为的博客
I know this may take some time and you were probably hoping for a quick fix, but learning the above can help you with other projects as well.
我知道这可能需要一些时间,你可能希望快速修复,但学习上述内容也可以帮助你完成其他项目。
I may be wrong, there may be an easier way to do what you're saying.
我可能错了,可能有一种更简单的方法来做你所说的。
Also, try googling "datagridview custom behavior".
此外,尝试谷歌搜索“datagridview自定义行为”。
I believe you are talking about a windows form application and not a web application, correct?
我相信你在谈论Windows窗体应用程序而不是Web应用程序,对吗?
#3
You can use
您可以使用
<SelectedRowStyle CssClass="MyCssClass" />
where you'd create a CSS class to define a table row with your required border style. Or you can use in-line border styling with
在哪里创建一个CSS类来定义具有所需边框样式的表行。或者您可以使用内嵌边框样式
<SelectedRowStyle BorderStyle="Solid" BorderWidth="1" BorderColor="Black" />
#1
Another possibility is to set the selection color to be a darker shade of the normal back color. This would be much simpler than having to reimplement the drawing of borders of selected rows.
另一种可能性是将选择颜色设置为正常背面颜色的较暗色调。这比必须重新实现所选行的边界绘制要简单得多。
So when you add a row with, say, a yellow background
因此,当您添加一行时,例如黄色背景
Dim backColor as Color = Color.Yellow
row.DefaultCellStyle.BackColor = backColor
row.DefaultCellStyle.SelectionBackColor = Color.FromArgb(backColor.R * 3 / 4, backColor.G * 3 / 4, backColor.B * 3 / 4)
Multiplying each color component by the same number < 1 serves to darken the color, you could play around with different values here to see what pleases you the most aesthetically.
将每个颜色分量乘以相同的数字<1可以使颜色变暗,您可以在这里使用不同的值来看看最美观的东西。
#2
In order to change the default behavior of the DataGridView you typically need to derive a child class based off the DataGridView class. I had to do this when I wanted to change the default behavior of the DataGridView's table cells as far as input goes, ie. pressing the up and down arrow, enter key, that sort of stuff.
为了更改DataGridView的默认行为,通常需要基于DataGridView类派生子类。当我想要改变DataGridView的表格单元格的默认行为时,我必须这样做,即输入。按向上和向下箭头,输入键,那种东西。
What you're asking seems simliar to creating a custom column, I believe you should follow the same steps. However, I'm not sure how you could change the background color.
你问的问题似乎与创建自定义列相似,我相信你应该遵循相同的步骤。但是,我不知道你怎么能改变背景颜色。
I would suggest you start there. In fact, when you want to do more with the datagridview than Microsoft intended, I've found you need to do this.
我建议你从那里开始。事实上,当你想要使用datagridview做更多的事情时,我发现你需要这样做。
I would like to include a few links:
我想包括几个链接:
MSDN on creating custom columns for the DataGridView
MSDN为DataGridView创建自定义列
Blog about creating custom input behavior for DataGridView
有关为DataGridView创建自定义输入行为的博客
I know this may take some time and you were probably hoping for a quick fix, but learning the above can help you with other projects as well.
我知道这可能需要一些时间,你可能希望快速修复,但学习上述内容也可以帮助你完成其他项目。
I may be wrong, there may be an easier way to do what you're saying.
我可能错了,可能有一种更简单的方法来做你所说的。
Also, try googling "datagridview custom behavior".
此外,尝试谷歌搜索“datagridview自定义行为”。
I believe you are talking about a windows form application and not a web application, correct?
我相信你在谈论Windows窗体应用程序而不是Web应用程序,对吗?
#3
You can use
您可以使用
<SelectedRowStyle CssClass="MyCssClass" />
where you'd create a CSS class to define a table row with your required border style. Or you can use in-line border styling with
在哪里创建一个CSS类来定义具有所需边框样式的表行。或者您可以使用内嵌边框样式
<SelectedRowStyle BorderStyle="Solid" BorderWidth="1" BorderColor="Black" />