|
-
Aug 9th, 2012, 02:52 PM
#1
Thread Starter
New Member
Deleting Selected Row in Flexgrid Base On Another Flexgrid VB 6.0
Hello,
I'm new in here and in vb too.
I have a problem to deleting selected row in Grid1 base on selected column in Grid
Take a look this screenshot below :

This code I have :
Code:
Private Sub InitGridPeriksa()
'This for generate Grid
cIRowCount = 0
With Grid
.Clear
.ClearStructure
.Rows = 2
.FixedRows = 1
.FixedCols = 1
.Cols = 8
.ColSel = 7
'Initialize the column size
.ColWidth(0) = 435
.ColWidth(1) = 1425
.ColWidth(2) = 700
.ColWidth(3) = 4900
.ColWidth(4) = 1115
.ColWidth(5) = 1115
.ColWidth(6) = 500
.ColWidth(7) = 500
'Initialize the column name
.TextMatrix(0, 0) = ""
.TextMatrix(0, 1) = "TestLabID"
.TextMatrix(0, 2) = "KdTestLab"
.TextMatrix(0, 3) = "NmTestLab"
.TextMatrix(0, 4) = "BiayaVIP"
.TextMatrix(0, 5) = "Tarif"
.TextMatrix(0, 6) = "TestLabFK"
.TextMatrix(0, 7) = "PKFK"
'Set the column alignment
.ColAlignment(0) = vbLeftJustify
.ColAlignment(1) = vbLeftJustify
.ColAlignment(2) = vbLeftJustify
.ColAlignment(3) = vbLeftJustify
.ColAlignment(4) = vbRightJustify
.ColAlignment(5) = vbRightJustify
.ColAlignment(6) = vbLeftJustify
.ColAlignment(7) = vbLeftJustify
End With
End Sub
Private Sub InitGridTest()
'This for generate grid1
cIRowCount1 = 0
With Grid1
.Clear
.ClearStructure
.Rows = 2
.FixedRows = 1
.FixedCols = 1
.Cols = 6
.ColSel = 5
'Initialize the column size
.ColWidth(0) = 435
.ColWidth(1) = 1200
.ColWidth(2) = 3000
.ColWidth(3) = 2000
.ColWidth(4) = 500
.ColWidth(5) = 500
'Initialize the column name
.TextMatrix(0, 0) = ""
.TextMatrix(0, 1) = "KdNilaiNormal"
.TextMatrix(0, 2) = "NmTestLab"
.TextMatrix(0, 3) = "NilaiNormal"
.TextMatrix(0, 4) = "TestLabFK"
.TextMatrix(0, 5) = "PKFK"
'Set the column alignment
.ColAlignment(0) = vbLeftJustify
.ColAlignment(1) = vbLeftJustify
.ColAlignment(2) = vbLeftJustify
.ColAlignment(3) = vbLeftJustify
.ColAlignment(4) = vbLeftJustify
.ColAlignment(5) = vbLeftJustify
End With
End Sub
This code for btnRemove ...
I want to delete selected row in Grid1 base on Selected Column in Grid ,,, please see screenshot !
Code:
Private Sub btnRemove_Click()
Dim i As Integer
Dim j As Integer
'this code is function to get selected column data in grid
CurrRow1 = getFlexPos(Grid, 6, NSDPeriksa.BoundText)
'this code I get in here (VBForums) but this code is fail !!
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'this code fail to delete selected row in grid1 base on column in grid ... script out of range
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
For i = 1 To cIRowCount1
If Grid1.TextMatrix(i, 4) = CurrRow1 Then 'selected match column in grid1 base on grid
Grid1.RowSel = i
With Grid1 'the msflexgrid
If .RowSel <> 0 Then 'check if there is a selected row
For j = .RowSel To .Rows - 2 'loop from selected row to the las row
.TextMatrix(j, 0) = .TextMatrix(j + 1, 0) 'set rows with 1 back
.TextMatrix(j, 1) = .TextMatrix(j + 1, 1)
.TextMatrix(j, 2) = .TextMatrix(j + 1, 2)
.TextMatrix(j, 3) = .TextMatrix(j + 1, 3)
Next j
.Rows = .Rows - 1 'make the rows 1 less
Else
MsgBox "Selecet row to delete!!!", vbExclamation
End If
End With
End If
Next i
'This code is OK to delete selected row in grid
With Grid
TxtBiaya(0).Text = TxtBiaya(0).Text - toNumber(Grid.TextMatrix(.RowSel, 5))
TxtBiaya(0).Text = toMoney(TxtBiaya(0).Text)
cIRowCount = cIRowCount - 1
If .Rows = 2 Then Grid.Rows = Grid.Rows + 1
.RemoveItem (.RowSel)
End With
btnRemove.Visible = False
Grid_Click
End Sub
Please help !!!
-
Aug 9th, 2012, 06:15 PM
#2
Re: Deleting Selected Row in Flexgrid Base On Another Flexgrid VB 6.0
Try to be more clear in your threads, you said grid1 and grid, then the snapshot shows grid1 and grid2.
There is a remove button in that grid1 that should simply delete the selected record in grid1, the other logic step would be also delete the corresponding records from grid2, but i don't think this is what you mean because you're also talking about a column, so i have no idea what you're trying to do here.
Also have in mind: It's a bad practice just removing the row from the grid when a record is deleted by the user, the best would be first executing the DELETE command in the database to remove that record and then clearing the grid and reloading all records, this is something more "atomic" and better. The problem with just deleting the row from the grid by code (when working with databases) is: imagine for some reason the record in the db is not deleted, like any given error, then you delete the row from the grid anyway, so the grid is not showing the real state of the db and vice-versa, and there you'll get data corruption, inconsistencies, etc..
Also, if this is gonna be a 2 step command execution to the db, like 2 delete commands, then you should use a transaction for doing this, again the idea is keeping things as atomic as posible, meaning: everything is executed or nothing is executed, this means that if there is an error then everything will return to previous state (Connection.rollback), else all commands will execute (Connection.Commit), this is what transactions are for.
-
Aug 10th, 2012, 09:37 AM
#3
Thread Starter
New Member
Re: Deleting Selected Row in Flexgrid Base On Another Flexgrid VB 6.0
 Originally Posted by jcis
Try to be more clear in your threads, you said grid1 and grid, then the snapshot shows grid1 and grid2.
There is a remove button in that grid1 that should simply delete the selected record in grid1, the other logic step would be also delete the corresponding records from grid2, but i don't think this is what you mean because you're also talking about a column, so i have no idea what you're trying to do here.
Also have in mind: It's a bad practice just removing the row from the grid when a record is deleted by the user, the best would be first executing the DELETE command in the database to remove that record and then clearing the grid and reloading all records, this is something more "atomic" and better. The problem with just deleting the row from the grid by code (when working with databases) is: imagine for some reason the record in the db is not deleted, like any given error, then you delete the row from the grid anyway, so the grid is not showing the real state of the db and vice-versa, and there you'll get data corruption, inconsistencies, etc..
Also, if this is gonna be a 2 step command execution to the db, like 2 delete commands, then you should use a transaction for doing this, again the idea is keeping things as atomic as posible, meaning: everything is executed or nothing is executed, this means that if there is an error then everything will return to previous state (Connection.rollback), else all commands will execute (Connection.Commit), this is what transactions are for.
Thank you for your replay jcis ..
But I don't want to delete it from the database, I just want to remove it from msflexgrid. The btnRemove is the option for user if they wrong input the data they can remove it from grid before they click the button save.
Take a look the btnRemove code, the process removing data with grid1. The error say "script out of range".
-
Aug 10th, 2012, 10:13 AM
#4
Thread Starter
New Member
Re: Deleting Selected Row in Flexgrid Base On Another Flexgrid VB 6.0
Oh, I forgot it, the msflexgrid above in screenshot name "Grid" and the below name "Grid1"
-
Aug 10th, 2012, 11:32 AM
#5
Re: Deleting Selected Row in Flexgrid Base On Another Flexgrid VB 6.0
When you're going to remove columns from a grid or list control you need to loop backwards, not forward, this way the "index changing" won't affect your code.
When you do this: For i = 1 To cIRowCount1 and then you start removing rows, cIRowCount1 hold an invalid max index, because the number of rows has decreased, so when the loop gets to a point when variable i contains an index higher than the number of rows you get that error. So just remove rows looping backwards, like:
Code:
For i = cIRowCount1 To 1 Step -1
If Grid1.TextMatrix(i, 4) = CurrRow1 Then 'selected match column in grid1 base on grid
Grid1.RemoveItem(i)
End If
Next i
I don't understand why are you using 2 nested For loops and why are you moving up cell values like that, that's done automatically by the Flexgrid after a row is removed, you don't need to do that.
I also removed that If .RowSel <> 0 Then because you don't even need to use .Rowsel there. Also it's not a good idea placing messageboxes inside loops, except if you want the user to get really mad
-
Aug 10th, 2012, 12:20 PM
#6
Thread Starter
New Member
Re: Deleting Selected Row in Flexgrid Base On Another Flexgrid VB 6.0
 Originally Posted by jcis
When you're going to remove columns from a grid or list control you need to loop backwards, not forward, this way the "index changing" won't affect your code.
When you do this: For i = 1 To cIRowCount1 and then you start removing rows, cIRowCount1 hold an invalid max index, because the number of rows has decreased, so when the loop gets to a point when variable i contains an index higher than the number of rows you get that error. So just remove rows looping backwards, like:
Code:
For i = cIRowCount1 To 1 Step -1
If Grid1.TextMatrix(i, 4) = CurrRow1 Then 'selected match column in grid1 base on grid
Grid1.RemoveItem(i)
End If
Next i
I don't understand why are you using 2 nested For loops and why are you moving up cell values like that, that's done automatically by the Flexgrid after a row is removed, you don't need to do that.
I also removed that If .RowSel <> 0 Then because you don't even need to use .Rowsel there. Also it's not a good idea placing messageboxes inside loops, except if you want the user to get really mad 
Hi, thanks again for replay ..
I try your code .. still not working
The grid1 data did not remove...
I try another combination into your code like this :
Code:
For i = cIRowCount1 To 1 Step -1
Grid1.RowSel = i
If Grid1.TextMatrix(i, 4) = CurrRow1 Then 'selected match column in grid1 base on grid
Grid1.RemoveItem(i)
End If
Next i
But is still same, not working ...
-
Aug 10th, 2012, 01:05 PM
#7
Re: Deleting Selected Row in Flexgrid Base On Another Flexgrid VB 6.0
Did you debug your previous code and verify variable CurrRow1 contains the correct value? (the value for that column TestL in the selected row)
Can you post Function getFlexPos?
-
Aug 10th, 2012, 10:32 PM
#8
Thread Starter
New Member
Re: Deleting Selected Row in Flexgrid Base On Another Flexgrid VB 6.0
-
Aug 11th, 2012, 01:32 AM
#9
Re: Deleting Selected Row in Flexgrid Base On Another Flexgrid VB 6.0
ok, that Function compares cell text in a given column and if it finds the string then it returns the first index where it's been found, the index is the row number. As it is now it's not what we need because we need to do something like this but in the other grid. I would simply do this comparison in the loop itself, you dont really need this Function for doing this. Try this:
Code:
For i = cIRowCount1 To 1 Step -1
If Grid1.TextMatrix(i, 4) = NSDPeriksa.BoundText Then
Grid1.RemoveItem(i)
End If
Next i
If you need this comparison to be non case sensitive then use:
Code:
If UCase(Grid1.TextMatrix(i, 4)) = UCase(NSDPeriksa.BoundText) Then
'...
Last edited by jcis; Aug 11th, 2012 at 01:35 AM.
-
Aug 11th, 2012, 11:45 AM
#10
Thread Starter
New Member
Re: Deleting Selected Row in Flexgrid Base On Another Flexgrid VB 6.0
I try this code is working !, but only for first delete action.
For example I have 4 rows in the grid. I remove the 2nd row. It's working. And next I try to remove the row number 1 in grid, The grid1 didn't removing row that I selected in the grid.
here the code :
Code:
For i = cIRowCount1 To 1 Step -1
With Grid1
cIRowCount1 = cIRowCount1 - 1 ' --> I think this is more confusing me !!!!!!
If .Rows = 2 Then .Rows = .Rows + 1
If .TextMatrix(i, 6) = Text1.Text Then 'selected match column in grid1 base on grid
'.RowSel = i
.RemoveItem (i)
End If
End With
Next i
After the row in grid1 is removed, I need to re-count the total rows in grid1 for the next action and place the selected row in the last row in grid1. I can't imagine how to do that ??
-
Aug 11th, 2012, 09:39 PM
#11
Thread Starter
New Member
Re: Deleting Selected Row in Flexgrid Base On Another Flexgrid VB 6.0
-
Aug 13th, 2012, 09:57 AM
#12
Thread Starter
New Member
Re: Deleting Selected Row in Flexgrid Base On Another Flexgrid VB 6.0
Hello ...
anyone help ... it's 1 week now ...
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|