Inserting rows in a listbox

Hi,
is it possible to do inserting new row in between two different rows

for example, I have a data

001 Chicken Steak
001 Orange Juice
002 Pizza margarita
002 Evian Mineral Water
003 French Bread
003 Omelete Egg

so, I am expecting the data changed to,

Open Check
001 Chicken Steak
001 Orange Juice
Closed Check
Open Check
002 Pizza margarita
002 Evian Mineral Water
Closed Check
003 French Bread
003 Omelette Egg
Closed Check

so, the new row will inserting before and after different check number.

any help

thanks
arief

Hi Arief,
You would use the AddRowAt method to insert a new row. You might be better served by using multiple columns in your ListBox, then you can just iterate over the first column and look for the change you want to insert before. If you wanted to insert before check ID 002, something like this:

var rowMax as Integer = ListBox1.LastRowIndex
var checkNumber as Integer
for rowIndex as Integer = 0 to rowMax
  checkNumber = ListBox1.CellValueAt(rowIndex, 0).ToInteger
  if checkNumber = 2 then
    ListBox1.AddRowAt(rowIndex, "OpenCheck").
  end if
  exit for rowIndex    '// Leave the loop
next rowIndex

Of course this is a very basic example to show you how to use it, and all written in my browser so untested.

1 Like

Hi,
thanks for the help.
I just wondering what is the line code for,

`if checkNumber = 2 then`

there is nothing changes shown. when it removed, row added in listindex 0

thanks
regards,
arief

That’s just a static check to determine when the check ID goes to 2. You would use something more dynamic such as comparing the rows’ actual values. In application, you’d probably do something more like:

var rowMax as Integer = ListBox1.LastRowIndex
var checkNumber, lastCheckNumber as Integer
for rowIndex as Integer = 0 to rowMax
  checkNumber = ListBox1.CellValueAt(rowIndex, 0).ToInteger
  if checkNumber <> lastCheckNumber then
    if lastCheckNumber > 0 then
      ListBox1.AddRowAt(rowIndex, "CloseCheck")
    end if
    ListBox1.AddRowAt(rowIndex, "OpenCheck")
    lastCheckNumber = checkNumber
  end if
next rowIndex

Dear Mr. Cyphers,

I have tried the code, but I still don’t get the result as I expected…

I have a similar code,
its almost works, the problem is, the code replacing the other rows with the same check ID and I dont know where to put the code for adding row “Closed Check”.

Listbox1.AddRow "001"
Listbox1.AddRow "001"
Listbox1.AddRow "002"
Listbox1.AddRow "002"
Listbox1.AddRow "003"
Listbox1.AddRow "003"
Listbox1.AddRow "004"
Listbox1.AddRow "004"

Dim sourcex As Listbox = Listbox1 
Dim ix, jx As Integer
For ix = sourcex.ListCount-1 DownTo 0
  For jx = ix + 1 To sourcex.ListCount-1
    
    If sourcex.Cell(ix,0) = sourcex.Cell(jx,0) Then
      Listbox1.cell(ix,0)="open check"
    end if
  Next
Next

The Goals is, just keep all the rows with the same check number, and inserting the new row in between that difference.

I am expecting the result like

Open Check
Listbox1.AddRow "001"
Listbox1.AddRow "001"
Closed Check
Open Check
Listbox1.AddRow "002"
Listbox1.AddRow "002"
Closed Check
Open Check
Listbox1.AddRow "003"
Listbox1.AddRow "003"
Closed Check
Open Check
Listbox1.AddRow "004"
Listbox1.AddRow "004"
Closed Check

thanks
regards,
arief

Try this:

var rowMax as Integer = ListBox1.LastRowIndex
var rowIndex, checkNumber, lastCheckNumber as Integer
while rowIndex <= rowMax
  checkNumber = ListBox1.CellValueAt(rowIndex, 0).ToInteger
  if checkNumber > lastCheckNumber then
    if lastCheckNumber > 0 then
      ListBox1.AddRowAt(rowIndex, "CloseCheck")
      rowIndex = rowIndex + 1
      rowMax = rowMax + 1
    end if
    ListBox1.AddRowAt(rowIndex, "OpenCheck")
    rowIndex = rowIndex + 1
    rowMax = rowMax + 1
    lastCheckNumber = checkNumber
  end if
  rowIndex = rowIndex + 1
wend
Listbox1.AddRow( "CloseCheck" )
2 Likes

Wow, thanks. its worked now.

regards,
arief

Happy to help!