Visual basic 2010, database – how to fill specific field - vb.net

I'm creating database connection in Visual Basic. When user click on "borrow book" it transfer chosen data into table used for lent books. It all works alright, but I need last thing. In my database in lent table I have attribute "End date of lent" and I want fill it everytime when user borrow a book with date today + 1 month (for instance, if now is 19/04/2016, end date will be 19/05/2016). I created method "BorrowTime" for this, but I don't know what I should type into and how can I get value of inserted row into "row". And sorry for horrible look of code and form...
Public Class frm_UserView
Dim BooksSource As New BindingSource
Dim BorrowSource As New BindingSource
Dim BooksView As New DataView
Dim BorrowView As New DataView
Dim ds As New DataSet
Dim Borrow As Byte
Private Sub frm_UserView_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
tmr_Timer.Start()
Me.LoanerBooksTableAdapter.Fill(Me.GMITLibraryDataSet1.LoanerBooks)
Me.BooksTableAdapter.Fill(Me.GMITLibraryDataSet.Books)
BooksSource = dgd_UserBookView.DataSource
BooksView = CType(BooksSource.List, DataView)
ds = BooksView.DataViewManager.DataSet.Clone
BorrowSource.DataSource = ds
BorrowSource.DataMember = "Books"
BorrowView = CType(BorrowSource.List, DataView)
dgd_User_Borrow_View.DataSource = BorrowSource
End Sub
Private Sub btn_Borrow_Click(sender As System.Object, e As System.EventArgs) Handles btn_BorrowBook.Click
Borrow = 1
MoveBooks(dgd_UserBookView, BorrowView, Borrow)
dgd_User_Borrow_View.Sort(dgd_User_Borrow_View.Columns(0), System.ComponentModel.ListSortDirection.Ascending)
BorrowSource.MoveLast()
BorrowSource.MoveLast()
End Sub
Private Sub MoveBooks(ByRef source As DataGridView, ByRef target As DataView, ByRef borrow As Byte)
For i = 0 To source.SelectedRows.Count - 1
Dim numberOfRow As Integer
Dim row As DataRowView
row = target.AddNew()
Dim col As Int16 = 0
For Each cell As DataGridViewCell In source.SelectedRows(i).Cells
row.Item(col) = cell.Value
If borrow = 1 Then
numberOfRow = 0
BorrowTime(numberOfRow, dgd_User_Borrow_View)
End If
col = col + 1
Next
Next
Dim count As Int16 = source.SelectedRows.Count
For i = 0 To count - 1
source.Rows.RemoveAt(source.SelectedRows(0).Index)
Next
End Sub
Private Sub BorrowTime(ByRef row As Integer, ByRef dgd_Table As DataGridView)
'Code for adding date
End Sub
Private Sub btn_ReturnBook_Click(sender As System.Object, e As System.EventArgs) Handles btn_ReturnBook.Click
Borrow = 0
MoveBooks(dgd_User_Borrow_View, BooksView, Borrow)
dgd_UserBookView.Sort(dgd_UserBookView.Columns(0), System.ComponentModel.ListSortDirection.Ascending)
BooksSource.MoveLast()
BooksSource.MoveLast()
End Sub
Private Sub btn_UserLogOut_Click(sender As System.Object, e As System.EventArgs) Handles btn_UserLogOut.Click
frm_Login.Show()
Me.Hide()
End Sub
Private Sub Timer1_Tick(sender As System.Object, e As System.EventArgs) Handles tmr_Timer.Tick
Dim countBooks As Integer
Dim countLent As Integer
countBooks = BooksSource.Count
countLent = BorrowSource.Count
lbl_BookCounter.Text = "There are " + countBooks.ToString + " books"
lbl_BorrowCounter.Text = "You have lent " + countLent.ToString + " books"
End Sub
End Class
I need to put code here:
Private Sub BorrowTime(ByRef row As Integer, ByRef dgd_Table As DataGridView)
'Code for adding date
End Sub
My form

Public Class frm_UserView
Dim BooksSource As New BindingSource
Dim BorrowSource As New BindingSource
Dim BooksView As New DataView
Dim BorrowView As New DataView
Dim ds As New DataSet
Dim Borrow As Byte
Private Sub frm_UserView_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
tmr_Timer.Start()
Me.LoanerBooksTableAdapter.Fill(Me.GMITLibraryDataSet1.LoanerBooks)
Me.BooksTableAdapter.Fill(Me.GMITLibraryDataSet.Books)
BooksSource = dgd_UserBookView.DataSource
BooksView = CType(BooksSource.List, DataView)
ds = BooksView.DataViewManager.DataSet.Clone
BorrowSource.DataSource = ds
BorrowSource.DataMember = "Books"
BorrowView = CType(BorrowSource.List, DataView)
dgd_User_Borrow_View.DataSource = BorrowSource
End Sub
Private Sub btn_Borrow_Click(sender As System.Object, e As System.EventArgs) Handles btn_BorrowBook.Click
Borrow = 1
MoveBooks(dgd_UserBookView, BorrowView, Borrow)
dgd_User_Borrow_View.Sort(dgd_User_Borrow_View.Columns(0), System.ComponentModel.ListSortDirection.Ascending)
BorrowSource.MoveLast()
BorrowSource.MoveLast()
End Sub
Private Sub MoveBooks(ByRef source As DataGridView, ByRef target As DataView, ByRef borrow As Byte)
For i = 0 To source.SelectedRows.Count - 1
Dim numberOfRow As Integer
Dim row As DataRowView
row = target.AddNew()
Dim col As Int16 = 0
For Each cell As DataGridViewCell In source.SelectedRows(i).Cells
row.Item(col) = cell.Value
If borrow = 1 Then
numberOfRow = 0
BorrowTime(numberOfRow, dgd_User_Borrow_View)
End If
col = col + 1
Next
Next
Dim count As Int16 = source.SelectedRows.Count
For i = 0 To count - 1
source.Rows.RemoveAt(source.SelectedRows(0).Index)
Next
End Sub
Private Sub BorrowTime(ByRef row As Integer, ByRef dgd_Table As DataGridView)
'Code for adding date
End Sub
Private Sub btn_ReturnBook_Click(sender As System.Object, e As System.EventArgs) Handles btn_ReturnBook.Click
Borrow = 0
MoveBooks(dgd_User_Borrow_View, BooksView, Borrow)
dgd_UserBookView.Sort(dgd_UserBookView.Columns(0), System.ComponentModel.ListSortDirection.Ascending)
BooksSource.MoveLast()
BooksSource.MoveLast()
End Sub
Private Sub btn_UserLogOut_Click(sender As System.Object, e As System.EventArgs) Handles btn_UserLogOut.Click
frm_Login.Show()
Me.Hide()
End Sub
Private Sub Timer1_Tick(sender As System.Object, e As System.EventArgs) Handles tmr_Timer.Tick
Dim countBooks As Integer
Dim countLent As Integer
countBooks = BooksSource.Count
countLent = BorrowSource.Count
lbl_BookCounter.Text = "There are " + countBooks.ToString + " books"
lbl_BorrowCounter.Text = "You have lent " + countLent.ToString + " books"
End Sub
End Class

Related

Datagridview filter from textbox

i have a vb.net datagridiew and i fill my datagridview add manuel rows
I read too many questions about this questions and answers, but i could't find my answer.
Dim I = .Rows.Add
.Rows(I).Cells(0).Value = "AA"
for example;
COLUMN1 COLUMN2
AA 1
BB 2
so i want filter datagridview from textbox1.text change
What i tried;
TryCast(dgv1.DataSource, DataTable).DefaultView.RowFilter = "COLUMN1 LIKE '%A%'"
Implementation as per comment by #Jimi
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
BindGrid()
End Sub
Private Sub BindGrid()
Dim dt As New DataTable
dt.Columns.Add("Column1", GetType(String))
dt.Columns.Add("Column2", GetType(Integer))
DataGridView1.DataSource = dt
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
TryCast(DataGridView1.DataSource, DataTable).DefaultView.RowFilter = "COLUMN1 LIKE '%A%'"
End Sub
Create a class which represents a single row.
Public Class MyRow
Public Property Name As String
Public Property Value As Integer
End Class
Set collection of rows to the DataSource fo the DataGridView
Private _rows As List(Of MyRow)
Private Sub Form_Load()
_rows = New List(Of MyRow) From
{
New MyRow With { .Name = "AA", .Value = 1 },
New MyRow With { .Name = "BB", .Value = 2 }
}
myDataGridView.DataSource = _rows
End Sub
Every time value changed in textbox filter collection and set filtered result to the DataSource
Private Sub TextChanged(sender As Object, e As EventArgs) Handles txt.TextChanged
Dim textbox = DirectCast(sender, TextBox)
myDataGridView.DataSource = _rows.
Where(Function(row) row.Name.Contains(textbox.Text)).
ToList()
End Sub
i added second datagridview because in one datagridview always zero record found
Sub DATA_TABLE()
Dim dt As DataTable = New DataTable
Dim dr As DataRow
For COL = 0 To EARSIV_TABLO.ColumnCount - 1
dt.Columns.Add(EARSIV_TABLO.Columns(COL).HeaderText)
Next
For i = 0 To EARSIV_TABLO.RowCount - 1
dr = dt.NewRow()
For COL = 0 To EARSIV_TABLO.ColumnCount - 1
dr(COL) = EARSIV_TABLO.Rows(i).Cells(COL).Value
Next
dt.Rows.Add(dr)
Next
DGV1.DataSource = ""
DGV1.DataSource = dt
End Sub
Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
TryCast(DGV1.DataSource, DataTable).DefaultView.RowFilter = "UNVAN LIKE '%" & TextBox1.Text & "%'"
End Sub

Passing Values from (DataGridView1_Click) in a Form to another sub in another form

I want to pass value from (DataGridView1_Click) to another sub which is in another form
How To Achieve that?
Is there any way to do that, Please help me
Public Class SearchCustomers
Private Sub SearchCustomers_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
txtCustomerSearchBox.Text = ""
CBCustomerSearch.SelectedIndex = -1
txtCustomerSearchBox_TextChanged(Nothing, Nothing)
End Sub
This the click Event
' Private Sub DataGridView1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles DataGridView1.Click
'FrmCustomers mycustomers = New FrmCustomers()
' mycustomers.show_data(DataGridView1.CurrentRow.Cells(1).Value.ToString)
' End Sub
Private Sub DataGridView1_RowsAdded(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewRowsAddedEventArgs) Handles DataGridView1.RowsAdded
For I = 0 To DataGridView1.Rows.Count - 1
DataGridView1.Rows(I).Cells(0).Value = "Go"
Dim row As DataGridViewRow = DataGridView1.Rows(I)
row.Height = 25
Next
End Sub
Private Sub DataGridView1_CellContentClick( ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick
End Sub
Private Sub DataGridView1_Click( ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DataGridView1.Click
Dim oForm As New FrmCustomers()
Dim CustomerCode As String
CustomerCode = (DataGridView1.CurrentRow.Cells(1).Value.ToString)
oForm.show_data(CustomerCode)
MsgBox(DataGridView1.CurrentRow.Cells(1).Value.ToString, MsgBoxStyle.Exclamation, "Warning Message")
End Sub
End Class
this is the sub method in form 2
I want from this method to show data from DB to TextBox as seen in the code below
Sub show_data(CustomerCod)
OpenFileDialog1.FileName = ""
Dim sqls = "SELECT * FROM Customers WHERE CustomerCode=N'" & (CustomerCod) & "'"
Dim adp As New SqlClient.SqlDataAdapter(sqls, SQLconn)
Dim ds As New DataSet
adp.Fill(ds)
Dim dt = ds.Tables(0)
If dt.Rows.Count = 0 Then
MsgBox("no record found", MsgBoxStyle.Exclamation, "warning message")
Else
Dim dr = dt.Rows(0)
On Error Resume Next
CustomerCode.Text = dr!CustomerCode
CustomerName.Text = dr!CustomerName
Address.Text = dr!Address
Country.Text = dr!Country
City.Text = dr!City
Fax.Text = dr!Fax
Mobile.Text = dr!Mobile
Email.Text = dr!Email
Facebook.Text = dr!Facebook
Note.Text = dr!Note
'====================== Image Reincyrpation
If IsDBNull(dr!Cust_image) = False Then
Dim imgByteArray() As Byte
imgByteArray = CType(dr!Cust_image, Byte())
Dim stream As New MemoryStream(imgByteArray)
Dim bmp As New Bitmap(stream)
Cust_image.Image = Image.FromStream(stream)
stream.Close()
Label16.Visible = False
'================================================
End If
BtnEdit.Enabled = False
BtnDelete.Enabled = False
BtnSave.BackColor = Color.Red
CustomerName.Focus()
End If
End Sub
You can do like this (just to Call):
Private Sub DataGridView1_Click(sender As Object, e As EventArgs) Handles DataGridView1.Click
FrmCustomers.Show()
FrmCustomers.Hide()
FrmCustomers.show_data(CustomerCod)
FrmCustomers.Dispose()
End Sub

The record cannot be deleted or changed because table 'Modules' includes related records

Having a problem with deleting records this exception keeps occurring.
An unhandled exception of type 'System.Data.OleDb.OleDbException' occurred in System.Data.dll
Additional information: The record cannot be deleted or changed because table 'Modules' includes related records.
Public Class frmStudentDatabase
Dim objConnection As New OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;" & "Data Source=StudentDatabase.accdb")
Dim objStudentDA As New OleDb.OleDbDataAdapter("Select * From Students", objConnection)
Dim objStudentCB As New OleDb.OleDbCommandBuilder(objStudentDA)
Dim objDataSet As New DataSet()
Dim objModuleDA As New OleDb.OleDbDataAdapter("Select * From Modules", objConnection)
Dim objModuleCB As New OleDb.OleDbCommandBuilder(objModuleDA)
Private Sub frmStudentDatabase_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'Call the sub procedure that will fill the dataset
'Create (Relationships) and Retrieve all Student_ID numbers
Retrieve()
End Sub
Private Sub cboStudents_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cboStudents.SelectedIndexChanged
FillStudentDetails()
FillModuleDetails()
End Sub
Public Sub Retrieve()
objDataSet.Clear()
objStudentDA.FillSchema(objDataSet, SchemaType.Source, "Students")
objStudentDA.Fill(objDataSet, "Students")
objModuleDA.FillSchema(objDataSet, SchemaType.Source, "Modules")
objModuleDA.Fill(objDataSet, "Modules")
objDataSet.Relations.Clear()
objDataSet.Relations.Add("Students2Modules", objDataSet.Tables("Students").Columns("Student_ID"), _
objDataSet.Tables("Modules").Columns("Student_ID"))
'Empty combo box
cboStudents.Items.Clear()
'Loop through each row, adding the Student_ID to the combobox
Dim i As Integer, strCurrentID As String
For i = 1 To objDataSet.Tables("Students").Rows.Count
strCurrentID = objDataSet.Tables("Students").Rows(i - 1).Item("Student_ID")
cboStudents.Items.Add(strCurrentID)
Next
'Select first item in the list
cboStudents.SelectedIndex = 0
FillStudentDetails()
FillModuleDetails()
End Sub
Public Sub FillStudentDetails()
Dim objRow As DataRow
objRow = objDataSet.Tables("Students").Rows.Find(cboStudents.SelectedItem)
txtStudentID.Text = objRow.Item("Student_ID")
txtStudentName.Text = objRow.Item("Student_Name")
txtStudentAddress.Text = objRow.Item("Student_Address")
End Sub
Public Sub FillModuleDetails()
Dim objStudent As DataRow, objModule As DataRow
Dim strModuleEntry As String
'Clear any existing modules
lstModules.Items.Clear()
'Find the current student record
objStudent = objDataSet.Tables("Students").Rows.Find(cboStudents.SelectedItem.ToString)
For Each objModule In objStudent.GetChildRows("Students2Modules")
strModuleEntry = objModule.Item("Module_ID") & "," & objModule.Item("Module_Name") &
"," & objModule.Item("Module_Desc")
lstModules.Items.Add(strModuleEntry)
DataGridView1.DataSource = objDataSet.Tables("Modules")
Next
End Sub
Private Sub btnNew_Click(sender As Object, e As EventArgs) Handles btnNew.Click
Dim objRow As DataRow
Dim objRowModules As DataRow
Dim RowIndex As Integer
objRow = objDataSet.Tables("Students").NewRow
objRow.Item("Student_Name") = InputBox("Please Enter Student Name:")
objRow.Item("Student_Address") = InputBox("Please Enter Student Address:")
'Add data row to table
objDataSet.Tables("Students").Rows.Add(objRow)
objStudentDA.Update(objDataSet, "Students")
'Set up the module data row object
objRowModules = objDataSet.Tables("Modules").NewRow
'Find the number of the last row
RowIndex = objDataSet.Tables("Students").Rows.Count - 1
'Tell vb.net to get the Student_ID value from the last row
objRowModules.Item("Student_ID") = objDataSet.Tables("Students").Rows(RowIndex)("Student_ID")
objRowModules.Item("Module_Name") = InputBox("Please Enter Module Name:")
objRowModules.Item("Module_Desc") = InputBox("Please Enter Module Description:")
'Add to the DB
objDataSet.Tables("Modules").Rows.Add(objRowModules)
'Update the data adapter
objModuleDA.Update(objDataSet, "Modules")
MessageBox.Show("New record saved", "Saved")
Retrieve()
End Sub
Private Sub btnSave_Click(sender As Object, e As EventArgs) Handles btnSave.Click
'Code to modify an existing record
Dim objRowCurrent As DataRow
objRowCurrent = objDataSet.Tables("Students").Rows.Find(cboStudents.SelectedItem.ToString)
'We cannot modify the ID as it is an AutoNumber
objRowCurrent("Student_Name") = txtStudentName.Text
objRowCurrent("Student_Address") = txtStudentAddress.Text
objStudentDA.Update(objDataSet, "Students")
objDataSet.AcceptChanges()
'Now that we made changes, we need to retrieve the new data
Retrieve()
End Sub
Private Sub btnSearch_Click(sender As Object, e As EventArgs) Handles btnSearch.Click
'Dim i As integer, strCurrentID As String
Dim StudentName As String
Dim StudentFound As Boolean = False
StudentName = InputBox("Enter a Name Please!", "Search")
For i As Integer = 0 To (objDataSet.Tables("Students").Rows.Count - 1)
If CStr(objDataSet.Tables("Students").Rows(i)("Student_Name")) = StudentName Then
StudentFound = True
cboStudents.SelectedIndex = i
FillStudentDetails()
FillModuleDetails()
End If
Next
End Sub
Private Sub btnNext_Click(sender As Object, e As EventArgs) Handles btnNext.Click
Dim rowIndex As Integer = cboStudents.SelectedItem - 1
If (rowIndex < objDataSet.Tables("Students").Rows.Count - 1) Then
rowIndex = rowIndex + 1
cboStudents.SelectedIndex = rowIndex
FillStudentDetails()
FillModuleDetails()
Else
MessageBox.Show("No more records to display", "End")
End If
End Sub
Private Sub btnDelete_Click(sender As Object, e As EventArgs) Handles btnDelete.Click
Dim objRow As DataRow
objRow = objDataSet.Tables("Students").Rows.Find(txtStudentID.Text)
'Remember that all related child rows will be deleted! So no need to
'set up a new datarow for the Pet table
objRow.Delete()
objStudentDA.Update(objDataSet, "Students")
Retrieve()
End Sub
Private Sub btnPrevious_Click(sender As Object, e As EventArgs) Handles btnPrevious.Click
Dim rowIndex As Integer = cboStudents.SelectedItem - 1
If (rowIndex > 0) Then
rowIndex = rowIndex - 1
cboStudents.SelectedIndex = rowIndex
FillStudentDetails()
FillModuleDetails()
Else
MessageBox.Show("No more records to display", "Start")
End If
End Sub
End Class
You try to remove a record which has child records. Due to those references your removal fails. The reason is that the Student has Modules. You will need to remove the references to the Student before you can remove it.

removing item from comboBox if it matches a cetain cell in dataGridView in VB.net

I have a list of names (employees) in a comboBox that I use to add into a dataGridView. I'm trying to make it so that once the employee is in the database and displayed in the dataGrid, their name is removed from the comboBox so as not to appear available to add to the dataGrid.
The code I've written for the task looks like it should work, but it doesn't.
Public Class Main
Public techPhones As New PhonesDataSet.TechCompanyPhonesDataTable
Public techPhonesTableAdapter As New PhonesDataSetTableAdapters.TechCompanyPhonesTableAdapter
Public employees As New PhonesDataSet.EmployeesDataTable
Public empTableAdapter As New PhonesDataSetTableAdapters.EmployeesTableAdapter
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load, MyBase.Activated
LoadForm()
End Sub
Private Sub cboEmployee_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboEmployee.SelectedIndexChanged
lblOffice.Text = "Office: " + cboEmployee.SelectedItem("Office")
lblPhone.Text = cboEmployee.SelectedItem("Phone")
lblEmpID.Text = cboEmployee.SelectedItem("EmpID")
End Sub
Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click, DataGridView1.CellDoubleClick
Dim addEmp As New AddEmployee(cboEmployee.SelectedItem("Employee"), cboEmployee.SelectedItem("Office"), cboEmployee.SelectedItem("Phone"), _
cboEmployee.SelectedItem("OfficeID"), cboEmployee.SelectedItem("EmpID"), cboEmployee.SelectedItem("Contract"), cboEmployee.SelectedItem("Returned"), _
cboEmployee.SelectedItem("Loaner"))
addEmp.ShowDialog()
End Sub
Private Sub DataGridView1_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick
Dim name As String = DataGridView1.Rows(DataGridView1.CurrentCell.RowIndex).Cells(2).Value
Dim employee As String
For index As Integer = 0 To cboEmployee.Items.Count - 1 Step 1
If cboEmployee.Items(index)("Employee") = name Then
employee = cboEmployee.Items(index)("Employee")
cboEmployee.SelectedIndex = index
End If
Next
End Sub
Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click
techPhonesTableAdapter.DeleteQuery((DataGridView1.Rows(DataGridView1.CurrentCell.RowIndex).Cells(1).Value))
LoadForm()
End Sub
Private Sub LoadForm()
techPhones = techPhonesTableAdapter.GetData()
DataGridView1.DataSource = techPhones
employees = empTableAdapter.GetData()
cboEmployee.DataSource = employees
cboEmployee.DisplayMember = ("Employee")
cboEmployee.ValueMember = ("EmpID")
lblOffice.Text = "Office: " + cboEmployee.SelectedItem("Office")
lblPhone.Text = cboEmployee.SelectedItem("Phone")
lblEmpID.Text = cboEmployee.SelectedItem("EmpID")
cboEmployee.Focus()
cboEmployee.SelectAll()
lblRowCount.Text = "There are " + DataGridView1.RowCount.ToString + " phones accounted for."
' the code below is supposed to check every row in the data grid against the employee list in
' cboEmployees and remove it from the list when they match
With DataGridView1
For indexDGV As Integer = 0 To .Rows.Count - 1 Step 1
cboEmployee.Items.Remove(.Rows(indexDGV).Cells(2).Value)
Next
End With
cboEmployee.Refresh()
End Sub
End Class
I've also tried this minor variation:
With DataGridView1
For indexDGV As Integer = 0 To .Rows.Count - 1 Step 1
For indexCBO As Integer = cboEmployee.Items.Count - 1 To 0 Step -1
If .Rows(indexDGV).Cells(2).Value = cboEmployee.Items(indexCBO)("Employee") Then
cboEmployee.Items.RemoveAt(indexCBO)
End If
Next
Next
End With
You don't even need to iterate through the comboBox items. .Remove (string) will work, and if the item you want to remove is not there, it ignores the command.
With DataGridView1
For indexDGV As Integer = 0 To .Rows.Count - 1 Step 1
cboEmployee.Items.Remove(.Rows(indexDGV).Cells(2).Value)
Next
End With
Try like this
With DataGridView1
For indexDGV As Integer = 0 To .Rows.Count - 1 Step 1
For indexCBO As Integer = cboEmployee.Items.Count - 1 To 0 Step -1
If .Rows(indexDGV).Cells(2).Value.Equals(cboEmployee.Items(indexCBO).Tostring()) Then
cboEmployee.Items.RemoveAt(indexCBO)
Exit For
End If
Next
Next
End With

looping through datagridview

Mine is a windows app. containing forms named BOM nd BOMSelected..
There is datagridview in BOM which contains checkbox column.. When the user selects checkbox, the selected rows should be seen in the datagridview of other form, SelectedBom..
I have coded but don't get it working.. Some error..
Can you please help ??
Your Help is greatly appreciated..
Here is what i have done !!
Public Class SelectedBom
Private Sub SelectedBom_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'TODO: This line of code loads data into the 'HemDatabase1DataSet4.partno' table. You can move, or remove it, as needed.
'Me.PartnoTableAdapter.Fill(Me.HemDatabase1DataSet4.partno)
Dim count As Integer = 0
For j As Integer = 0 To BOM.dgv1.RowCount - 1
If BOM.dgv1.Rows(j).Cells(0).Value = True Then
Dim ro As New DataGridViewRow
DataGridView2.Rows.Add(ro)
For i As Integer = 0 To BOM.dgv1.ColumnCount - 1
Me.DataGridView2.Rows(count).Cells(i).Value = BOM.dgv1.Rows(j).Cells(i).Value
Next
count += 1
End If
Next
End Sub
End Class
Try,
For Each row As DataGridViewRow In BOM.dgv1.Rows
Dim obj(row.Cells.Count - 1) As Object
For i = 0 To row.Cells.Count - 1
obj(i) = row.Cells(i).Value
Next
Me.DataGridView2.Rows.Add(obj)
Next
EDIT:
Demo:
Add Button1 and DataGridView1 in BOM form
Public Class BOM
Public Class Sample
Public Property Satus As Boolean
Public Property Name As String
Public Property ID As Integer
End Class
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
SelectedBom.Show()
End Sub
Private Sub BOM_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Dim myList As New List(Of Sample)
myList.Add(New Sample() With {.ID = 1, .Name = "A"})
myList.Add(New Sample() With {.ID = 2, .Name = "B"})
myList.Add(New Sample() With {.ID = 3, .Name = "C"})
DataGridView1.DataSource = myList
End Sub
End Class
Add DataGridView1 in SelectBOM form
Public Class SelectedBom
Private Sub SelectedBom_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Dim i As Integer = 0
DataGridView1.AutoGenerateColumns = False
DataGridView1.Columns.Add("Name", "Name")
DataGridView1.Columns.Add("No", "No")
For Each row As DataGridViewRow In BOM.DataGridView1.Rows
If DirectCast(row.Cells(0).Value, Boolean) Then
DataGridView1.Rows.Add(row.Cells(1).Value, row.Cells(2).Value)
End If
Next
End Sub
End Class
Maybe instead of using the for each statement, you should instead use:
for istep as integer = 0 to datagridview.rowcount - 2
With the for each syntax, you can't assign a value to the individual row as you walk down the row.