B4J Question TableView Delete Row based on Value in TableView

Discussion in 'B4J Questions' started by aaronk, Jan 31, 2015.

  1. aaronk

    aaronk Well-Known Member Licensed User

    Hi,

    I am trying to delete a item from a TableView based on what is set in Colum 1.

    I am using the following code and can't seem to work out how to delete the a row based on the value.

    Code:
    Sub AppStart (Form1 As Form, Args() As String)
        MainForm = Form1
        MainForm.RootPane.LoadLayout(
    "1"'Load the layout file.
        MainForm.Show
        MainForm.Title = 
    "Table Example"
        
    'set the columns
        tv1.SetColumns(Array As String("Col1""Col2"))
        
    'fill with data
        'each row must be an array of objects. Make sure not to reuse the same array!
      
        
    Dim row(2As Object
            row(
    0) = "Bob"
            
    'the second item will be later edited by the user. To allow it to be updated we use a label.
            Dim lbl As Label
            lbl.Initialize(
    "")
            lbl.Text = 
    "1"
            row(
    1) = lbl
            tv1.Items.Add(row)
          
        
    Dim row(2As Object
            row(
    0) = "john"
            
    'the second item will be later edited by the user. To allow it to be updated we use a label.
            Dim lbl As Label
            lbl.Initialize(
    "")
            lbl.Text = 
    "2"
            row(
    1) = lbl
            tv1.Items.Add(row)
          
        
    Dim row(2As Object
            row(
    0) = "bill"
            
    'the second item will be later edited by the user. To allow it to be updated we use a label.
            Dim lbl As Label
            lbl.Initialize(
    "")
            lbl.Text = 
    "3"
            row(
    1) = lbl
            tv1.Items.Add(row)
          
        
    Dim row(2As Object
            row(
    0) = "Jack"
            
    'the second item will be later edited by the user. To allow it to be updated we use a label.
            Dim lbl As Label
            lbl.Initialize(
    "")
            lbl.Text = 
    "4"
            row(
    1) = lbl
            tv1.Items.Add(row)
          
      
        tv1.SetColumnSortable(
    1False)
        btnUpdate.Enabled = 
    False
    End Sub
    Lets say I want to Delete the row if column 1 = bill.

    I have tried using the following code, but it doesn't seem to like..

    Code:
    For i = 0 To tv1.Items.Size -1
            
    If tv1.SelectCell(i,1) = "bill" Then
                tv1.Items.RemoveAt(i)
            
    End If
        
    Next
    Code:
    Error description: Object reference not set to an instance of an object.
    Occurred on line: 
    64
    If tv1.SelectCell(1,1) = "bill" Then
    Word: )
    I guess I am doing something wrong, does anyone know how to delete a row based on the value that is in column 1 ?
     
  2. rwblinn

    rwblinn Well-Known Member Licensed User

    Hi,

    try
    Code:
    For i = 0 To tv1.Items.Size -1
      
    Dim row() As Object = tv1.Items.Get(i)
      
    Dim name As String = row(0)
      
    If name = "bill" Then
        tv1.Items.RemoveAt(i)
        
    Return
      
    End If
    Next
     
  3. aaronk

    aaronk Well-Known Member Licensed User

    Thanks.. that did the job.
    I thought I was doing it wrong.
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice