Results 1 to 4 of 4

Thread: [RESOLVED] DataGrid - Checkbox Selection Problem

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2005
    Location
    Michigan
    Posts
    304

    Resolved [RESOLVED] DataGrid - Checkbox Selection Problem

    I have the following datagrid but the checkboxes are not behaving normally. I expect each checkbox to accept 1 click to change state and another click to change it back but this is not happening.

    When I check on one of the boxes, it will select the entire row, and if I check it again, it will then change the state. Then the next click will select the row again, and the following click will change the state back.

    How can I stop the grid from selecting the entire row, and just change the state of the checkbox for each click?

    VB Code:
    1. Public dtSession As New DataTable("SessionTable")
    2.     Public dsSession As DataSet = New DataSet("SessionData")
    3.     Public sSessionId As String = "Session ID"
    4.     Public sSessionDesc As String = "Session Descripton"
    5.     Public sSessionCB1 As String = "CB1"
    6.     Public sSessionCB2 As String = "CB2"
    7.     Public sSessionCB3 As String = "CB3"
    8.     Public sSessionCB4 As String = "CB4"
    9.  
    10.  
    11.     Public Sub sessionDataGrid_Load()
    12.  
    13.         Dim myRow As DataRow
    14.         Dim x As Integer
    15.         Dim dgts As New DataGridTableStyle
    16.         Dim dgcs1 As New DataGridTextBoxColumn
    17.         Dim dgcs2 As New DataGridTextBoxColumn
    18.         Dim dgcs3 As New DataGridBoolColumn
    19.         Dim dgcs4 As New DataGridBoolColumn
    20.         Dim dgcs5 As New DataGridBoolColumn
    21.         Dim dgcs6 As New DataGridBoolColumn
    22.         Dim cb1, cb2, cb3, cb4 As DataColumn
    23.  
    24.                Try
    25.             sessionDataGrid.CaptionText = "Sessions Currently in Data Base"
    26.             dtSession.Clear()
    27.             dtSession.Columns.Add(New DataColumn(sSessionId, GetType(String)))
    28.             dtSession.Columns.Add(New DataColumn(sSessionDesc, GetType(String)))
    29.  
    30.             cb1 = New DataColumn(sSessionCB1, GetType(Boolean))
    31.             cb1.ReadOnly = False
    32.             cb1.DefaultValue = False
    33.             dtSession.Columns.Add(cb1)
    34.  
    35.             cb2 = New DataColumn(sSessionCB2, GetType(Boolean))
    36.             cb2.ReadOnly = False
    37.             cb2.DefaultValue = False
    38.             dtSession.Columns.Add(cb2)
    39.  
    40.             cb3 = New DataColumn(sSessionCB3, GetType(Boolean))
    41.             cb3.ReadOnly = False
    42.             cb3.DefaultValue = False
    43.             dtSession.Columns.Add(cb3)
    44.  
    45.             cb4 = New DataColumn(sSessionCB4, GetType(Boolean))
    46.             cb4.ReadOnly = False
    47.             cb4.DefaultValue = False
    48.             dtSession.Columns.Add(cb4)
    49.  
    50.             dgcs1.MappingName = sSessionId
    51.             dgcs1.ReadOnly = True
    52.             dgcs1.HeaderText = sSessionId
    53.             dgts.GridColumnStyles.Add(dgcs1)
    54.  
    55.             dgcs2.MappingName = sSessionDesc
    56.             dgcs2.ReadOnly = True
    57.             dgcs2.Width = sessionDataGrid.Size.Width / 3
    58.             dgcs2.HeaderText = sSessionDesc
    59.             dgts.GridColumnStyles.Add(dgcs2)
    60.  
    61.             dgcs3.MappingName = sSessionCB1
    62.             dgcs3.ReadOnly = False
    63.             dgcs3.AllowNull = False 'Makes the checkbox 2 state
    64.             dgcs3.HeaderText = sSessionCB2
    65.             dgts.GridColumnStyles.Add(dgcs3)
    66.  
    67.             dgcs4.MappingName = sSessionCB2
    68.             dgcs4.ReadOnly = False
    69.             dgcs4.AllowNull = False 'Makes the checkbox 2 state
    70.             dgcs4.HeaderText = sSessionCB2
    71.             dgts.GridColumnStyles.Add(dgcs4)
    72.  
    73.             dgcs5.MappingName = sSessionCB3
    74.             dgcs5.ReadOnly = False
    75.             dgcs5.AllowNull = False 'Makes the checkbox 2 state
    76.             dgcs5.HeaderText = sSessionCB3
    77.             dgts.GridColumnStyles.Add(dgcs5)
    78.  
    79.             dgcs6.MappingName = sSessionCB4
    80.             dgcs6.ReadOnly = False
    81.             dgcs6.AllowNull = False 'Makes the checkbox 2 state
    82.             dgcs6.HeaderText = sSessionCB4
    83.             dgts.GridColumnStyles.Add(dgcs6)
    84.  
    85.             dgts.MappingName = "SessionTable"
    86.             sessionDataGrid.TableStyles.Add(dgts)
    87.  
    88.             For x = 0 To gSessions.Length - 2
    89.                 myRow = dtSession.NewRow()
    90.                 myRow(sSessionId) = Hex(gSessions(x).Id)
    91.                 myRow(sSessionDesc) = gSessions(x).Desc
    92.                 myRow(sSessionCB1) = True 'TODO:Get actual value later
    93.                 myRow(sSessionCB2) = True 'TODO:Get actual value later
    94.                 myRow(sSessionCB3) = True 'TODO:Get actual value later
    95.                 myRow(sSessionCB4) = False 'TODO:Get actual value later
    96.                 dtSession.Rows.Add(myRow)
    97.             Next
    98.  
    99.             dsSession.Tables.Add(dtSession)
    100.             sessionDataGrid.DataSource = dtSession
    101.  
    102.         Catch ex As Exception
    103.             MsgBox(ex.Message)
    104.         End Try
    105.  
    106.     End Sub

  2. #2
    Lively Member
    Join Date
    Nov 2002
    Location
    Cranston, RI
    Posts
    110

    Re: DataGrid - Checkbox Selection Problem

    Try this.


    VB Code:
    1. Private Sub DataGrid1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DataGrid1.Click
    2.         Dim myColumn As Integer = 0
    3.         Dim pt As Point = DataGrid1.PointToClient(Control.MousePosition)
    4.         Dim hti As DataGrid.HitTestInfo = DataGrid1.HitTest(pt)
    5.         Dim bmb As BindingManagerBase = BindingContext(DataGrid1.DataSource,DataGrid1.DataMember)
    6.  
    7.         If hti.Row < bmb.Count AndAlso hti.Type = DataGrid.HitTestType.Cell AndAlso hti.Column = myColumn Then
    8.             DataGrid1(hti.Row, myColumn) = Not CBool(DataGrid1(hti.Row, myColumn))
    9.                    
    10.        End If
    11. End Sub

    This is something I use with the checkbox in the first column.
    Last edited by JanZuk; Sep 26th, 2005 at 03:49 PM.

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2005
    Location
    Michigan
    Posts
    304

    Re: DataGrid - Checkbox Selection Problem

    Thanks jz. With the column tweak below it worked.

    VB Code:
    1. Public Sub sessionDataGrid_SingleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles sessionDataGrid.Click
    2.         Dim dgs As DataGridCell = sessionDataGrid.CurrentCell
    3.         Dim row As Integer = dgs.RowNumber
    4.         Dim col As Integer = dgs.ColumnNumber
    5.         Dim pt As Point = sessionDataGrid.PointToClient(Control.MousePosition)
    6.         Dim hti As DataGrid.HitTestInfo = sessionDataGrid.HitTest(pt)
    7.         Dim bmb As BindingManagerBase = BindingContext(sessionDataGrid.DataSource, sessionDataGrid.DataMember)
    8.         Static Dim last As Integer = 0
    9.  
    10.         'clear last row selected
    11.         sessionDataGrid.UnSelect(last)
    12.         sessionDataGrid.Select(row)
    13.         last = row
    14.  
    15.         'control checkbox
    16.         If hti.Row < bmb.Count AndAlso hti.Type = DataGrid.HitTestType.Cell AndAlso hti.Column = col Then
    17.             sessionDataGrid(hti.Row, col) = Not CBool(sessionDataGrid(hti.Row, col))
    18.         End If
    19.     End Sub

  4. #4
    Lively Member
    Join Date
    Nov 2002
    Location
    Cranston, RI
    Posts
    110

    Re: [RESOLVED] DataGrid - Checkbox Selection Problem

    Cool.

    Just be careful that the col that is being clicked and updated is a checkboxcolumn and not something else where you're trying to flip the bool.

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