Reputation: 3
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
Upvotes: 0
Views: 3986
Reputation: 2701
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
Upvotes: 1
Reputation: 16
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
Upvotes: 0