vb mshflexgrid问题

xiaoyanli2008 2008-11-28 09:49:32
两个MSHFLEXGRID控件,mshflexgrid1和mshflexgrid2,如何将mshflexgrid1的行的内容移动到mshflexgrid2并将该行删除,反之也成立。我写的代码只能实现把mshflexgrid1的行全部移动到mshflexgrid2,反之把mshflexgrid2的全部行可以移动到mshflexgrid1。但是当mshflexgrid1的内容不全部移动到mshflexgrid2时,也就是mshflexgrid1里还存在几行,mshflexgrid2里也有几行时,再把mshflexgrid2里的行移到mshflexgrid1里就出错了,从mshflexgrid2移到mshflexgrid1的行说覆盖了mshflexgrid1的行一行,后面以此类推,请问怎样解决这个问题?我的代码如下 :

'从mshflexgrid1到mshflexgrid2
Private Sub grid1togrid2_Click()
Dim i As Integer
For i = MSHFlexGrid2.Row To MSHFlexGrid2.Row
MSHFlexGrid2.RowHeight(MSHFlexGrid2.Row) = 255
MSHFlexGrid2.TextMatrix(i, 0) = MSHFlexGrid1.TextMatrix(MSHFlexGrid1.Row, 0)
MSHFlexGrid2.TextMatrix(i, 1) = MSHFlexGrid1.TextMatrix(MSHFlexGrid1.Row, 1)
MSHFlexGrid2.TextMatrix(i, 2) = MSHFlexGrid1.TextMatrix(MSHFlexGrid1.Row, 2)
MSHFlexGrid2.TextMatrix(i, 3) = MSHFlexGrid1.TextMatrix(MSHFlexGrid1.Row, 3)
If MSHFlexGrid1.Rows > 2 Then
MSHFlexGrid1.RemoveItem (MSHFlexGrid1.Row)
Else
MSHFlexGrid1.AddItem "", MSHFlexGrid1.FixedRows '清空最后一非固定行
MSHFlexGrid1.Rows = MSHFlexGrid1.FixedRows + 1
End If
MSHFlexGrid2.AddItem (Row)
MSHFlexGrid2.Row = MSHFlexGrid2.Row + 1
MSHFlexGrid2.RowHeight(MSHFlexGrid2.Row) = 0
Next
End Sub

'从mshflexgrid2到mshflexgrid1
Private Sub grid2togrid1_Click()
Dim j As Integer
For j = MSHFlexGrid1.Row To MSHFlexGrid1.Row
MSHFlexGrid1.RowHeight(MSHFlexGrid1.Row) = 255
MSHFlexGrid1.TextMatrix(j, 0) = MSHFlexGrid2.TextMatrix(MSHFlexGrid2.Row, 0)
MSHFlexGrid1.TextMatrix(j, 1) = MSHFlexGrid2.TextMatrix(MSHFlexGrid2.Row, 1)
MSHFlexGrid1.TextMatrix(j, 2) = MSHFlexGrid2.TextMatrix(MSHFlexGrid2.Row, 2)
MSHFlexGrid1.TextMatrix(j, 3) = MSHFlexGrid2.TextMatrix(MSHFlexGrid2.Row, 3)
If MSHFlexGrid2.Rows > 2 Then
MSHFlexGrid2.RemoveItem (MSHFlexGrid2.Row)
Else
MSHFlexGrid2.AddItem "", MSHFlexGrid2.FixedRows
MSHFlexGrid2.Rows = MSHFlexGrid2.FixedRows + 1
End If
MSHFlexGrid1.AddItem (Row)
MSHFlexGrid1.Row = MSHFlexGrid1.Row + 1
MSHFlexGrid1.RowHeight(MSHFlexGrid1.Row) = 0
Next
End Sub

注:两按钮的代码完全一样。
...全文
294 15 点赞 打赏 收藏 举报
写回复
15 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
aijiuaile5959 2009-06-01
mshflexgrid正在学习中。学习学习
  • 打赏
  • 举报
回复
xiaoyanli2008 2008-11-28
还有MSHFlexGrid1.Row 怎么也不等于 MSHFlexGrid1.Rows 啊,至少也得比MSHFlexGrid1.Rows少一行
  • 打赏
  • 举报
回复
xiaoyanli2008 2008-11-28
空行我只是吧高度设为0了啊,
我加上你那句话还是不行,
  • 打赏
  • 举报
回复
Tiger_Zhao 2008-11-28
你自己将空行删除了,怪谁。
在 grid1togrid2_Click() 最前面添加
If MSHFlexGrid1.Row = MSHFlexGrid1.Rows Then Exit Sub

grid2togrid1 判断 MSHFlexGrid2
  • 打赏
  • 举报
回复
xiaoyanli2008 2008-11-28
哎,问题大着呢,都不行啊,请问还有什么方法能解决吗?急呀!
  • 打赏
  • 举报
回复
xiaoyanli2008 2008-11-28
“zuoxingyu”的方法也不行,这样从MSHFlexGrid2移回到MSHFlexGrid1时,一条数据都没有了。
  • 打赏
  • 举报
回复
zuoxingyu 2008-11-28
在窗体上画上MSHFlexGrid1和MSHFlexGrid2


Private Sub Form_Load()
MSHFlexGrid1.Rows = 1
MSHFlexGrid2.Rows = 1
MSHFlexGrid1.AddItem "1" & vbTab & "1"
MSHFlexGrid1.AddItem "2" & vbTab & "2"
MSHFlexGrid1.AddItem "3" & vbTab & "3"


End Sub

Private Sub MSHFlexGrid1_Click()
MSHFlexGrid2.AddItem MSHFlexGrid1.TextMatrix(MSHFlexGrid1.Row, 0) & vbTab & MSHFlexGrid1.TextMatrix(MSHFlexGrid1.Row, 1)
MSHFlexGrid1.RemoveItem MSHFlexGrid1.Row
End Sub

Private Sub MSHFlexGrid2_Click()
MSHFlexGrid1.AddItem MSHFlexGrid2.TextMatrix(MSHFlexGrid2.Row, 0) & vbTab & MSHFlexGrid2.TextMatrix(MSHFlexGrid2.Row, 1)
MSHFlexGrid2.RemoveItem MSHFlexGrid2.Row
End Sub


  • 打赏
  • 举报
回复
xiaoyanli2008 2008-11-28
真有问题,你先从MSHFlexGrid1里移动一条数据到MSHFlexGrid2里,然后再从MSHFlexGrid2里移动一条数据到MSHFlexGrid1里看看,那条数据就不见了。
  • 打赏
  • 举报
回复
xiaoyanli2008 2008-11-28
再等等,我再调试一下,因为刚才里面有的原来的代码。
  • 打赏
  • 举报
回复
zhufenghappy 2008-11-28
怎么可能呢,按我的6条数据一条也少不了阿
  • 打赏
  • 举报
回复
xiaoyanli2008 2008-11-28
这样的话,从MSHFlexGrid2移回到MSHFlexGrid1的时候还是有问题,而且当我移动多次时,两张表里面都没有内容了,都为空了。
  • 打赏
  • 举报
回复
zhufenghappy 2008-11-28
你的for语句无效给你注释掉了,现在是一次移动一行
'从mshflexgrid1到mshflexgrid2
Private Sub Command1_Click()
Dim i As Integer
If MSHFlexGrid1.TextMatrix(MSHFlexGrid1.Row, 1) = "" Then Exit Sub '空行不移动
'For i = MSHFlexGrid2.Row To MSHFlexGrid2.Row
MSHFlexGrid2.RowHeight(MSHFlexGrid2.Row) = 255
MSHFlexGrid2.AddItem ""
MSHFlexGrid2.TextMatrix(MSHFlexGrid2.Rows - 1, 0) = MSHFlexGrid1.TextMatrix(MSHFlexGrid1.Row, 0)
MSHFlexGrid2.TextMatrix(MSHFlexGrid2.Rows - 1, 1) = MSHFlexGrid1.TextMatrix(MSHFlexGrid1.Row, 1)
MSHFlexGrid2.TextMatrix(MSHFlexGrid2.Rows - 1, 2) = MSHFlexGrid1.TextMatrix(MSHFlexGrid1.Row, 2)
MSHFlexGrid2.TextMatrix(MSHFlexGrid2.Rows - 1, 3) = MSHFlexGrid1.TextMatrix(MSHFlexGrid1.Row, 3)
If MSHFlexGrid1.Rows > 2 Then
MSHFlexGrid1.RemoveItem (MSHFlexGrid1.Row)
Else
MSHFlexGrid1.AddItem "", MSHFlexGrid1.FixedRows '清空最后一非固定行
MSHFlexGrid1.Rows = MSHFlexGrid1.FixedRows + 1
End If

' MSHFlexGrid2.Row = MSHFlexGrid2.Row + 1
If MSHFlexGrid2.TextMatrix(1, 1) = "" Then MSHFlexGrid2.RowHeight(1) = 0
'Next
End Sub

'从mshflexgrid2到mshflexgrid1
Private Sub Command2_Click()
Dim j As Integer
If MSHFlexGrid2.TextMatrix(MSHFlexGrid2.Row, 1) = "" Then Exit Sub '空行不移动
'For j = MSHFlexGrid1.Row To MSHFlexGrid1.Row
MSHFlexGrid1.AddItem 1
MSHFlexGrid1.RowHeight(MSHFlexGrid1.Row) = 255
MSHFlexGrid1.TextMatrix(MSHFlexGrid1.Rows - 1, 0) = MSHFlexGrid2.TextMatrix(MSHFlexGrid2.Row, 0)
MSHFlexGrid1.TextMatrix(MSHFlexGrid1.Rows - 1, 1) = MSHFlexGrid2.TextMatrix(MSHFlexGrid2.Row, 1)
MSHFlexGrid1.TextMatrix(MSHFlexGrid1.Rows - 1, 2) = MSHFlexGrid2.TextMatrix(MSHFlexGrid2.Row, 2)
MSHFlexGrid1.TextMatrix(MSHFlexGrid1.Rows - 1, 3) = MSHFlexGrid2.TextMatrix(MSHFlexGrid2.Row, 3)
If MSHFlexGrid2.Rows > 2 Then
MSHFlexGrid2.RemoveItem (MSHFlexGrid2.Row)
Else
MSHFlexGrid2.AddItem "", MSHFlexGrid2.FixedRows
MSHFlexGrid2.Rows = MSHFlexGrid2.FixedRows + 1
End If
' MSHFlexGrid1.AddItem (Row)
' MSHFlexGrid1.Row = MSHFlexGrid1.Row + 1
If MSHFlexGrid1.TextMatrix(1, 1) = "" Then MSHFlexGrid1.RowHeight(1) = 0
'Next
End Sub



Private Sub Form_Load()
Dim i As Integer
Dim j As Integer
With MSHFlexGrid1
.Rows = 4
.Cols = 4
For i = 1 To 3
For j = 1 To 3
.TextMatrix(i, j) = i + j
Next
Next
End With
With MSHFlexGrid2
.Rows = 4
.Cols = 4
For i = 1 To 3
For j = 1 To 3
.TextMatrix(i, j) = i + j + 10
Next
Next
End With
End Sub
  • 打赏
  • 举报
回复
xiaoyanli2008 2008-11-28
补充:mshflexgrid1里初始时有三行数据,mshflexgrid2里初始时没有数据,有一行为空被隐藏掉了。
  • 打赏
  • 举报
回复
Tiger_Zhao 2008-11-28
那就判断 Rows-1。
因为你的操作可能将当前行指向空行,被复制到另一边后就被删除了。
你可以将空行显式出来,然后操作一下就明白了。
  • 打赏
  • 举报
回复
Tiger_Zhao 2008-11-28
那就判断 Rows-1。
因为你的操作可能将当前行指向空行,被复制到另一边后就被删除了。
你可以将空行显式出来,然后操作一下就明白了。
  • 打赏
  • 举报
回复
相关推荐
发帖
VB基础类
创建于2007-09-28

7522

社区成员

VB 基础类
申请成为版主
帖子事件
创建了帖子
2008-11-28 09:49
社区公告
暂无公告