Click here to Skip to main content
15,888,579 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello everyone.

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

I get a datagrid as follows:
VB
A B C D E --> header
1 2 4 5 7
6 2 5 1 3
1 9 4 8 9

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

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

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.
Posted
Updated 22-Dec-10 0:42am
v3
Comments
Dalek Dave 22-Dec-10 6:42am    
Edited for Grammar and Readability.
Rajesh Anuhya 22-Dec-10 7:20am    
can give some code.., so that i can help you..
ngthtra 22-Dec-10 21:25pm    
I am loaded some line code, you colud see and help resolve,pls.
thanks

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. ;)

VB
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


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.
 
Share this answer
 
v2
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


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

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900