469,594 Members | 2,118 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,594 developers. It's quick & easy.

Help: Selecting Rows with checkbox in Datagridview

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("OrderI D").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("OrderI D").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


*** Posted via a free Usenet account from http://www.teranews.com ***
May 31 '06 #1
0 8456

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Bob Loveshade | last post: by
reply views Thread by Scotty | last post: by
2 posts views Thread by mrstrong | last post: by
2 posts views Thread by mrstrong | last post: by
reply views Thread by suresh191 | last post: by
4 posts views Thread by guiromero | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.