Help: Selecting Rows with checkbox in Datagridview

G

Guest

I am testing the following piece of code:
Is it possible to list the row number containing the checked box? After checking the desired box, I would want a message box to popup listing the row numbers on the process button click event.
Many thanks

Option Explicit On
Option Strict On
Imports System.Data

' Form to show the order's for a customer.
Public Class CustomerOrdersForm
Private checkState As System.Collections.Generic.Dictionary(Of Integer, Boolean)

Public Overloads Sub ShowDialog(ByVal customerId As String, ByVal parent As IWin32Window, ByVal northwindDS As DataSet)
statusStripPanel1.Width = 150

' Put the customer id in the window title.
Me.Text = "Orders for Customer ID: " + customerId

' Create a DataView of the Orders table to filter on the specified customerID.
Dim dv As DataView = New DataView(northwindDS.Tables("Orders"))
dv.RowFilter = "CustomerID = '" + customerId + "'"
dv.Sort = "OrderID"
dataGridView1.AutoGenerateColumns = False
dataGridView1.DataSource = dv

' The check box column will be virtual.
dataGridView1.VirtualMode = True
dataGridView1.Columns.Insert(0, New DataGridViewCheckBoxColumn)

' Modify the first column
With dataGridView1.Columns(0)
' Don't allow the column to be resizable.
.Resizable = DataGridViewTriState.False

' Make the check box column frozen so it is always visible.
.Frozen = True

' Put an extra border to make the frozen column more visible
.DividerWidth = 1
End With

' Make all columns except the first read only.
For Each c As DataGridViewColumn In dataGridView1.Columns
If Not (c.Index = 0) Then
c.ReadOnly = True
End If
Next

' Initialize the dictionary that contains the boolean check state.
checkState = New System.Collections.Generic.Dictionary(Of Integer, Boolean)

' Show the dialog.
Me.ShowDialog(parent)
End Sub

Private Sub dataGridView1_CellValuePushed(ByVal sender As Object, ByVal e As DataGridViewCellValueEventArgs) Handles dataGridView1.CellValuePushed
' Handle the notification that the value for a cell in the virtual column
' needs to be pushed back to the dictionary.

If e.ColumnIndex = 0 Then
' Get the orderID from the OrderID column.
Dim orderID As Integer = CType(dataGridView1.Rows(e.RowIndex).Cells("OrderID").Value, Integer)

' Add or update the checked value to the dictionary depending on if the
' key (orderID) already exists.
If Not checkState.ContainsKey(orderID) Then
checkState.Add(orderID, CType(e.Value, Boolean))
Else
checkState(orderID) = CType(e.Value, Boolean)
End If
End If
End Sub

Private Sub dataGridView1_CellValueNeeded(ByVal sender As Object, ByVal e As DataGridViewCellValueEventArgs) Handles dataGridView1.CellValueNeeded
' Handle the notification that the value for a cell in the virtual column
' is needed. Get the value from the dictionary if the key exists.
If e.ColumnIndex = 0 Then
Dim orderID As Integer = CType(dataGridView1.Rows(e.RowIndex).Cells("OrderID").Value, Integer)

If checkState.ContainsKey(orderID) Then
e.Value = checkState(orderID)
Else
e.Value = False
End If
End If
End Sub

Private Sub processToolStripButton_Click(ByVal sender As Object, ByVal e As EventArgs) Handles processToolStripButton.Click
' Perform processing here.
MessageBox.Show("The following rows were selected....")
End Sub
End Class




*** ***
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top