错误在VB6中将数据设置为datagrid [英] Error Set data to datagrid in VB6

查看:61
本文介绍了错误在VB6中将数据设置为datagrid的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好.

我有问题,所以需要大家的帮助.

我得到一个数据网格,如下所示:

Hello everyone.

I have some problem so I need the help of everyone.

I get a datagrid as follows:

A B C D E --> header
1 2 4 5 7
6 2 5 1 3
1 9 4 8 9


和一个命令按钮.
当我单击此按钮时,我的数据网格将在其中添加列F,如下所示:


and a commandbutton.
When I click on this button, my datagrid will add a column F in it as follows:

A B F C D E --> header
1 2 - 4 5 7
6 2 - 5 1 3
1 9 - 4 8 9


F列没有任何数据.

我想为列F的任何行设置数据.

问题是,当我为第2行(6 2 - 5 1 3)设置值5时,应用程序显示错误找不到列,| 1".

我无法修复它,所以希望每个人都可以帮助我解决它并为F列设置数据.
:doh:
谢谢.


Column F doesn''t have any data.

I want to Set data for any row of column F.

The problem is when I set value 5 for row 2(6 2 - 5 1 3), the application shows error "Column not found, |1".

I could not fix it, so I hope that everyone can help me resolve it and set data for column F.
:doh:
thanks.

推荐答案

该错误可能是由于您试图在绑定的数据网格中拥有未绑定的列而引起的.

要么填充遍历记录集的未绑定网格,要么在记录集中创建一个附加字段以绑定到附加列.

下面的代码工作正常.我还有一个字段要绑定到列F.顺便说一句,我正在手动填充它,您可以在此处使用您的代码. ;)

That error may be due to the fact that you are trying to have an unbound column in bound datagrid.

Either populate your unbound grid looping through recordset, or make an additional field in recordset to be bound to the additional column.

The code below works fine. I have an additional field to be bound to column F. BTW, I am populating it manually, you can use your code there. ;)

Sub populateGrid(fColumn As Boolean)
    Dim rs As ADODB.Recordset
    'Make a recordset
    Set rs = New ADODB.Recordset
    rs.CursorLocation = adUseClient
    'add your fields
    rs.Fields.Append "A", adVarChar, 100
    rs.Fields.Append "B", adVarChar, 100
    rs.Fields.Append "C", adVarChar, 100
    rs.Fields.Append "D", adVarChar, 100
    rs.Fields.Append "E", adVarChar, 100
    rs.Fields.Append "F", adVarChar, 100
    'Open recordset
    rs.Open , Nothing
    'add your records
    For i = 1 To 5
        rs.AddNew
        rs.Fields("A").Value = i
        rs.Fields("B").Value = i
        rs.Fields("C").Value = i
        rs.Fields("D").Value = i
        rs.Fields("E").Value = i
    Next
    If fColumn Then
        Dim dc
        Set dc = DataGrid1.Columns.Add(2)
        dc.Caption = "F"
        dc.DataField = "F"
    End If
    'bind to grid
    Set DataGrid1.DataSource = rs
End Sub



我希望这对您有所帮助,因为自从最近几天以来,您一直在努力解决这个网格问题.我认为VB6会让您等待更长的时间.



I hope this helps, as I''m seeing you struggling with this grid question since last few days. It''s VB6 that is making you wait longer for answer I think.


感谢您的帮助.但是我需要的是我有一个带有数据的数据网格,然后添加更多列F并为F设置数据.我的代码如下:
Thanhs your helps. but My required is I have a datagrid with data and then I add more column F and set data for F.My code is same as follow:
''create datagrid and add column F
Dim rs As ADODB.Recordset
    ''Make a recordset
    Set rs = New ADODB.Recordset
    rs.CursorLocation = adUseClient
    ''add your fields
    rs.Fields.Append "A", adVarChar, 100
    rs.Fields.Append "B", adVarChar, 100
    rs.Fields.Append "C", adVarChar, 100
    rs.Fields.Append "D", adVarChar, 100
    rs.Fields.Append "E", adVarChar, 100
    rs.Fields.Append "F", adVarChar, 100
    ''Open recordset
    rs.Open , Nothing
    ''add your records
    For i = 1 To 5
        rs.AddNew
        rs.Fields("A").Value = i
        rs.Fields("B").Value = i
        rs.Fields("C").Value = i
        rs.Fields("D").Value = i
        rs.Fields("E").Value = i
    Next   
    ''bind to grid
    Set DataGrid1.DataSource = rs
    Dim dc
    Set dc = DataGrid1.Columns.Add(2)
    dc.Caption = "F"
    dc.DataField = "F"
    dc.Button = True
''set type for F is button type. when this button is clicked, a listbox is displayed to set data for the row of F
Private Sub DataGrid1_ButtonClick(ByVal ColIndex As Integer)
    With List1
      .Text = DataGrid1.Text
      .Visible = True
      .SetFocus
      ''set listbox above datagrid
      .ZOrder
End With
<pre lang="vb">Private Sub DataGrid1_KeyDown(KeyCode As Integer, Shift As Integer)
   Dim AltDown As Long
   If Not List1.Visible Then
      AltDown = (Shift And vbAltMask) > 0
      If DataGrid1.Col = 2 Then
         If AltDown And KeyCode = vbKeyDown Or KeyCode = vbKeyReturn Then
            Call DataGrid1_ButtonClick(2)
            KeyCode = 0
         End If
      End If
   End If
End Sub
''when double click on listbox(List1),the data of list1 will be set into datagrid
Private Sub List1_MouseUp(Button As Integer, Shift As Integer, x As Single, Y As Single)
   If mbDblClick Then
      mbDblClick = False
      Call List1_KeyDown(vbKeyReturn, 0)
   End If
End Sub

Private Sub List1_KeyDown(KeyCode As Integer, Shift As Integer)
   Select Case KeyCode
      Case vbKeyReturn
         DataGrid1.Text = List1.Text
         List1.Visible = False
         DataGrid1.SetFocus
      Case vbKeyEscape
         List1.Visible = False
         DataGrid1.SetFocus
   End Select
End Sub



这是我的代码,但是在运行时,我的应用程序显示错误找不到列,| 1",行错误为"DataGrid1.Text = List1.Text"



this is my code but when running, my application show error "Column not found, |1" and row error is " DataGrid1.Text = List1.Text"


这篇关于错误在VB6中将数据设置为datagrid的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆