Hi all,

how to get values from checkbox in datagridview and transfer into arraylist? i'm using datagridviewcheckbox column..new checkbox will be added into datagridview when it populate from sql query. the example table is shown as below.

CardNo|Name|Door Access|checkbox1|Lift Access|checkbox2
02233 |Lyn |Testing | checked | - |unchecked
01999 |Matt| - |unchecked|FreeAcc |checked

so, when i checked checkbox 1 and checkbox2, the card number and value of checkbox will be insert into an arraylist. the arraylist should be like this:
arrList(0) = 02233|1|0
arrList(1) = 01999|0|1

please help me? it seem go complicated when i'm using for loop.

thank u..

Hi,
i did the code for you, I hope I have understood you well.
Check it out, and let me know:

Public Partial Class Form1
	Inherits Form
	Private bList As BindingList(Of Game)
	Private list As List(Of ListCheck)
	Public Sub New()
		InitializeComponent()
		dataGridView1.CurrentCellDirtyStateChanged += New EventHandler(AddressOf dataGridView1_CurrentCellDirtyStateChanged)
		dataGridView1.CellValueChanged += New DataGridViewCellEventHandler(AddressOf dataGridView1_CellValueChanged)

		'creating a binding source:
		bList = New BindingList(Of Game)()
		bList.Add(New Game() With { _
			Key .CardNumber = 2233, _
			Key .Name = "Lyn", _
			Key .DoorAccess = "Testing", _
			Key .Tick1 = False, _
			Key .LifeAccess = "-", _
			Key .Tick2 = False _
		})
		bList.Add(New Game() With { _
			Key .CardNumber = 1999, _
			Key .Name = "Matt", _
			Key .DoorAccess = "-", _
			Key .Tick1 = False, _
			Key .LifeAccess = "FreeAcc", _
			Key .Tick2 = False _
		})
		dataGridView1.DataSource = New BindingSource(bList, Nothing)

		'setting some properties of dgv:
		dataGridView1.AllowUserToAddRows = False
		'dataGridView1.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells);
		dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill

		'creating new custom list to save card number, and both checkBoxes:
		list = New List(Of ListCheck)()
	End Sub

	Private Sub dataGridView1_CurrentCellDirtyStateChanged(sender As Object, e As EventArgs)
		If dataGridView1.IsCurrentCellDirty Then
			dataGridView1.CommitEdit(DataGridViewDataErrorContexts.Commit)
		End If
	End Sub

	Private Sub dataGridView1_CellValueChanged(obj As Object, e As DataGridViewCellEventArgs)
		If e.ColumnIndex = 3 OrElse e.ColumnIndex = 5 Then
			'compare to checkBoxes column index
			Dim _tick1 As DataGridViewCheckBoxCell = TryCast(dataGridView1(3, e.RowIndex), DataGridViewCheckBoxCell)
			Dim _tick2 As DataGridViewCheckBoxCell = TryCast(dataGridView1(5, e.RowIndex), DataGridViewCheckBoxCell)
			Dim cardNo As Integer = Integer.Parse(dataGridView1(0, e.RowIndex).Value.ToString())

			'checking if the card Number exist in the list
			'if not, it addes a new card and values of the checkBoxes
			Dim bChecking As Boolean = False
			Dim i As Integer
			For i = 0 To list.Count - 1
				If list(i).CardNumber = cardNo Then
					bChecking = True
				End If
			Next
			If Not bChecking Then
				list.Add(New ListCheck() With { _
					Key .CardNumber = cardNo, _
					Key .Tick1 = CBool(_tick1.Value), _
					Key .Tick2 = CBool(_tick2.Value) _
				})
			Else
				list(i - 1).Tick1 = CBool(_tick1.Value)
				list(i - 1).Tick2 = CBool(_tick2.Value)
				'removing card number from the list if both ticks are false:
				If Not CBool(_tick1.Value) AndAlso Not CBool(_tick2.Value) Then
					list.RemoveAt(i - 1)
				End If
			End If
		End If
	End Sub

	Private Sub buttonCheck_Click(sender As Object, e As EventArgs)
		'just a check of the cards and selected (unselected) checkBoxes in the list:
		Dim sb As New StringBuilder()
		For Each a As ListCheck In list
			sb.AppendLine("Card number: " & a.CardNumber & " | Tick 1: " & (If(a.Tick1, "1", "0")) & " | Tick 2: " & (If(a.Tick2, "1", "0")))
		Next
		MessageBox.Show(If(sb.Length > 0, sb.ToString(), "List is empty."))
	End Sub
End Class
Class Game
	Public Property CardNumber() As Integer
		Get
			Return m_CardNumber
		End Get
		Set
			m_CardNumber = Value
		End Set
	End Property
	Private m_CardNumber As Integer
	Public Property Name() As String
		Get
			Return m_Name
		End Get
		Set
			m_Name = Value
		End Set
	End Property
	Private m_Name As String
	Public Property DoorAccess() As String
		Get
			Return m_DoorAccess
		End Get
		Set
			m_DoorAccess = Value
		End Set
	End Property
	Private m_DoorAccess As String
	Public Property Tick1() As Boolean
		Get
			Return m_Tick1
		End Get
		Set
			m_Tick1 = Value
		End Set
	End Property
	Private m_Tick1 As Boolean
	Public Property LifeAccess() As String
		Get
			Return m_LifeAccess
		End Get
		Set
			m_LifeAccess = Value
		End Set
	End Property
	Private m_LifeAccess As String
	Public Property Tick2() As Boolean
		Get
			Return m_Tick2
		End Get
		Set
			m_Tick2 = Value
		End Set
	End Property
	Private m_Tick2 As Boolean
End Class

Public Class ListCheck
	Public Property CardNumber() As Integer
		Get
			Return m_CardNumber
		End Get
		Set
			m_CardNumber = Value
		End Set
	End Property
	Private m_CardNumber As Integer
	Public Property Tick1() As Boolean
		Get
			Return m_Tick1
		End Get
		Set
			m_Tick1 = Value
		End Set
	End Property
	Private m_Tick1 As Boolean
	Public Property Tick2() As Boolean
		Get
			Return m_Tick2
		End Get
		Set
			m_Tick2 = Value
		End Set
	End Property
	Private m_Tick2 As Boolean

thanx mitja! appreciate it a lot !!:)

YOu are welcome?

btw, if you are satisfied with the answer, you can mark it as answered (or vote up).
thx in advance

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.