Results 1 to 9 of 9

Thread: Struggeling with DataGridView Check Box Column.

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jan 2009
    Posts
    201

    Struggeling with DataGridView Check Box Column.

    Afternoon all,

    I have a datagridview that is populated from a SQL server via a datatable. I would like to add a Check Box Column with the idea to use the check boxes to do a multiple row selection. So far i've managed to add the column but I seem unable to check any of the check boxes. Any idea what I'm doing wrong?

    Thanks

    Dave

    Code:
                Dim EmployeeDT As New DataTable
                Dim EmployeeList As New EmployeeDetails
    
                V_EmployeeDGV.DataSource = Nothing
                V_EmployeeDGV.Rows.Clear()
                EmployeeDT = EmployeeList.GetEmployee_List(V_SIDTB.Text)
                V_EmployeeDGV.DataSource = EmployeeDT
                V_EmployeeDGV.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill
    
                V_EmployeeDGV.Columns("T_TID").Visible = False
    
                Dim ChkBoxCol As New DataGridViewCheckBoxColumn
                With ChkBoxCol
                    .FlatStyle = FlatStyle.Standard
                    .HeaderText = "Select"
                    .Width = 30
                End With
                V_EmployeeDGV.Columns.Insert(0, ChkBoxCol)
    
                V_EmployeeDGV.ClearSelection()

  2. #2
    PowerPoster ChrisE's Avatar
    Join Date
    Jun 2017
    Location
    Frankfurt
    Posts
    3,042

    Re: Struggeling with DataGridView Check Box Column.

    Hi,

    this might help...
    Code:
      Dim checkBoxColumn As New DataGridViewCheckBoxColumn()
    
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    
    
            ''add checkbox
            checkBoxColumn.HeaderText = "Select"
            checkBoxColumn.Width = 30
            checkBoxColumn.Name = "Select"
            DataGridView1.Columns.Insert(3, checkBoxColumn)
    
    
        End Sub
    
    
        Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
            Dim message As String = String.Empty
            For Each row As DataGridViewRow In DataGridView1.Rows
                Dim isSelected As Boolean = Convert.ToBoolean(row.Cells("Select").Value)
                If isSelected Then
                    message &= Environment.NewLine
                    'I have a ContactName in Cell 2
                    message &= row.Cells("ContactName").Value.ToString()
                End If
            Next
            'return all the selected Contacts
            MessageBox.Show("Selected Contact :" & message)
        End Sub
    regards
    Chris
    to hunt a species to extinction is not logical !
    since 2010 the number of Tigers are rising again in 2016 - 3900 were counted. with Baby Callas it's 3901, my wife and I had 2-3 months the privilege of raising a Baby Tiger.

  3. #3
    PowerPoster
    Join Date
    Sep 2005
    Location
    Modesto, Ca.
    Posts
    5,195

    Re: Struggeling with DataGridView Check Box Column.

    The DataGridViewCheckBoxColumn does have have a ReadOnly property, for some reason it may have defaulted to True or you might try setting it to False. Mine doesn't but it's worth testing.

  4. #4
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,684

    Re: Struggeling with DataGridView Check Box Column.

    Here is how I would do this, load a DataTable from the database table, add a new DataColumn to the DataTable.

    Code:
    Dim dt As DataTable = GetData()
    Add the column, set default values for each row to false and place the column at position 0 (or change it to 3 as shown in your initial post).
    Code:
    dt.Columns.Add(New DataColumn With {
        .ColumnName = "Process", .DataType = GetType(Boolean)})
    
    dt.Columns(CheckBoxName).SetOrdinal(0)
    For Each row As DataRow In dt.Rows
        row.SetField(Of Boolean)(CheckBoxName, False)
    Next
    Assign the data table to the DataGridView DataSource
    Code:
    DataGridView1.DataSource = dt
    Later
    Code:
    Dim Checked As List(Of DataRow) =
        (
            From Rows In CType(DataGridView1.DataSource, DataTable)
            Where Rows.Field(Of Boolean)("Process")
            Select Rows
        ).ToList

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Jan 2009
    Posts
    201

    Re: Struggeling with DataGridView Check Box Column.

    Thanks for your replies.

    @Karen, I never thought of adding the column to the datatable...!

    Anyhow, tried everything but still I can't check or un-check the checkbox. I though I'd dumb it right down:

    Code:
            V_EmployeeDGV.ReadOnly = False
            Dim ChkBoxCol As New DataGridViewCheckBoxColumn With {.FlatStyle = FlatStyle.Standard, .HeaderText = "Select", .Width = 30}
            V_EmployeeDGV.Columns.Insert(0, ChkBoxCol)
            V_EmployeeDGV.Rows.Add()
    A datagridview is created with 1 row and 1 column with 1 checkbox but I'm still unable to check or uncheck it. Something a bit weird is going on here.....

    Thanks

    Dave

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: Struggeling with DataGridView Check Box Column.

    Are you able to edit cells in other columns?

  7. #7
    Fanatic Member kpmc's Avatar
    Join Date
    Sep 2017
    Posts
    1,012

    Re: Struggeling with DataGridView Check Box Column.

    Dave,
    Follow Karens example, why are you still adding the CBcolumn to the dgv?

    Code:
    Public Class DGV_CheckBox
        Dim DT As New DataTable
        Private Sub DGV_CheckBox_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            DT.Columns.Add("Select", GetType(System.Boolean))
            DT.Columns("Select").DefaultValue = False
            DataGridView1.DataSource = DT
        End Sub
    End Class

  8. #8

    Thread Starter
    Addicted Member
    Join Date
    Jan 2009
    Posts
    201

    Re: Struggeling with DataGridView Check Box Column.

    Found it..... 'Editmode' was set to 'EditProgramaticaly'. Changed it to 'KeystrokeOrF2' so far it looks to be working as it should.....

  9. #9
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,684

    Re: Struggeling with DataGridView Check Box Column.

    Quote Originally Posted by QuattroDave View Post
    Thanks for your replies.

    @Karen, I never thought of adding the column to the datatable...!

    Anyhow, tried everything but still I can't check or un-check the checkbox. I though I'd dumb it right down:

    Code:
            V_EmployeeDGV.ReadOnly = False
            Dim ChkBoxCol As New DataGridViewCheckBoxColumn With {.FlatStyle = FlatStyle.Standard, .HeaderText = "Select", .Width = 30}
            V_EmployeeDGV.Columns.Insert(0, ChkBoxCol)
            V_EmployeeDGV.Rows.Add()
    A datagridview is created with 1 row and 1 column with 1 checkbox but I'm still unable to check or uncheck it. Something a bit weird is going on here.....

    Thanks

    Dave
    Hi Dave, I've always recommended working from the DataTable rather than the DataGridView e.g. give the DataGridView a Boolean column in a DataTable without defining a DataGridView columns and you will have a DataGridViewCheckBoxColumn. There is nothing wrong with setting display properties for the DataGridView and events that is about if.

    Check out my MSDN code sample for more on the above.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width