DataGridView复选框列标题单元格. [英] DataGridView checkbox column header cell.

查看:79
本文介绍了DataGridView复选框列标题单元格.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我正在尝试在datagridview列标题中呈现一个复选框,以便在选中/取消选中该复选框时,该列中的所有单元格都应
相应地选中/未选中. datagridview绑定到数据库表,但是包含复选框标题单元格的列未绑定.
问题是,每当我单击标题复选框时,都会检查该列中的所有单元格,但标题复选框本身变得不可见.
如果再次单击HEADER(尽管该复选框不可见,但仍可以单击标题),则未选中下面列中的所有单元格,并且标题
复选框变为可见且未选中.除了标题复选框的可见性之外,一切都按预期进行.为什么会这样?
请提出如何摆脱它.问候.
我的代码如下:

Hi,
I''m trying to render a checkbox in a datagridview column header so that when the checkbox is checked/unchecked all the cells in that column should be
checked/unchecked accordingly. The datagridview is bound to a database table but the column containing the checkbox header cell is unbound.
The problem is, whenever i click the header checkbox, all the cells in that column are checked but the header checkbox itself gets invisible.
If I click the HEADER (though the checkbox is invisible, still I can click the header) again, all the cells in the column below are unchecked and the header
checkbox becomes visible and unchecked. Everything is happening as expected except the visibility of the header checkbox. Why is it so?
Please suggest how to get rid of it. Regards.
My code follows below:

'' The custom class for checkbox header cell.
Imports System.Drawing

Public Delegate Sub CheckBoxClickedHandler(ByVal state As Boolean)

Public Class DataGridViewCheckBoxHeaderCellEventArgs
    Inherits EventArgs
    Private _bChecked As Boolean

    Public Sub New(ByVal bChecked As Boolean)
        _bChecked = bChecked
    End Sub

    Public ReadOnly Property Checked As Boolean
        Get
            Return _bChecked
        End Get
    End Property
End Class

Public Class DataGridViewCheckBoxHeaderCell
    Inherits DataGridViewColumnHeaderCell
    Private checkBoxLocation As Point
    Private checkBoxSize As Size
    Private _checked As Boolean = False
    Private _cellLocation As New Point
    Private _cbState As Windows.Forms.VisualStyles.CheckBoxState = VisualStyles.CheckBoxState.UncheckedNormal
    Public Event OnCheckBoxClicked As CheckBoxClickedHandler

    Public Sub New()
    End Sub

    Protected Overrides Sub Paint(ByVal graphics As System.Drawing.Graphics, ByVal clipBounds As System.Drawing.Rectangle, ByVal cellBounds As System.Drawing.Rectangle, ByVal rowIndex As Integer, ByVal dataGridViewElementState As System.Windows.Forms.DataGridViewElementStates, ByVal value As Object, ByVal formattedValue As Object, ByVal errorText As String, ByVal cellStyle As System.Windows.Forms.DataGridViewCellStyle, ByVal advancedBorderStyle As System.Windows.Forms.DataGridViewAdvancedBorderStyle, ByVal paintParts As System.Windows.Forms.DataGridViewPaintParts)
        MyBase.Paint(graphics, clipBounds, cellBounds, rowIndex, dataGridViewElementState, value, formattedValue, errorText, cellStyle, advancedBorderStyle, paintParts)

        Dim p As New Point
        Dim s As Size = CheckBoxRenderer.GetGlyphSize(graphics, Windows.Forms.VisualStyles.CheckBoxState.UncheckedNormal)
        p.X = cellBounds.Location.X + (cellBounds.Width / 2) - (s.Width / 2)
        p.Y = cellBounds.Location.Y + (cellBounds.Height / 2) - (s.Height / 2)
        _cellLocation = cellBounds.Location
        checkBoxLocation = p
        checkBoxSize = s

        If _checked Then
            _cbState = Windows.Forms.VisualStyles.CheckBoxState.CheckedNormal
        Else
            _cbState = Windows.Forms.VisualStyles.CheckBoxState.UncheckedNormal
            CheckBoxRenderer.DrawCheckBox(graphics, checkBoxLocation, _cbState)
        End If
    End Sub

    Protected Overrides Sub OnMouseClick(ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs)
        Dim p As New Point(e.X + _cellLocation.X, e.Y + _cellLocation.Y)
        If p.X >= checkBoxLocation.X And p.X <= checkBoxLocation.X + checkBoxSize.Width And p.Y >= checkBoxLocation.Y And p.Y <= checkBoxLocation.Y + checkBoxSize.Height Then
            _checked = Not _checked
            RaiseEvent OnCheckBoxClicked(_checked)
            Me.DataGridView.InvalidateCell(Me)
        End If

        MyBase.OnMouseClick(e)
    End Sub
End Class

'' Actual class where the custom class is used.
'' dgvContacts is the name of the datagridview.
'' colCheck is the name of the column containing the checkbox header cell.
Public Class ContactList
''Other declarations...
......................
......................
Private WithEvents _cbHeader As DataGridViewCheckBoxHeaderCell

Sub New()
        '' This call is required by the designer.
        InitializeComponent()

        '' Add any initialization after the InitializeComponent() call.
	_cbHeader = New DataGridViewCheckBoxHeaderCell
        colCheck.HeaderCell = _cbHeader
        colCheck.HeaderText = ""
        colCheck.Visible = True
End Sub

''Other definitions...
.....................
.....................

Private Sub _cbHeader_OnCheckBoxClicked(ByVal state As Boolean) Handles _cbHeader.OnCheckBoxClicked
        For i As Integer = 0 To dgvContacts.RowCount - 1
            dgvContacts("colCheck", i).Value = state
        Next

        dgvContacts.EndEdit()
End Sub
End Class

推荐答案

如果出错,请不要介意

而不是进行真实或错误的更改,而是启用真实或错误,它可以帮助您...
if wrong plz dont mind

instead of doing visibile true or false change enable true or false it helps u i think...


啊!我对错误进行了三角剖分.这是:

Ah! I triangulated the bug. Here it is:

If _checked Then
    _cbState = Windows.Forms.VisualStyles.CheckBoxState.CheckedNormal
Else
    _cbState = Windows.Forms.VisualStyles.CheckBoxState.UncheckedNormal
    CheckBoxRenderer.DrawCheckBox(graphics, checkBoxLocation, _cbState)
End If



应该是:



It should be:

If _checked Then
    _cbState = Windows.Forms.VisualStyles.CheckBoxState.CheckedNormal
Else
    _cbState = Windows.Forms.VisualStyles.CheckBoxState.UncheckedNormal
End If

CheckBoxRenderer.DrawCheckBox(graphics, checkBoxLocation, _cbState)


这篇关于DataGridView复选框列标题单元格.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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