DataGridView,向每个行添加唯一的渐变 [英] DataGridView, add unique gradient to each Row

查看:150
本文介绍了DataGridView,向每个行添加唯一的渐变的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我喜欢在datagridview的每一行中添加渐变笔刷.我喜欢根据行中的某些单元格值分别更改每行的渐变颜色.

我知道如何为每行,单元格,行,列等添加纯色背景色,但是我不知道如何为每行单独添加渐变笔刷.

我需要用值填充datagridview,datagridview与任何数据库或其他数据源断开连接.填充datagridview后,我要遍历每一行,找到我的键值,确定渐变色,然后将渐变添加到该行.

我喜欢做与现在使用背景色相似的操作:

I like to add a gradient brush to each row of my datagridview. I like to change the gradient colors for each row independently depending of some cell values in the row.

I know how to add a solid background color to each, cell, row, column etc. but I do not know how to add a gradient brush to each row individualy.

I need to fill the datagridview with values, the datagridview is disconnected from any database or other data source. After filling the datagridview, I want to step trough each row, find my key values, decide the gradient colors and then add the gradient to the row.

I like to do something similar to what I now do with a background color:

For Each R As DataGridViewRow In DGV.Rows
    Dim CS As New DataGridViewCellStyle
    Select Case i
         Case 1 : CS.BackColor = Color.Yellow
         Case 2 : CS.BackColor = Color.Blue
    End Select
    R.DefaultCellStyle = CS
Next




这可能吗?那你该怎么做?
我正在使用VB.NET和VS 2008和2010

彼得·史威纳森

[修改:将代码标记更改为pre]




Is this possible? And How do you do this?
I am using VB.NET and VS 2008 and 2010

Peter Schwennesen

[Modified: changed code tag to pre]

推荐答案

您可能想覆盖RowPrePaint事件.在 DataGridView .. :: .. RowPrePaint事件 [ ^ ]有关如何在选择了渐变行背景的情况下对其进行绘制.您只需要对其进行修改即可.

[更新]
伙计,花了我永远的时间才真正弄清楚如何实现这一点.我要做的就是将RowTemplate的DefaultCellStyle的BackColor设置为Transparent以使其起作用,但是花了我一段时间才弄清楚了.

无论如何,我不确定您的意思是绘画不正确.每当您调整DataGridView的大小时,它都会重新绘制.我只是通过调整表单的大小来调整DGV的大小来验证了这一点,并且在调试器窗口中将RowPrePaint打印为"RowPrePaint".

不过,我确实发现您的代码存在一些问题.主要是,当您创建一个新矩形时,您要在顶部,左侧位置以及高度和宽度中传递.您改为在顶部,左侧和底部,右侧传递.使用您的代码,它在可用的行下方绘制.

更改为:
You probably want to override the RowPrePaint event. There is sample code at DataGridView..::.RowPrePaint Event[^] on how to paint a gradient row background if the row is selected. You would just need to modify it to your needs.

[Update]
Man, it took me forever to actually figure out how to implement that. All I had to do was set the RowTemplate''s DefaultCellStyle''s BackColor to Transparent to make it work, but it took me a while to figure that out.

Anyway, I''m not sure what you mean by it''s not painting correctly. Whenever you resize the DataGridView, it repaints. I verified this by simply having the form resize the DGV if I resized the form, and I had the RowPrePaint print "RowPrePaint" to the debugger window.

I do see some problems with your code, though. Primarily that when you create a New Rectangle, you want to pass in the top, left location, and the height and width. You instead pass in the top, left and bottom, right. With your code, it was painting below the rows available.

Change it to:
Dim P1 As New System.Drawing.Point(New System.Drawing.Point(Left, Top))
Dim P2 As New System.Drawing.Point(New System.Drawing.Point(Right - Left, Bottom - Top))



我看到的另一个问题是,如果DGV比列宽,则e.RowBounds.Right会超出列.您可以使用GetColumnDisplayRectangle修复该问题.更改为:



The other problem I see is that e.RowBounds.Right extends beyond the columns if the DGV is wider than the columns are. You can fix that using the GetColumnDisplayRectangle. Change to:

Dim Top As Integer = e.RowBounds.Top
Dim Bottom As Integer = e.RowBounds.Bottom
Dim lastColumnRect As Rectangle = _
        DataGridView1.GetColumnDisplayRectangle(DataGridView1.ColumnCount - 1, False)
Dim Right As Integer = lastColumnPt.X + lastColumnPt.Width
Dim Left As Integer = e.RowBounds.Left


进行这些更改后,它只会在实际的行范围内绘制.

有趣的是,当您移动任何滚动条时,它也会重新绘制.因此,您可以使用GetColumnDisplayRectangle迭代遍历各列,并将最后一个输入设置为true,以仅获取实际在显示中的列,并且可以将矩形设置为该列.


With those changes, it will only draw within the actual row bounds.

It''s also interesting to note that when you move any of the scrollbars, it also repaints. So you could go iteratively through the columns using the GetColumnDisplayRectangle and set the last input to true to only get the columns that are actually in the display and you could set the rectangle to that.


谢谢寻找答案.我已尽力制作出似乎可以完成工作的以下内容:

Thanks for the answer. I have mannaged to produce the following that seems to do the work:

Sub DGV_RowPrePaint(ByVal sender As Object, ByVal e As DataGridViewRowPrePaintEventArgs) Handles DGV.RowPrePaint<br />
<br />
        Dim Top As Integer = e.RowBounds.Top<br />
        Dim Bottom As Integer = e.RowBounds.Bottom<br />
        Dim Right As Integer = e.RowBounds.Right<br />
        Dim Left As Integer = e.RowBounds.Left<br />
<br />
        Dim P1 As New System.Drawing.Point(New System.Drawing.Point(Left, Top))<br />
        Dim P2 As New System.Drawing.Point(New System.Drawing.Point(Right, Bottom))<br />
        Dim C1 As System.Drawing.Color = Color.Azure<br />
        Dim C2 As System.Drawing.Color = Color.Chocolate<br />
<br />
        Dim RegType As Integer = DGV.Rows(e.RowIndex).Cells("RegType").Value<br />
<br />
        Select Case RegType<br />
            Case 0<br />
                C1 = Color.Blue<br />
                C2 = Color.LightSeaGreen<br />
            Case 1<br />
                C1 = Color.LightSalmon<br />
                C2 = Color.Indigo<br />
            Case 2<br />
                C1 = Color.LightSkyBlue<br />
                C2 = Color.Khaki<br />
            Case 3<br />
                C1 = Color.LightYellow<br />
                C2 = Color.Lavender<br />
        End Select<br />
<br />
        Dim RA As New Rectangle(P1, P2)<br />
        Dim BB As New System.Drawing.Drawing2D.LinearGradientBrush(RA, C1, C2, System.Drawing.Drawing2D.LinearGradientMode.Horizontal)<br />
        Try<br />
            e.Graphics.FillRectangle(BB, RA)<br />
        Finally<br />
            BB.Dispose()<br />
        End Try<br />
<br />
    End Sub<br />




但.调整表单大小时,绘画未正确完成,渐变未超过datagridview的孔宽度.
有什么办法可以使表单repaintet(和datagridview)??

彼得·史威纳森(Peter Schwennesen)




But. the painting is not properly done, the gradient is not over the hole width of the datagridview, when resizing the form.
Are there some way to have the form repaintet (and the datagridview) ??

Peter Schwennesen


这篇关于DataGridView,向每个行添加唯一的渐变的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆