复选框数据网格视图

发布于 2024-12-07 18:59:41 字数 2016 浏览 0 评论 0 原文

我的表单上有一个 datagridview,通过它我可以渲染自定义名称样式和过滤器图像的标题。

我想要的是:

  1. 在第一个位置而不是第零个索引处添加带有 headercheckbox 的复选框列。我希望它位于 -1。

  2. 在特定列中添加一些复选框。

我能够将它们添加到想要的位置,但它没有刷新界面,并且我无法让它们选中/取消选中。

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 have a datagridview on my form and with this I am rendering its headers for the custom name style and the filter image.

What I want is :

  1. To add the checkbox column with headercheckbox at the very first position not the zeroth index. I want it at -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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

嗳卜坏 2024-12-14 18:59:41

如果我错了,请纠正我,但我看到两个不同的问题:

  1. 创建(以编程方式)带有复选框的列。
    实现此目的的最简单方法是创建 DataGridViewCheckBoxColumn() 类型的列。

  2. 将此列移到行开头之前
    对于这个问题,我建议查看 WPF 而不是 WinForms。 WPF 提供了更多的功能来设计特定的布局和界面。下面是 MSDN 提供的一个很好的示例来实现这一点: http://social.msdn.microsoft.com/Forums/en-US/a87fc1cb-4d2c-4252-a628-910c02b03adb/wpf-datagrid-with-multiple-row-selectioncheckbox-column-template< /a>

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().

  2. Move this column before the beginning of the rows
    For this problem, I'd advise looking into WPF instead of WinForms. WPF offers much more functionality to designing specific layout and interfaces. 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文