Search code examples
vb.netbindingsource

Vb.net bindingsource.filter from a list of strings


i have in a windows.form a combobox and a datagridview, i add rows in datagridview with a button and get the value(string) from combobox and if i double click on the row of datagridview i delete the row.

When i add the row i want to hide/disable/remove the combobox value and when i delete the row i want to restore it in combobox.

The combobox values are binding from dataset source and combobox is dropdownlist style.

I try some things until now but this is what i think is better approach and where i am:

Dim filterList As List(Of String) = New List(Of String)

Private Sub filterListAdd()

        Dim dgResult As String
        filterList .Clear() 'Clear the list so no duplicates

        For i As Integer = 0 To combobox.Items.Count - 1
            Dim a As String = combobox.GetItemText(combobox.Items(i))

            For row As Integer = 0 To Dgview.RowCount - 1
                For col As Integer = 0 To Dgview.ColumnCount - 1
                Next
                Surname = Dgview.Rows(row).Cells(0).Value.ToString
                If dgResult = a Then 
                    filterList .Add(a) 'Add to list
                End If
            Next
        Next i

    End Sub   

Private Sub Button_Click(sender As Object, e As EventArgs) Handles Button.Click

Dgview.Rows.Add(combobox.Text)
filterListAdd()

'Here i want to bindingsource.filter = filterList 

End Sub

Private Sub Dgview_MouseDoubleClick(sender As Object, e As MouseEventArgs) Handles Dgview.MouseDoubleClick

Dgview.Rows.Remove(Dgview.CurrentRow)

'I Guess here with the same way i filter it again

End Sub

Any help will be appreciated, thanks in advance.

Panos


Solution

  • Public Class Form1
    
    Dim clsItems As New ArrayList()
    
    Private Sub RemoveComboItem()
    
    Me.BindingSource.Position = Me.BindingSource.Find("1", Combobox.Text)
    Dim 1Add As String = DirectCast(BindingSource.Current, DataRowView).Item("1").ToString
    Dim 2Add As String = DirectCast(BindingSource.Current, DataRowView).Item("2").ToString
    Dim 3Add As String = DirectCast(BindingSource.Current, DataRowView).Item("3").ToString
    
          clsItems.Add(New MyItem(1Add, 2Add, 3Add))
    
          BindingSource.RemoveCurrent()
    
      End Sub
    
    Private Sub AddComboItem(dg As DataGridView) ' Because i have two datagridviews
    
          Dim 1 As String = dg.CurrentRow.Cells(0).Value.ToString()
    
          For i = 0 To clsItems.Count - 1
              If i <= clsItems.Count - 1 Then
                  If 1 = clsItems(i).1 Then
                      Dim drNewRow As DataRow
                      drNewRow = DeeDataSet.Tables("Table").NewRow()
                      drNewRow("1") = clsItems(i).1
                      drNewRow("2") = clsItems(i).2
                      drNewRow("3") = clsItems(i).3
                      DeeDataSet.Tables("Table").Rows.Add(drNewRow)
                      clsItems.Remove(clsItems(i)) ' Remove it from array so no duplicates
                  End If
              End If
          Next
    
      End Sub
    
    Private Sub Button_Click(sender As Object, e As EventArgs) Handles Button.Click
    
    RemoveComboItem()
    
    End Sub
    
    Private Sub Dgview_MouseDoubleClick(sender As Object, e As MouseEventArgs) Handles Dgview.MouseDoubleClick
    
    AddComboItem(Dgview)
    Dgview.Rows.Remove(Dgview.CurrentRow)
    
    End Sub
    
    End class
    
    Public Class MyItem
    
      Private m_s1 As String
      Private m_s2 As String
      Private m_s3 As String
    
      Public Sub New(1As String, 2 As String, 3 As String)
    
          M_sName = 1
          M_sSurname = 2
          M_sTitle = 3
    
      End Sub
    
      Public ReadOnly Property 1
    
          Get
              Return m_s1
          End Get
    
      End Property
    
      Public ReadOnly Property 2
    
          Get
              Return M_s2
          End Get
    
      End Property
    
      Public ReadOnly Property 3
    
          Get
              Return M_s3
          End Get
    
      End Property
    
    End Class
    

    In that form i don't update the dataset because every change i want to make it in other form, so when i finish from here the dataset.table has no changes.