Checkbox datagridview
I have a datagridview on my form and with this I am rendering its headers for the custom name styl开发者_JAVA百科e and the filter image.
What I want is :
To add the checkbox column with headercheckbox at the very first position not the zeroth index. I want it at -1.
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:
Create (programmatically) a column with checkboxes. The easiest way to achieve this is to create a column of type DataGridViewCheckBoxColumn().
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
精彩评论