473,569 Members | 2,845 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 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 ***
May 31 '06 #1
0 8629

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
2935
by: David | last post by:
I'm creating a C# (.NET BETA2) Windows application (NOT a Web application, a Windows application), and I would like to programmatically selecting a row inside of a DataGridView. It seems like it should be pretty easy to do, but I'm have a hard time with this. Can anyone give me any guidance? Thanks, David
1
2229
by: Jay | last post by:
Hi All, My users are complaining about the page refreshing when they are selecting multiple rows in a datagrid. Has anyone tried to manage this using javascript? I tried smartnavigation but that caused me other problems. Any ideas? Thanks, Jay
1
3332
by: Bob Loveshade | last post by:
I am looking for an example that shows how to select and highlight multiple rows in a DataGrid. My DataGrid is part of a Web User Control which is contained in an ASPX page. I haven't been able to find any examples which demonstrate how to do this. My Code:
0
1579
by: GS | last post by:
Hi, I have dataGridView with MultupleSelect property enabled and I programmatically set rows to be Selected based on checkbox being selected in DataGrid (code below). Issue is that it resets Selected rows each time I select a new row. What am I doing wrong? private void dgrResults_CellContentClick(object sender,...
3
32254
by: Rich | last post by:
Hello, I am populating a datagridview from a datatable and filtering the number of rows with a dataview object. Is there a way to retrieve the rows displayed by the datagridview into a separate datatable without having to loop through each column in the datagridview? Or is there a way to retrieve the rows from the original datatable...
0
1847
by: Scotty | last post by:
Hi, Hope someone can help me I have a datagridview sith data Code below works fine if I print the data if there is only 1 page (without using '!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! If intRowPos <= 10 Then e.HasMorePages = False
2
4274
by: mrstrong | last post by:
Gday, I have a datagridview that I am creating the columns programatically which all seems to work fine. I have a couple of dropdown boxes, so I have set the editMode= EditOnEnter. Now my checkboxes dont seem to work, and the datagridview throws a dataerror when I try to leave the cell after trying to check/uncheck the checkbox?!
2
6109
by: mrstrong | last post by:
Gday, Why would all my checkboxes inside a datagridview stop working (ie checked state not updating when user clicks) when the datagridview's editmode property is changed to "EditOnEnter"? It seems to return an error: "Formatted Value of the cell has wrong type". It works fine when the datagridview's editmode property is
2
2959
by: ravitunk | last post by:
hi..i have a datagridview in my windows application using C#......i want to select mutiple cells(by pressing shift key) or select mutiple columns or multiple rows.....if this happens then i should disable a button control....plz tell me what event gets fired if a user selects multiple cells or columns or rows..so tht i can write code to disable a...
0
7695
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7612
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7922
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7964
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6281
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5218
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3637
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2111
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1209
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.