Insert data from auto generated textbox to SQL Server database - sql

I have created a code for generating a text box in vb.net using button click and function
Public Function AddNewTextBox() As System.Windows.Forms.TextBox
Dim txt As New System.Windows.Forms.TextBox()
Me.Controls.Add(txt)
txt.Top = cLeft * 30
txt.Left = 100
'txt.Text = "TextBox " & Me.cLeft.ToString
cLeft = cLeft + 1
txt.ForeColor = Color.DarkGreen
txt.BackColor = Color.Gray
txt.Font = New Font("Arial", 14.0, FontStyle.Regular)
txt.Size = New Size(237, 31)
txt.Location = New Point(156, 130 + top1)
Return txt
End Function
In the button
Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'call the function
AddNewTextBox()
End Sub
I have tried this
cmd.CommandText = "INSERT INTO userlog ([username],[userlastname]) Values ( #username) "
cmd.Parameters.AddWithValue("#username", txt.Text(i).Text)
cmd.Parameters.AddWithValue("#userlastname", txt.Text(i).Text)
but getting an error in
txt.Text(i)
since txt is declared only in AddNewTextBox function.
I have made 3 auto generated text boxs
How do I save this data inside the text box to the database?

Add a FlowlayoutPanel to your form and set the FlowDirection to TopDown. (as commented by #jmcilhinney) This saves calculating the position of the text boxes.
It doesn't make sense to have a function returning a text box when you never use the return value.
The data access code uses the .Add method suggested by #SMor. See
http://www.dbdelta.com/addwithvalue-is-evil/
and
https://blogs.msmvps.com/jcoehoorn/blog/2014/05/12/can-we-stop-using-addwithvalue-already/
and another one:
https://dba.stackexchange.com/questions/195937/addwithvalue-performance-and-plan-cache-implications
I had to guess at the datatypes. Check your database for correct types.
The values come from the controls collection of the FlowLayoutPanel where the controls were added.
Using blocks ensure that you database objects are closed and disposed even if there is an error. Pass the connection string directly to the constructor of the connection and the command text and connection directly to the constructor of the command.
Public Sub AddNewTextBox()
Dim txt As New System.Windows.Forms.TextBox()
txt.Name = "user" & nameTextBox.ToString
txt.ForeColor = Color.DarkGreen
txt.BackColor = Color.Gray
txt.Font = New Font("Arial", 14.0, FontStyle.Regular)
txt.Size = New Size(120, 30)
FlowLayoutPanel1.Controls.Add(txt)
End Sub
Private Sub UpdateUsers()
Using cn As New SqlConnection("Your connection string")
Using cmd As New SqlCommand("INSERT INTO userlog ([username],[userlastname]) Values ( #username, #userlastname);", cn)
cmd.Parameters.Add("#username", SqlDbType.VarChar).Value = FlowLayoutPanel1.Controls(0).Text
cmd.Parameters.AddWithValue("#userlastname", SqlDbType.VarChar).Value = FlowLayoutPanel1.Controls(1).Text
cn.Open()
cmd.ExecuteNonQuery()
End Using
End Using
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
AddNewTextBox()
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
UpdateUsers()
End Sub
EDIT
For Each tb As TextBox In FlowLayoutPanel1.Controls
If tb.Text = "" Then
MessageBox.Show("Please fill all text boxes before Updating")
Return
End If
Next

Since the TextBox is being added to the control collection of the Form, you can use the OfType enumerable method to get all TextBox controls. What's more I would probably assign the Tag of the generated TextBox to the desired field name so that you can query the control collection for the first instance of the TextBox who's tag equals the desired field.
Also its worth mentioning that you can use the With keyword to get rid of some unnecessary code.
With all that being said, your AddNewTextBox method would look like this:
Public Function AddNewTextBox(ByVal fieldName As String) As System.Windows.Forms.TextBox
Dim txt As New System.Windows.Forms.TextBox()
Me.Controls.Add(txt)
With
.Top = cLeft * 30
.Left = 100
'.Text = "TextBox " & Me.cLeft.ToString
cLeft = cLeft + 1
.ForeColor = Color.DarkGreen
.BackColor = Color.Gray
.Font = New Font("Arial", 14.0, FontStyle.Regular)
.Size = New Size(237, 31)
.Location = New Point(156, 130 + top1)
.Tag = fieldName
End With
Return txt
End Function
Your Button's click event would look like this:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'call the function
Dim txt As TextBox = AddNewTextBox("username")
End Sub
And your parameterized query would look like this:
Dim usernameTextBox As TextBox = Me.Controls.OfType(Of TextBox).FirstOrDefault(Function(txt) txt.Tag IsNot Nothing AndAlso txt.Tag = "username")
If usernameTextBox IsNot Nothing Then
cmd.Parameters.AddWithValue("#username", usernameTextBox.Text)
End If

Related

Trying to get SUM of ListBox selected items from local DataTable into a Label

I have been searching for days, for any possible reference or suggestions and everything I've come across hasn't worked.
The goal:
User will select options in ComboBox1 that will then determine the available options in ComboBox2, then will populate a list of operations in ListBox1.
When the user selects available operations in ListBox1, I need the output to be the sum of values (total time in minutes in this case) into a label for display.
The data used in stored in a local db. So far everything works with my comboboxes and the listbox.
Im attempting to get the Text value, of all selected items, in ListBox1 to output the numeric value in my table (column 4 "OperationsTime"), into a label that will display the sum of all the selections (Total Time In Minutes).
Some Things I have Tried From Other Posts:
Label9.Text = ListBox1.ValueMember
Label9.Text = ListBox1.ValueMember.ToString
Label9.Text = CType(ListBox1.SelectedItem, DataRowView).Row.Item("OperationsTime").ToString
Attempted using Double:
Dim Total As Double = 0
For Each Time As Integer In ListBox1.SelectedItems
Total += CDbl(Time.ToString.Substring(Time.ToString.LastIndexOf(",") + 1))
Next
Label9.Text = Total.ToString
Screen Shot of the Table:
Operations Data Table
Below is my code:
Imports System.Data
Imports System.Configuration
Imports System.Data.SqlClient
Public Class MainHome
Private Function GetData(ByVal sql As String) As DataTable
Dim constr As String = "Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\hartj\Documents\visual studio 2015\Projects\TIMEMATRIX2.0\TIMEMATRIX2.0\TMX.mdf;Integrated Security=True"
Using con As SqlConnection = New SqlConnection(constr)
Using sda As SqlDataAdapter = New SqlDataAdapter(sql, con)
Dim dt As DataTable = New DataTable()
sda.Fill(dt)
Dim row As DataRow = dt.NewRow()
row(0) = 1
row(1) = "Please Select"
dt.Rows.InsertAt(row, 0)
Return dt
End Using
End Using
End Function
Private Sub MainHome_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
ComboBox1.DataSource = Me.GetData("SELECT SizeId, SizeName FROM Size")
ComboBox1.DisplayMember = "SizeName"
ComboBox1.ValueMember = "SizeId"
ComboBox2.Enabled = False
ComboBox3.Enabled = False
End Sub
Private Sub ComboBox1_SelectionChangeCommitted(sender As Object, e As EventArgs) Handles ComboBox1.SelectionChangeCommitted
ComboBox2.DataSource = Nothing
ComboBox3.DataSource = Nothing
ComboBox2.Enabled = False
ComboBox3.Enabled = False
If ComboBox1.SelectedValue.ToString() <> "0" Then
Dim sql As String = String.Format("SELECT DetailLevelId, DetailLevelName FROM DetailLevel WHERE SizeId = {0}", ComboBox1.SelectedValue)
ComboBox2.DataSource = Me.GetData(sql)
ComboBox2.DisplayMember = "DetailLevelName"
ComboBox2.ValueMember = "DetailLevelId"
ComboBox2.Enabled = True
End If
End Sub
Private Sub ComboBox2_SelectionChangeCommitted(sender As Object, e As EventArgs) Handles ComboBox2.SelectionChangeCommitted
ListBox1.DataSource = Nothing
ListBox1.Enabled = False
If ComboBox2.SelectedValue.ToString() <> "0" Then
Dim sql As String = String.Format("SELECT OperationsId, OperationsName, OperationsTime FROM Operations WHERE DetailLevelId = {0}", ComboBox2.SelectedValue)
ListBox1.DataSource = Me.GetData(sql)
ListBox1.ValueMember = "OperationsName"
ListBox1.ValueMember = "OperationsTime"
ListBox1.Enabled = True
Label9.Text = CType(ListBox1.SelectedValue, Integer).ToString
'Label.Text = CType(cbbank.SelectedItem, DataRowView).Row.Item("Account").ToString
End IF
End Sub
Dim totalOperationsTime As Double
For Each view As DataRowView In ListBox1.SelectedItems
totalOperationsTime += CDbl(view("OperationsTime"))
Next
There's no need to get the DataRow from the DataRowView because you can access the field values directly from the DataRowView. It can and does do many of the same things that the DataRow does.
That's the most conventional way but there are other options too. You could throw some LINQ at it:
Dim totalOperationsTime = ListBox1.SelectedItems.Cast(Of DataRowView)().
Sum(Function(view) CDbl(view("OperationsTime")))
It is somewhat annoying that the ValueMember property only helps you get a value for the SelectedItem. Here's a class I wrote some time ago that adds a GetItemValue method that makes use of the ValueMember much as the GetItemText method does for the DisplayMember:
Public Class ListBoxEx
Inherits ListBox
Public Function GetItemValue(item As Object) As Object
Dim index = Me.Items.IndexOf(item)
If (index <> -1 AndAlso Me.DataManager IsNot Nothing) Then
Return Me.FilterItemOnProperty(Me.DataManager.List(index), Me.ValueMember)
End If
Return Nothing
End Function
End Class
If you use that control instead of a regular ListBox then you can do this:
Dim totalOperationsTime As Double
For Each item In ListBoxEx1.SelectedItems
totalOperationsTime += CDbl(ListBoxEx1.GetItemValue(item))
Next
or this:
Dim totalOperationsTime = ListBox1.SelectedItems.Cast(Of Object)().
Sum(Function(item) CDbl(ListBoxEx1.GetItemValue(item)))
One advantage of using that custom control is that you don't have to know what type the data source or its items are. You only have to know that the ValueMember has been set.
I made a few changes to your code. It works with a ListBox. See comments for details.
' "Please Select" doesn't work well in the ListBox, I added it as an option
Private Shared Function GetData(ByVal sql As String, insertPleaseSelect As Boolean) As DataTable
Dim constr As String = "Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\hartj\Documents\visual studio 2015\Projects\TIMEMATRIX2.0\TIMEMATRIX2.0\TMX.mdf;Integrated Security=True"
Using con As SqlConnection = New SqlConnection(constr)
Using sda As SqlDataAdapter = New SqlDataAdapter(sql, con)
Dim dt As DataTable = New DataTable()
sda.Fill(dt)
If insertPleaseSelect Then
Dim row As DataRow = dt.NewRow()
row(0) = 1
row(1) = "Please Select"
dt.Rows.InsertAt(row, 0)
End If
Return dt
End Using
End Using
End Function
Private Sub MainHome_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
ComboBox1.DataSource = GetData("SELECT [SizeId], [SizeName] FROM [Size]", True)
ComboBox1.DisplayMember = "SizeName"
ComboBox1.ValueMember = "SizeId"
ComboBox2.Enabled = False
ListBox1.SelectionMode = SelectionMode.MultiSimple ' allow multi-select
End Sub
Private Sub ComboBox1_SelectionChangeCommitted(sender As Object, e As EventArgs) Handles ComboBox1.SelectionChangeCommitted
ComboBox2.DataSource = Nothing
ComboBox2.Enabled = False
If ComboBox1.SelectedValue.ToString() <> "0" Then
Dim sql As String = String.Format("SELECT [DetailLevelId], [DetailLevelName] FROM [DetailLevel] WHERE [SizeId] = {0}", ComboBox1.SelectedValue)
ComboBox2.DataSource = GetData(sql, True)
ComboBox2.DisplayMember = "DetailLevelName"
ComboBox2.ValueMember = "DetailLevelId"
ComboBox2.Enabled = True
End If
End Sub
Private Sub ComboBox2_SelectionChangeCommitted(sender As Object, e As EventArgs) Handles ComboBox2.SelectionChangeCommitted
ListBox1.DataSource = Nothing
ListBox1.Enabled = False
If ComboBox2.SelectedValue.ToString() <> "0" Then
Dim sql As String = String.Format("SELECT [OperationsId], [OperationsName], [OperationsTime] FROM [Operations] WHERE [DetailLevelId] = {0}", ComboBox2.SelectedValue)
ListBox1.DataSource = GetData(sql, False)
ListBox1.DisplayMember = "OperationsName" ' changed this from ValueMember to DisplayMember
ListBox1.ValueMember = "OperationsTime"
ListBox1.Enabled = True
ListBox1.ClearSelected() ' Every time the ListBox is populated, clear it
End If
End Sub
' Added this handler to respond to user input, not programmatic selection changes
Private Sub ListBox1_Click(sender As Object, e As EventArgs) Handles ListBox1.Click
' Here is the sum
Label9.Text = ListBox1.SelectedItems.OfType(Of DataRowView).Sum(Function(o) CType(o("OperationsTime"), Double))
End Sub

How to increase Input box font size in VB.Net 2012

I am using VB2012, How can I increase the input box font size?
Dim inputBoxMessage As String = "You are picking part : "
For count = 0 To dgv_partDetails.Rows.Count - 1
If dgv_partDetails.Rows.Count = 0 Then
The following assumes you want to ask for user input outside the DataGridView. If this is not the case stop here.
Create a new form, add two button, set DialogResult for one to OK and the other to Cancel, do this in the property window for both buttons. Add a Label (set autosize to false) and TextBox, set the font to how you want them. Set FormBorderStyle = FixedToolWindow, set StartPosition to CenterParent.
Call the dialog as shown below
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim f As New frmUserInput
Dim Value As Integer = 0
Try
f.lblQuestion.Text = "your question"
f.Text = "Question"
' optional
f.txtInput.Text = "100"
If f.ShowDialog = Windows.Forms.DialogResult.OK Then
If Not String.IsNullOrWhiteSpace(f.txtInput.Text) Then
' Work with data, lets say we want a numeric
If Integer.TryParse(f.txtInput.Text, Value) Then
' use value varible
Else
MessageBox.Show("Not a valid integr [" & f.txtInput.Text & "]")
End If
End If
End If
Finally
f.Dispose()
End Try
End Sub
End Class
Or we can set properties on creation of the form
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim f As New frmUserInput
Dim Value As Integer = 0
Try
Dim specialFont As Font = New Font(
"Microsoft Sans Serif", 12.0!,
System.Drawing.FontStyle.Regular,
System.Drawing.GraphicsUnit.Point,
CType(0, Byte))
f.lblQuestion.Text = "your question"
f.lblQuestion.Font = specialFont
f.lblQuestion.ForeColor = System.Drawing.Color.Red
f.Text = "Question"
' optional
f.txtInput.Text = "100"
If f.ShowDialog = Windows.Forms.DialogResult.OK Then
If Not String.IsNullOrWhiteSpace(f.txtInput.Text) Then
' Work with data, lets say we want a numeric
If Integer.TryParse(f.txtInput.Text, Value) Then
' use value varible
Else
MessageBox.Show("Not a valid integr [" & f.txtInput.Text & "]")
End If
End If
End If
Finally
f.Dispose()
End Try
End Sub
End Class
Personally I would wrap this code into a class or code module rather than in a form so it can be used in more than one spot in your code.

Programmatically add, remove columns, rows in datagridview which was created programmatically

HI their i am trying to create a dynamic text editor which will have tabs and option to add, remove datagridviews using buttons. Also in datagridviews, columns and rows could be added programmatically. I have reached till following code:
Private Sub Button3_Click(sender As System.Object, e As System.EventArgs) Handles Button3.Click
Dim tbpg As TabPage = TabControl1.SelectedTab
Dim dgview As New DataGridView
Controls.Add(dgview)
dgview.Location = New Point(5, 5)
dgview.Size = New Size(250, 250)
dgview.ColumnCount = 5
dgview.RowCount = 5
tbpg.Controls.Add(dgview)
End Sub
also
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
If RadioButton1.Checked = True Then
Dim clm As New DataGridViewTextBoxColumn
clm.DataPropertyName = "Property Name"
clm.HeaderText = "Header Name"
clm.Name = "Column Name"
DataGridView1.Columns.Add(clm)
ElseIf RadioButton2.Checked = True Then
DataGridView1.Rows.Add()
End If
End Sub
The problem is initially there is no datagridview, so the line DataGridView1.Columns.Add(clm) gives error. So i think there should be a currentdatagridview or selecteddatagridview property.
Any help appreciated.
You can change the scope of the initial declaration and make it private to your form.
Private dgview As DataGridView
Then when you create it, refer to the local private
dgview = New DataGridView
Lastly in your column creation, just keep referring to dgview
dgview.Columns.Add()
That should cover your requirement.
Full code sample:
Public Class Form1
Private dgView As DataGridView
Private Sub createDGV()
dgView = New DataGridView
TabControl1.SelectedTab.Controls.Add(dgView)
dgView.Location = New Point(5, 5)
dgView.Size = New Size(250, 250)
dgView.ColumnCount = 5
dgView.RowCount = 5
End Sub
Private Sub addColumn()
Dim thisDGV As DataGridView = findMyDGV()
If RadioButton1.Checked = True Then
Dim clm As DataGridViewTextBoxColumn = New DataGridViewTextBoxColumn
clm.DataPropertyName = "Property Name"
clm.HeaderText = "Header Name"
clm.Name = "Column Name"
thisDGV.Columns.Add(clm)
ElseIf RadioButton2.Checked = True Then
thisdgv.Rows.Add()
End If
End Sub
Private Function findMyDGV() As DataGridView
For Each ctrl As Control In TabControl1.SelectedTab.Controls
If TypeOf ctrl Is DataGridView Then
Return ctrl
End If
Next
End Function
Private Sub TestDGVButton_Click(sender As System.Object, e As System.EventArgs) Handles TestButton.Click
Try
createDGV()
addColumn()
Catch ex As Exception
MessageBox.Show(String.Concat("An error occurred: ", ex.Message))
End Try
End Sub
End Class

VB.net adding multiple controls on button click

I'm trying to make a userinterface that generates itself on request (button click)
Private Sub Body_new_part_add_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Body_new_part_add.Click
So when i add a Combobox as first it's no problem it generates the box & places it on the right position etc.
Dim oTypeBox As New ComboBox
oTypeBox.Name = "Body_type_" & oBodyPartsNumber
oTypeBox.Location = New System.Drawing.Point(7, 78)
Body_parts.Controls.Add(oTypeBox)
Now i want to add another control, a textbox next to the Combobox.
Dim oTypeBox As New ComboBox
oTypeBox.Name = "Body_type_" & oBodyPartsNumber
oTypeBox.Location = New System.Drawing.Point(7, 78)
Body_parts.Controls.Add(oTypeBox)
Dim oTextbox As New TextBox
oTextbox.name = "test"
oTextbox.Location = New System.Drawing.Point(50, 78)
Body_parts.Controls.Add(oTextbox)
This gives me this error.
'New' cannot be used on an interface.
What do i need to change in order to get this done? I need to add +- 10 controls on each button click event.
Try this one
Public Class Form1
Dim cLeft As Integer = 1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
AddNewTextBox()
End Sub
Public Function AddNewTextBox() As System.Windows.Forms.TextBox
Dim txt As New System.Windows.Forms.TextBox()
Me.Controls.Add(txt)
txt.Top = cLeft * 25
txt.Left = 100
txt.Text = "TextBox " & Me.cLeft.ToString
cLeft = cLeft + 1
Return txt
End Function
End Class

Runtime datatable update error

I have a datagridview with a checkbox column. When I click(check/Uncheck) on checkbox field as random, two other fields in corrensponding row should be added OR removed in a datatable (declared runtime).
So that I can do some procedures with data in the datatable.
For that I have declared a datatable as global.
Now the problem is, each time when I click on a checkbox, a simple mouse scrolling is required to update datatable, OR a click needed in the new datagridview which is showing values in the datatable.
My code given below,
global declaration: Public PaymentTable As DataTable
Private Sub ShowOrdersFrm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
DataBind()
Me.DGVOrders.RowsDefaultCellStyle.BackColor = Color.GhostWhite
Me.DGVOrders.AlternatingRowsDefaultCellStyle.BackColor = Color.PaleGoldenrod
PaymentTable = New DataTable()
PaymentTable.Columns.Add("RowId", GetType(Integer))
PaymentTable.Columns.Add("Amount", GetType(Decimal))
End Sub
Private Sub DataBind()
DGVOrders.DataSource = Nothing
DGVOrders.Columns.Clear()
Dim cmd As New SqlCommand
Dim da As New SqlDataAdapter
Dim dt As New DataTable
con.Open()
With cmd
.Connection = con
.CommandText = "select * from VIEW_PAYMENTS_DUES_BYORDER where CustCode='" & CustCode & "' order by OrderNo DESC"
End With
da.SelectCommand = cmd
da.Fill(dt)
BindingSource1.DataSource = dt
DGVOrders.DataSource = BindingSource1
DGVOrders.ClearSelection()
con.Close()
DGVOrders.Columns(0).Visible = False
DGVOrders.Columns(1).HeaderText = "Order No"
DGVOrders.Columns(2).HeaderText = "Cust Code"
DGVOrders.Columns(3).HeaderText = "Name"
DGVOrders.Columns(4).HeaderText = "Order Date"
DGVOrders.Columns(5).HeaderText = "Order Price"
DGVOrders.Columns(6).HeaderText = "Total Payment"
DGVOrders.Columns(7).HeaderText = "Dues"
For i = 0 To DGVOrders.RowCount - 1
If DGVOrders.Rows(i).Cells(7).Value > 0 Then
DGVOrders.Rows(i).Cells(7).Style.ForeColor = System.Drawing.Color.Red
Else
DGVOrders.Rows(i).Cells(7).Style.ForeColor = System.Drawing.Color.Green
End If
Next
' CHECK BOX
Dim colmnchk As New DataGridViewCheckBoxColumn
colmnchk.DataPropertyName = "chkSelect"
colmnchk.HeaderText = "SELECT"
colmnchk.Name = "chkSelect"
DGVOrders.Columns.Add(colmnchk)
For i = 0 To DGVOrders.RowCount - 1
Next
'CHECK BOX END
End Sub
Private Sub DGVOrders_CellValueChanged(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DGVOrders.CellValueChanged
If DGVOrders.Columns(e.ColumnIndex).Name = "chkSelect" Then
Dim checkCell As DataGridViewCheckBoxCell = _
CType(DGVOrders.Rows(e.RowIndex).Cells("chkSelect"), _
DataGridViewCheckBoxCell)
If checkCell.Value = True Then
PaymentTable.Rows.Add(DGVOrders.Rows(e.RowIndex).Cells(0).Value, DGVOrders.Rows(e.RowIndex).Cells(7).Value)
Else
Dim toRemoveID As Integer = DGVOrders.Rows(e.RowIndex).Cells(0).Value
For i = 0 To PaymentTable.Rows.Count - 1
If PaymentTable.Rows(i).Item(0) = toRemoveID Then
PaymentTable.Rows(i).Delete()
Exit Sub
End If
Next
End If
DataGridView1.DataSource = PaymentTable
End If
End Sub
Can sombody to solve the issue, or is there any other good method if my code is wrong ?
Try using a different event like CellContentClick and testing for the ColumnIndex. The drawback with this is the event will fire whenever you click on any cell.
Private Sub DGVOrders_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles DGVOrders.CellContentClick
'Only interested in the CheckBox column
If e.ColumnIndex = colmnchk.Index Then
Debug.WriteLine("In DGVOrders_CellContentClick for the checkbox")
End If
End Sub