简体   繁体   English

复选框datagridview

[英]Checkbox datagridview

I have a datagridview on my form and with this I am rendering its headers for the custom name style and the filter image. 我在表单上有一个datagridview,并以此呈现自定义名称样式和过滤器图像的标题。

What I want is : 我想要的是:

  1. To add the checkbox column with headercheckbox at the very first position not the zeroth index. 要在头位置而不是第零索引处添加带有headercheckbox的复选框列。 I want it at -1. 我想要-1。

  2. To add some checkboxes at the particular columns. 在特定列添加一些复选框。

I was able to add them at the wanted positions but it was not refreshing the interface and I was unable to get them check/unchecked. 我能够将它们添加到所需位置,但是它没有刷新界面,因此无法使它们处于选中/未选中状态。

Dim indx As Int16 = -1

Private Sub DataGridView1_CellPainting(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellPaintingEventArgs) Handles DataGridView1.CellPainting
    tbIndex = 0 'mainTab.SelectedItem.Name.Substring(6)
    If e.ColumnIndex >= 0 AndAlso e.RowIndex = -1 Then
        If dic.ContainsKey(tbIndex) Then
            indx = dic.Item(tbIndex)
        Else
            indx = -1
        End If


        e.PaintBackground(e.ClipBounds, False)

        Dim pt As Point = e.CellBounds.Location
        Dim offset As Integer = (e.CellBounds.Width - 25)

        pt.X += offset
        pt.Y = 5

        If e.ColumnIndex = indx Then
            e.Graphics.DrawImage(My.Resources.SortDSC, pt.X, pt.Y, 20, 20)
        Else
            e.Graphics.DrawImage(My.Resources.SortASC, pt.X, pt.Y, 20, 20)
        End If

        Dim drawFormat As System.Drawing.StringFormat = New System.Drawing.StringFormat()
        drawFormat.FormatFlags = StringFormatFlags.NoFontFallback

       e.Graphics.DrawString(dgv(tbIndex).Columns(e.ColumnIndex).HeaderText, New Font("Georgia", 10), Brushes.DodgerBlue, pt.X - offset, pt.Y + 15, drawFormat)
        e.Handled = True
        e.Handled = True
    End If
End Sub

Private Sub DataGridView1_ColumnHeaderMouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles DataGridView1.ColumnHeaderMouseClick
        If indx = e.ColumnIndex.ToString Then
            indx = -1
        Else
            indx = e.ColumnIndex.ToString
        End If

        If Not dic.ContainsKey(tbIndex) Then
            dic.Add(tbIndex, indx)
        Else
            dic.Remove(tbIndex)
            dic.Add(tbIndex, indx)
        End If
    End Sub

I want to add them at runtime. 我想在运行时添加它们。

Please correct me if I'm wrong, but I'm seeing two different questions: 如果我错了,请纠正我,但我看到两个不同的问题:

  1. Create (programmatically) a column with checkboxes. 创建(以编程方式)带有复选框的列。 The easiest way to achieve this is to create a column of type DataGridViewCheckBoxColumn(). 实现此目的的最简单方法是创建一个DataGridViewCheckBoxColumn()类型的列。

  2. Move this column before the beginning of the rows For this problem, I'd advise looking into WPF instead of WinForms. 将此列移到行的开头之前。对于此问题,我建议您研究WPF而不是WinForms。 WPF offers much more functionality to designing specific layout and interfaces. WPF为设计特定的布局和界面提供了更多功能。 Here's an excellent example by MSDN to do exactly this: http://social.msdn.microsoft.com/Forums/en-US/a87fc1cb-4d2c-4252-a628-910c02b03adb/wpf-datagrid-with-multiple-row-selectioncheckbox-column-template 这是MSDN做到这一点的一个很好的例子: http : //social.msdn.microsoft.com/Forums/en-US/a87fc1cb-4d2c-4252-a628-910c02b03adb/wpf-datagrid-with-multiple-row-selection复选框列模板

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM