Runtime datatable update error - vb.net

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

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

Auto-fill textbox on a dialog form, from a Datagridview on the original form, vb.net 2013

I am currently working in windows form applications using vb.net 2013. I have two forms, we can call them form1 and form 2 for now. Form1 has a datagridview with a checkbox column that the end user will click to open form2 as a dialog form. Once form2 opens I want it to automatically load and fill two text boxes that have information from corresponding columns from the original DGV. In short, the DGV on form1 has 3 columns; JobNumber, LineNumber, and the checkbox. The end user will click the checkbox to bring up form2 and I want the Jobnumber and Linenumber to automaticaly fill into two textboxes on form2. Here is my code from form1.
'assembly dialog result form
dr = f.ShowDialog
If dr = Windows.Forms.DialogResult.OK Then
'dim y as job string
Dim Y As String = DataGridOrdered.Rows(e.RowIndex).Cells(3).Value
'open connection
Using conn1 As New SqlConnection(connstring)
conn1.Open()
Using comm1 As SqlCommand = New SqlCommand("UPDATE Production.dbo.tblFCOrdered SET Complete = 1, RackIn = 1 WHERE JobNumber = '" & Y & "'", conn1)
comm1.ExecuteNonQuery()
conn1.Close()
End Using
End Using
Call DGVOrderedRefresh()
ElseIf dr = Windows.Forms.DialogResult.Yes Then
'dim M as job string
Dim M As String = DataGridOrdered.Rows(e.RowIndex).Cells(3).Value
'open connection
Using conn1 As New SqlConnection(connstring)
conn1.Open()
Using comm1 As SqlCommand = New SqlCommand("UPDATE Production.dbo.tblFCOrdered SET Complete = 1, RackIn = 1 WHERE JobNumber = '" & M & "'", conn1)
comm1.ExecuteNonQuery()
conn1.Close()
End Using
End Using
Call DGVOrderedRefresh()
ElseIf dr = Windows.Forms.DialogResult.Cancel Then
'refresh datagridview ordered
Call DGVOrderedRefresh()
End If
ElseIf e.ColumnIndex = 0 Then
'fail safe to make sure the header is not clicked
If e.RowIndex = -1 Then
Exit Sub
End If
The code for my dialog is as follows
Imports System.Data
Imports System.Data.SqlClient
Public Class BuildName
' Dim connstring As String = "DATA SOURCE = BNSigma\CORE; integrated security = true"
Dim connstring As String = "DATA SOURCE = BNSigma\TEST; integrated security = true"
Private Sub Label3_Click(sender As Object, e As EventArgs) Handles Label3.Click
End Sub
Private Sub BuildName_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim ds As New DataTable
Try
'load name combo box
Using conn1 As New SqlConnection(connstring)
conn1.Open()
Using comm1 As SqlCommand = New SqlCommand("SELECT Name FROM Production.dbo.FCNames", conn1)
Dim adapater As New SqlDataAdapter
adapater.SelectCommand = comm1
adapater.Fill(ds)
adapater.Dispose()
conn1.Close()
CBName.DataSource = ds
CBName.DisplayMember = "Name"
CBName.ValueMember = "Name"
End Using
End Using
Catch ex As Exception
MsgBox("Error loading name List, please contact a mfg. Engr.!")
MsgBox(ex.ToString)
End Try
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
'Update built by name
Dim v As Object = TBFloor.Text
Dim G As Object = TBLine.Text
Dim O As Object = TBJobNumber.Text
Try
Using conn1 As New SqlConnection(connstring)
conn1.Open()
Using comm1 As SqlCommand = New SqlCommand("UPDATE Production.dbo.tblFCOrdered SET BuiltBy = #Name Where JobNumber = '" & O & "'", conn1)
comm1.Parameters.AddWithValue("#Name", CBName.Text)
comm1.ExecuteNonQuery()
conn1.Close()
End Using
End Using
Catch ex As Exception
MsgBox("Error updating Ordered table, please contact a MFG. ENGR.!")
MsgBox(ex.ToString)
End Try
End Sub
End Class
UPDATED CODE FOR VALTER
Form1 code
Public row_Index As Integer = 0
Private Sub DataGridOrdered_CurrentCellDirtyStateChanged(sender As Object, e As EventArgs) Handles DataGridOrdered.CurrentCellDirtyStateChanged
If DataGridOrdered.IsCurrentCellDirty Then
DataGridOrdered.CommitEdit(DataGridViewDataErrorContexts.Commit)
End If
End Sub
Private Sub DataGridOrdered_CellValueChanged(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridOrdered.CellValueChanged
If DataGridOrdered.Columns(e.ColumnIndex).Name = 9 Then
Dim checkCell As DataGridViewCheckBoxCell = CType(DataGridOrdered.Rows(e.RowIndex).Cells(9), DataGridViewCheckBoxCell)
If CType(checkCell.Value, [Boolean]) = True Then
row_Index = e.RowIndex
BuildName.ShowDialog(Me)
End If
End If
End Sub
Form 2 code
Private Sub BuildName_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim value1 As Object = FormOrdered.DataGridOrdered.Rows(FormOrdered.row_Index).Cells(3).Value
Dim value2 As Object = FormOrdered.DataGridOrdered.Rows(FormOrdered.row_Index).Cells(4).Value
TBJobNumber.Text = CType(value1, String)
TBFloor.Text = CType(value2, String)
In your form1 add:
Public Row_Index As Integer = 0 //use a simple variable
Sub datagridOrdered_CurrentCellDirtyStateChanged( _
ByVal sender As Object, ByVal e As EventArgs) _
Handles datagridOrdered.CurrentCellDirtyStateChanged
If datagridOrdered.IsCurrentCellDirty Then
datagridOrdered.CommitEdit(DataGridViewDataErrorContexts.Commit)
End If
End Sub
Private Sub datagridOrdered_CellValueChanged(sender As System.Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles datagridOrdered.CellValueChanged
If datagridOrdered.Columns(e.ColumnIndex).Name = "Assemble" Then //<- here
Dim checkCell As DataGridViewCheckBoxCell = _
CType(datagridOrdered.Rows(e.RowIndex).Cells(2), _ //<- here
DataGridViewCheckBoxCell)
If CType(checkCell.Value, [Boolean]) = True Then
//RowIndex = e.RowIndex you dont need this
Row_Index = e.RowIndex
BuildName.ShowDialog(Me)
End If
End If
End Sub
//Nor this
//Private _count As Integer
//Public Property RowIndex() As Integer
//Get
//Return _count
//End Get
//Set(value As Integer)
//_count = value
//End Set
//End Property
And in dialog load event use Form1.Row_Index instead:
Private Sub BuildName_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim value1 As Object = FormOrdered.datagridOrdered.Rows(Form1.Row_Index).Cells(0).Value //<- here
Dim value2 As Object = FormOrdered.datagridOrdered.Rows(Form1.Row_Index).Cells(1).Value //<- here
TBJobNumber.Text = CType(value1, String)
TBFloor.Text = CType(value2, String)
...
...
End Sub
or add a module and add the Row_Index there. You can use it then as is
Private Sub BuildName_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim value1 As Object = FormOrdered.DataGridView1.Rows(Row_Index).Cells(0).Value
Dim value2 As Object = FormOrdered.DataGridView1.Rows(Row_Index).Cells(1).Value
TBJobNumber.Text = CType(value1, String)
TBFloor.Text = CType(value2, String)
...
...
End Sub

looping through all records on the database

i revised the code a little from a friend
but it only display the first record how will i able to manipulate to loop through all records
anyone who could give correct answer in code will receive bounty thanks
Note: DisplayOfficeEquipmentList() is a sub that displays data on the database to the textboxes and comboboxes
Public Sub DisplayOfficeEquipmentList()
Dim sqlconn As New SqlClient.SqlConnection
sqlconn.ConnectionString = "server = SKPI-APPS1;" & _
"Database = EOEMS;integrated security=true"
Dim dt As New DataTable
sqlconn.Open()
Dim da As New SqlDataAdapter("SELECT * FROM tblOfficeEquipmentProfile", sqlconn)
da.Fill(dt)
cmbCategory.Text = dt.Rows(0)("OE_Category").ToString()
cmbSubCategory.Text = dt.Rows(0)("OE_SubCategory").ToString()
txtOEID.Text = dt.Rows(0)("OE_ID").ToString()
txtName.Text = dt.Rows(0)("OE_Name").ToString()
txtUser.Text = dt.Rows(0)("OE_User").ToString()
cmbBrand.Text = dt.Rows(0)("OE_Brand").ToString()
cmbModel.Text = dt.Rows(0)("OE_Model").ToString()
txtSpecs.Text = dt.Rows(0)("OE_Specs").ToString()
txtSerialNo.Text = dt.Rows(0)("OE_SerialNo").ToString()
txtPropertyNo.Text = dt.Rows(0)("OE_PropertyNo").ToString()
txtMacAddress.Text = dt.Rows(0)("OE_MacAddress").ToString()
txtStaticIP.Text = dt.Rows(0)("OE_Static_IP").ToString()
txtVendor.Text = dt.Rows(0)("OE_Vendor").ToString()
dtpPurchaseDate.Text = dt.Rows(0)("OE_PurchaseDate").ToString()
txtWarrantyStatus.Text = dt.Rows(0)("OE_WarrantyStatus").ToString()
txtWarrantyInclusiveYear.Text = dt.Rows(0)("OE_WarrantyInclusiveYear").ToString()
txtStatus.Text = dt.Rows(0)("OE_Status").ToString()
cmbDeptCode.Text = dt.Rows(0)("OE_Dept_Code").ToString()
cmbLocationCode.Text = dt.Rows(0)("OE_Location_Code").ToString()
txtRemarks.Text = dt.Rows(0)("OE_Remarks").ToString()
sqlconn.Close()
End Sub
Private Sub btnNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNext.Click
Private Sub btnPrevious_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrevious.Click
End Sub
You have to loop the lines, you are getting the values of the row at index 0( Just one row).
Use foreach:
foreach (System.Data.DataRow row in dt.Rows)
{
//Get values of row
}
EDIT: In vb.net it would be something like this:
For Each filarow As DataRow In dt.Rows
Dim OE_ID As String = filarow("OE_ID").ToString
Dim txtName As String = filarow("OE_NAME").ToString
Next
By the way, it seems you are filling textboxes, so the values will change at the next loop. Maybe you should use another control like a ListBox
You need to separate the logic that retrieve data from the logic that shows that data.
First add a method that load your datatable
Private Function LoadData() as DataTable
Using sqlconn = New SqlClient.SqlConnection("server = SKPI-APPS1;" & _
"Database = EOEMS;integrated security=true")
Dim dt As New DataTable
sqlconn.Open()
Dim da As New SqlDataAdapter("SELECT * FROM tblOfficeEquipmentProfile", sqlconn)
da.Fill(dt)
return dt
End Using
End Function
Then in the buttons click pass the datatable and the rownumber to display to the DisplayOfficeEquipmentList
Private Sub btnNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNext.Click
if currentRow + 1 >= dt.Rows.Count Then
Return
End if
currentRow = currentRow + 1
DisplayOfficeEquipmentList(dt, currentRow)
End Sub
Private Sub btnPrevious_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNext.Click
if currentRow - 1 < 0 Then
Return
End if
currentRow = currentRow - 1
DisplayOfficeEquipmentList(dt, currentRow)
End Sub
and in the DisplayOfficeEquipmentList refer to the row passed by the buttons click
Public Sub DisplayOfficeEquipmentList(ByRef dt as DataTable, ByVal rowNum as INteger)
Dim row as DataRow
row = dt.Rows(rowNum)
cmbCategory.Text = row("OE_Category").ToString()
cmbSubCategory.Text = row("OE_SubCategory").ToString()
....
End Sub
For this to work, you need to call the LoadData somewhere when you show your form (Load event?) and you should set the currentRow as a form global level variable

How to get the value of DataGridView.SelectedRows().Cells() from different forms?

I have 2 forms and within each of the 2 forms there is a DataGridView(chatform and prodetail).
In the chatform I created a DataGridView which has a generated Button in each row.
Each Button when clicked will load a prodetail form and when in the prodetail form I want to get the value of the SelectedRow.Cell from the DataGridView in the originating chatform.
Code (chatform):
Public Sub loadtoDGV()
Dim sqlq As String = "SELECT * FROM chattbl"
Dim sqlcmd As New SqlCommand
Dim sqladpt As New SqlDataAdapter
Dim tbl As New DataTable
With sqlcmd
.CommandText = sqlq
.Connection = conn
End With
With sqladpt
.SelectCommand = sqlcmd
.Fill(tbl)
End With
DataGridView1.Rows.Clear()
For i = 0 To tbl.Rows.Count - 1
With DataGridView1
.Rows.Add(tbl.Rows(i)("Username"), tbl.Rows(i)("Title"), tbl.Rows(i)("ChatDateTime"))
End With
Next
conn.Close()
End Sub
Private Sub ChatForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
loadtoDGV()
End Sub
Code (DataGridView1.CellContentClick):
Private Sub grdData_CellContentClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick
Dim colName As String = DataGridView1.Columns(e.ColumnIndex).Name
If colName = "Detail" Then
Prodetail.Show()
MessageBox.Show(String.Format("You clicked the button in row {0} of the Detail column", e.RowIndex))
End If
End Sub
Code (prodetail):
Public Sub loadtoDGV2()
Dim i As Integer
i = ChatForm.DataGridView1.SelectedRows.Count
MsgBox(i)
Dim compareai As String = ChatForm.DataGridView1.SelectedRows(i).Cells(1).Value
Dim sqlq As String = "SELECT * FROM Chattbl WHERE Title =" & compareai & ""
Dim sqlcmd As New SqlCommand
Dim sqladpt As New SqlDataAdapter
Dim tbl As New DataTable
With sqlcmd
.CommandText = sqlq
.Connection = conn
End With
With sqladpt
.SelectCommand = sqlcmd
.Fill(tbl)
End With
DataGridView1.Rows.Clear()
For i = 0 To tbl.Rows.Count - 1
With DataGridView1
.Rows.Add(tbl.Rows(i)("Username"), tbl.Rows(i)("Title"), tbl.Rows(i)("ChatDateTime"), tbl.Rows(i)("ChatContent"))
End With
Next
conn.Close()
End Sub
Private Sub Prodetail_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
loadtoDGV2()
End Sub
What have I done wrong?
I tried to use MsgBox(i) i = SelectedRow(0) assuming it would show the data for first row, but DataGridView1 in prodetail does not load any data from the database.
I did not observe any errors, I just don't have a solution.
The first problem is you are calling the class instead of an instance. VB.NET will allow you to call an instance of the form as its name, but it will be the same instance across every use. I would not suggest doing this.
To start I would change this:
Private Sub grdData_CellContentClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick
Dim colName As String = DataGridView1.Columns(e.ColumnIndex).Name
If colName = "Detail" Then
Prodetail.Show()
MessageBox.Show(String.Format("You clicked the button in row {0} of the Detail column", e.RowIndex))
End If
End Sub
To this:
Private Sub grdData_CellContentClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick
Dim colName As String = DataGridView1.Columns(e.ColumnIndex).Name
If colName = "Detail" Then
Dim newDetailForm as new Proddetail(dataGridView1.Rows(e.RowIndex).Cells(1).Value)
newDetailForm.show()
MessageBox.Show(String.Format("You clicked the button in row {0} of the Detail column", e.RowIndex))
End If
End Sub
Then in the Proddetail class you need to add a constructor and a member like this:
Private SearchValue as String
Public Sub New(byval theSearchValue as string)
InitalizeComponent()
SearchValue = theSearchValue
End Sub
Then in your load routine:
Public Sub loadtoDGV2()
Dim sqlq As String = "SELECT * FROM Chattbl WHERE Title =" & SearchValue & ""
Dim sqlcmd As New SqlCommand
Dim sqladpt As New SqlDataAdapter
Dim tbl As New DataTable
With sqlcmd
.CommandText = sqlq
.Connection = conn
End With
With sqladpt
.SelectCommand = sqlcmd
.Fill(tbl)
End With
DataGridView1.Rows.Clear()
For i = 0 To tbl.Rows.Count - 1
With DataGridView1
.Rows.Add(tbl.Rows(i)("Username"), tbl.Rows(i)("Title"), tbl.Rows(i)("ChatDateTime"), tbl.Rows(i)("ChatContent"))
End With
Next
conn.Close()
End Sub
This should then display the details of the clicked row in a new instance of the Proddetail class.
I added a custom paramaterized constructor to the class that takes the value for the SQL query string. This way when you create a new instance of the form in your code, you can always pass in the search string that would lead to the details you want to view.

conversion from string to type boolean is not valid

The error occurs on the line If wba.Selected = "MARKETING CODE" Then ... Hopefully someone might be able to help. Thanks
'Private Sub NewRecord_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
markCodes = New DataGridViewComboBoxColumn()
markCodes.Name = "Marketing Codes"
Me.DataGridView1.Columns.Add(markCodes)
markCodes.Visible = False
End Sub
'Private Sub DataGridView1_EditingControlShowing(ByVal sender As Object, ByVal e As DataGridViewEditingControlShowingEventArgs)
If DataGridView1.CurrentCell.ColumnIndex = DataGridView1.Columns(wba.Name).Index Then
If TypeOf e.Control Is ComboBox Then
cbxWba = TryCast(e.Control, ComboBox)
RemoveHandler cbxWba.SelectionChangeCommitted, New EventHandler(AddressOf cbxWba_SelectionChangeCommitted)
AddHandler cbxWba.SelectionChangeCommitted, New EventHandler(AddressOf cbxWba_SelectionChangeCommitted) 'this event will fire up if there's a selected item.
End If
End If
End Sub
'Private Sub cbxWba_SelectionChangeCommitted(ByVal sender As Object, ByVal e As EventArgs)
loadDescriptions()
End Sub
Private Sub loadDescriptions()
If wba.Selected = "MARKETING CODE" Then
markCodes.Visible = True
Try
Dim con As New SqlConnection
con.ConnectionString = ""
Dim myCommand1 As New SqlClient.SqlCommand
Dim myAdapter1 As New SqlClient.SqlDataAdapter
Dim sql As String = "Select analysis_a + ' - ' + [desc] as Expr1 from marketingCode"
Dim ds As New DataSet
myCommand1.Connection = con
myCommand1.CommandText = sql
myAdapter1.SelectCommand = myCommand1
myCommand1.Connection.Open()
myAdapter1.Fill(ds)
myCommand1.Connection.Close()
Dim dt As New DataTable
dt = ds.Tables(0)
For Each row As DataRow In ds.Tables(0).Rows
DataGridView1.CurrentRow.Cells(description.Name).Value = row("Expr1").ToString()
markCodes.DataSource = dt
markCodes.DisplayMember = "Expr1"
markCodes.ValueMember = "Expr1"
Next
Catch
MsgBox("failed")
End Try
End If
End Sub
What is wba? The .Selected sounds like you are comparing a boolean property to an string.
Looking at your code, I assume that wba is a datagridviewrow, and then you cant use .Selected in that way.
To check for a value of the first selected cell in the datagridview:
With DataGridView1
If .SelectedCells.Count > 0 Then
If .SelectedCells(0).Value = "MARKETING CODE" Then
'we have a hit
End If
End If
End With
or, if you allready know what cell it is and has stored the selected cell in the wba-variable:
If wba.Value = "MARKETING CODE" Then
'we have a hit
End If
If you have selectionmode=fullrowselect and want to check if first cell in the first selected datagridviewrow has a value:
With DataGridView1
If .SelectedRows.Count > 0 Then
If .SelectedRows(0).Cells(0).Value = "MARKETING CODE" Then
'we have a hit
End If
End If
End With
If you have the row stored in the wba, and want to check if the first cell in that row has a specific value then its like this:
If wba.Cells(0).Value = "MARKETING CODE" Then
'we have a hit
End If