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 CustomerOrdersF orm
Private checkState As System.Collecti ons.Generic.Dic tionary(Of Integer, Boolean)
Public Overloads Sub ShowDialog(ByVa l customerId As String, ByVal parent As IWin32Window, ByVal northwindDS As DataSet)
statusStripPane l1.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(northw indDS.Tables("O rders"))
dv.RowFilter = "CustomerID = '" + customerId + "'"
dv.Sort = "OrderID"
dataGridView1.A utoGenerateColu mns = False
dataGridView1.D ataSource = dv
' The check box column will be virtual.
dataGridView1.V irtualMode = True
dataGridView1.C olumns.Insert(0 , New DataGridViewChe ckBoxColumn)
' Modify the first column
With dataGridView1.C olumns(0)
' Don't allow the column to be resizable.
.Resizable = DataGridViewTri State.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 DataGridViewCol umn In dataGridView1.C olumns
If Not (c.Index = 0) Then
c.ReadOnly = True
End If
Next
' Initialize the dictionary that contains the boolean check state.
checkState = New System.Collecti ons.Generic.Dic tionary(Of Integer, Boolean)
' Show the dialog.
Me.ShowDialog(p arent)
End Sub
Private Sub dataGridView1_C ellValuePushed( ByVal sender As Object, ByVal e As DataGridViewCel lValueEventArgs ) Handles dataGridView1.C ellValuePushed
' 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(dataGridV iew1.Rows(e.Row Index).Cells("O rderID").Value, Integer)
' Add or update the checked value to the dictionary depending on if the
' key (orderID) already exists.
If Not checkState.Cont ainsKey(orderID ) Then
checkState.Add( orderID, CType(e.Value, Boolean))
Else
checkState(orde rID) = CType(e.Value, Boolean)
End If
End If
End Sub
Private Sub dataGridView1_C ellValueNeeded( ByVal sender As Object, ByVal e As DataGridViewCel lValueEventArgs ) Handles dataGridView1.C ellValueNeeded
' 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(dataGridV iew1.Rows(e.Row Index).Cells("O rderID").Value, Integer)
If checkState.Cont ainsKey(orderID ) Then
e.Value = checkState(orde rID)
Else
e.Value = False
End If
End If
End Sub
Private Sub processToolStri pButton_Click(B yVal sender As Object, ByVal e As EventArgs) Handles processToolStri pButton.Click
' Perform processing here.
MessageBox.Show ("The following rows were selected....")
End Sub
End Class
*** Posted via a free Usenet account from http://www.teranews.com ***