求助VB中datagrid问题

qq37724861 2011-04-20 12:29:26
form1上布置datagrid1控件,设置好数据源等信息
form2上布置一个Listbox,Style设置为CheckBox
然后点击datagrid1的表头,会弹出菜单——隐藏此列,点击后该列被隐藏,然后隐藏的列列名同时添加到form2的listbox中(这些功能代码都编写好,无问题)

在listbox中选择若干项,点击确定,则在form1中取消这些列的隐藏。

遇到的问题:
现在就是在取消隐藏时有问题,
请看代码: command1为取消隐藏列的按钮

Private Sub Command1_Click()
For i = 0 To List1.ListCount - 1
If List1.Selected(i) = True Then
List1.ListIndex = i
MsgBox List1.Text
MsgBox Form1.DataGrid1.Columns(List1.Text)
'以下语句不行:
'Form1.DataGrid1.Columns(List1.Text).Visible = True
End If
Next i
End Sub


上面的msgbox我是发现错误后做的测试
第一个msgbox 弹出的是正确的 List1的内容
但是第二个msgbox弹出的就不是 List1的内容,导致datagrid不能正确显示列

请教 此处语句该怎么写呢?
...全文
66 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
qq37724861 2011-04-21
  • 打赏
  • 举报
回复
多谢楼上几位帮忙,我自己搞定了!谢谢!!!
qq37724861 2011-04-21
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 yanlongwuhui 的回复:]
看下有没有List1.Text对应的列名
[/Quote]
当然有了,隐藏datagrid列的同时 把隐藏的列名添加到list中的
qq37724861 2011-04-21
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 dxd0128 的回复:]
Private Sub Command1_Click()
For i = 0 To List1.ListCount - 1
If List1.Selected(i) = True Then
List1.ListIndex = i
MsgBox List1.Text
MsgBox Form1.DataGrid1.Columns(List1.ListIndex).Capt……
[/Quote]

datagrid列的index不等于list的index 这样是不行的
独立级IT民工 2011-04-21
  • 打赏
  • 举报
回复
Private Sub Command1_Click()
For i = 0 To List1.ListCount - 1
If List1.Selected(i) = True Then
List1.ListIndex = i
MsgBox List1.Text
MsgBox Form1.DataGrid1.Columns(List1.ListIndex).Caption
DataGrid1.Columns(List1.ListIndex).Visible = True
End If
Next i
End Sub
独立级IT民工 2011-04-21
  • 打赏
  • 举报
回复
Private Sub Command1_Click()
For i = 0 To List1.ListCount - 1
If List1.Selected(i) = True Then
List1.ListIndex = i
MsgBox List1.Text
MsgBox Form1.DataGrid1.Columns(List1.ListIndex).Caption
'以下语句不行:
DataGrid1.Columns(List1.ListIndex).Visible = False
End If
Next i
End Sub
yanlongwuhui 2011-04-21
  • 打赏
  • 举报
回复
看下有没有List1.Text对应的列名
yanlongwuhui 2011-04-20
  • 打赏
  • 举报
回复
试试:MsgBox Form1.DataGrid1.Columns(List1.Text).TEXT
qq37724861 2011-04-20
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 yanlongwuhui 的回复:]
试试:MsgBox Form1.DataGrid1.Columns(List1.Text).TEXT
[/Quote]
text、caption、datafield都试过,都不行的

16,554

社区成员

发帖
与我相关
我的任务
社区描述
VB技术相关讨论,主要为经典vb,即VB6.0
社区管理员
  • VB.NET
  • 水哥阿乐
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧