滚动时,所选的DataGridView行被另一行覆盖(合并) [英] Selected DataGridView row is being overwritten (merged) with another row when scrolling

查看:91
本文介绍了滚动时,所选的DataGridView行被另一行覆盖(合并)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一个DataGridView,其中有56行数据.当我选择任何一行时,将其滚动出屏幕然后再重新打开,所选行中的原始数据将与另一行的数据合并-似乎取决于我是向上/向下滚动还是使用滚动条或鼠标滚轮(我怀疑pgUp/Down和箭头键也会有类似的效果).当用户水平滚动时,搜索Web只会产生一种类似情况的结果...建议他使用Double Buffering,但是基于反射的代码只会使我选择的行变成全黑:

So I have a DataGridView with 56 rows of data in it. When I select any row, then scroll it off the screen and then back on again, the original data from the selected row becomes merged with the data of another row - seems to depend on whether I scroll up/down or use scrollbar or mouse wheel (I suspect pgUp/Down and arrow keys will have a similar effect also). Searching the web only produced one result of a similar situation when the user scrolled horizontally... it was suggested he use Double Buffering, but the Reflection based code only caused my selected row to turn totally black:

Imports System.Reflection

Module DataGridViewExtensions
    <System.Diagnostics.DebuggerStepThrough()> _
     <System.Runtime.CompilerServices.Extension()> _
    Public Sub DoubleBuffered(ByVal sender As DataGridView, ByVal Setting As Boolean)
        Dim dgvType As Type = sender.[GetType]()
        Dim pi As PropertyInfo = dgvType.GetProperty("DoubleBuffered", BindingFlags.Instance Or BindingFlags.NonPublic)
        pi.SetValue(sender, Setting, Nothing)
    End Sub
End Module

我尝试将DGV VirtualMode设置为true和false,但两者都不起作用.任何见识都会很棒!

I have tried setting the DGV VirtualMode to true and false and neither makes a difference. Any insight would be great!

推荐答案

好,所以这可能不是确定的答案,但指出了正确的方向... DGV存在其他问题-最初是DGV如图所示,第一行被选中并以白色覆盖"背景颜色...找到了这段代码来解决背景颜色问题,并解决了文本覆盖问题:

Ok, so this may not be the definitive answer but points me in the right direction... was having other issues with the DGV - when the DGV was initially shown, the first row was being selected and "overwriting" the background colour in white... found this code to fix the background colour issue and it solved the text overwriting issue:

Private Sub dgvPicks_CellFormatting(sender As Object, e As System.Windows.Forms.DataGridViewCellFormattingEventArgs) Handles dgvPicks.CellFormatting
    If e.ColumnIndex = 0 Then
        e.CellStyle.SelectionBackColor = SystemColors.Highlight
        e.CellStyle.SelectionForeColor = Color.White
    Else
        e.CellStyle.SelectionBackColor = e.CellStyle.BackColor
        e.CellStyle.SelectionForeColor = e.CellStyle.ForeColor
    End If
End Sub

不确定是专门设置正确的 SelectionBackColor 还是使用 CellFormatting 事件,但是此代码只用一块石头杀死了两只鸟!我从本文中找到了可以提供更多见解的代码:在DataGridView控件中实现透明行选择

Not sure if it specifically has to do with properly setting the SelectionBackColor or has to do with using the CellFormatting event, but this code killed two birds with one stone! I found the code from this article which may provide more insight: Implementing a transparent row selection in a DataGridView control

这篇关于滚动时,所选的DataGridView行被另一行覆盖(合并)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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