Results 1 to 13 of 13

Thread: [RESOLVED] DataGridView CheckBox

Threaded View

  1. #1

    Thread Starter
    Lively Member anamada's Avatar
    Join Date
    Jun 2011
    Location
    Philippines, Makati
    Posts
    107

    Resolved [RESOLVED] DataGridView CheckBox

    Good day everyone!


    I need urgent help on my program.
    In DGV, I have 2 checkbox columns. Column1 and Column 2. Once I checked a checkbox in a column and it happens that there is already a checked checkbox in the other column and they are in the same ROW. It will automatically disable/uncheck the Checked Checkbox.. Meaning only One CheckBox must be allowed to be marked if happens that they are in the same row.

    I'll attach a sample pic of what I wanted to happen.

    This is what i've done so far.

    Code:
    Imports System.Data.SqlClient
    Imports System.Text.RegularExpressions
    Imports System.IO
    Imports System.Net.Mail
    Imports System.Net
    Imports System.Text
    
    Public Class Form1
        Inherits System.Windows.Forms.Form
    
        Dim sql_command As SqlCommand
        Dim connectionString As String
        Dim sql_connection As SqlConnection
        Dim icount As Integer
        Dim datareadersql As SqlDataReader
        Dim sqlstring As String
        Dim str As String
        Dim sqlcom As String
    
        Dim itemlist As New ArrayList()
        Dim value As String
        Dim stoid As String
    
        
        'STOCKHOLDER INFORMATION: Search GO BUTTON
        '
        Private Sub Go1B_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Go1B.Click
            Try
                connectionString = ""
                sql_connection = New SqlConnection(connectionString)
                sql_connection.Open()
    
                If (cSearch1.Text = "" Or tSearch1.Text = "") Then
                    sqlstring = "SELECT STK_HOLD_CODE, STK_HOLD_NAME, STK_FRACTIONAL_SHARES FROM Stkmast WHERE STK_HOLD_NAME LIKE '%" & tSearch1.Text & "%'"
    
                ElseIf cSearch1.SelectedIndex = 0 Then
                    sqlstring = "SELECT STK_HOLD_CODE, STK_HOLD_NAME, STK_FRACTIONAL_SHARES FROM Stkmast WHERE STK_HOLD_NAME LIKE '%" & tSearch1.Text & "%'"
    
                ElseIf cSearch1.SelectedIndex = 1 Then
                    sqlstring = "SELECT STK_HOLD_CODE, STK_HOLD_NAME, STK_FRACTIONAL_SHARES FROM Stkmast WHERE STK_HOLD_CODE LIKE '%" & tSearch1.Text & "%'"
    
                Else
                    MsgBox("Select a Search Criteria")
                End If
    
                sql_command = New SqlCommand(sqlstring, sql_connection)
                Dim sqladapter As New SqlDataAdapter(sql_command)
                Dim stock_info As New DataSet()
                sqladapter.Fill(stock_info, "Stkmast")
                DataGridView1.DataSource = stock_info.Tables("Stkmast")
    
    
                DataGridView1.Columns(0).HeaderText = "Parent"
                DataGridView1.Columns(1).HeaderText = "Child"
                DataGridView1.Columns(2).HeaderText = "Stockholder Code"
                DataGridView1.Columns(3).HeaderText = "Name"
                DataGridView1.Columns(4).HeaderText = "No. of Shares"
    
            Catch ex As Exception
            End Try
        End Sub
        '
        'STOCKHOLDER INFORMATION: DB DATA LOAD
        '
        Private Sub stockInfoGb_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Me.Load
            connectionString = ""
            sql_connection = New SqlConnection(connectionString)
            sql_connection.Open()
            sqlstring = "SELECT STK_HOLD_CODE, STK_HOLD_NAME FROM Stkmast"
        End Sub
    
    
        '
        'TESTING (STOCKHOLDER CODE)
        '
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Try
    
                For Each row As DataGridViewRow In DataGridView1.Rows
                    Dim cell As DataGridViewCheckBoxCell = CType(row.Cells("checkboxchild"), DataGridViewCheckBoxCell)
                    Dim cell2 As DataGridViewCheckBoxCell = CType(row.Cells("checkboxparent"), DataGridViewCheckBoxCell)
                    If CBool(cell.Value) = True Then
                        stoid = CStr(row.Cells("STK_HOLD_CODE").Value)
                        itemlist.Add(stoid)
                    End If
    
                    If CBool(cell2.Value) = True Then
                        stoid = CStr(row.Cells("STK_HOLD_CODE").Value)
                        itemlist.Add(stoid)
                    End If
    
                Next
                Dim strStrings() As String = CType(itemlist.ToArray(GetType(String)), String())
                Dim strJoinedString As String
    
                For Each astring In itemlist
                    strJoinedString = strJoinedString
                    strJoinedString = Join(strStrings, ",")
    
    
                Next
                MsgBox(strJoinedString)
            Catch ex As Exception
                MsgBox(ex.Message)
            End Try
            itemlist.Clear()
        End Sub
    
    
        'Private Sub DataGridView1_CellMouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles DataGridView1.CellMouseDown
        '    If e.ColumnIndex = 0 Then
        '        If CBool(DataGridView1(0, e.RowIndex).Value()) = False Then
        '            If MessageBox.Show("Are you sure you want to assign this as a Parent Code?", "CheckBox", MessageBoxButtons.YesNo) = Windows.Forms.DialogResult.Yes Then
        '                DataGridView1(0, e.RowIndex).Value = True
    
        '            Else
        '                MessageBox.Show("You are only allowed to choose 1 Parent Code")
        '            End If
        '        End If
        '    End If
        'End Sub
    
    
    
    Private Sub DataGridView1_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick
            If (DataGridView1.Columns(e.ColumnIndex).Name = "checkboxparent") Then
                If (e.RowIndex >= 0) Then
    
                    For Each row As DataGridViewRow In Me.DataGridView1.Rows
                        If (row.Index <> e.RowIndex) Then
                            row.Cells("checkboxparent").Value = False
                        End If
                    Next
    
                End If
            End If
    
        End Sub
    
    End Class
    Private Sub DataGridView1_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick
    If (DataGridView1.Columns(e.ColumnIndex).Name = "checkboxparent") Then
    If (e.RowIndex >= 0) Then

    For Each row As DataGridViewRow In Me.DataGridView1.Rows
    If (row.Index <> e.RowIndex) Then
    row.Cells("checkboxparent").Value = False
    End If
    Next

    End If
    End If

    End Sub
    Attached Images Attached Images   
    Last edited by anamada; Jan 14th, 2012 at 01:10 AM.

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