Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Add DataGrid at runtime 5

Status
Not open for further replies.

brownfox1

Programmer
Jun 20, 2007
11
GB
Is this possible? Maybe with Add.Control...?
 
I'm assuming this is a VB.NET question. If so
forum796 is where it should be asked

for reference you can add any control with the
Me.Controls.Add(<control>)

example
Code:
        Dim gd As New DataGridView
        gd.Name = "gd"
        Me.Controls.Add(gd)

Any further questions please move it to the other forum though

____________ signature below ______________
You are a amateur developer until you realize all your code sucks.
Jeff Atwood

 
No, sorry, visual basic 6. I got add.control from google, didn't realise it a was .net thing
 
I want to do something like this but with a datagrid...
Code:
01 Option Explicit
02 `WithEvents is a way tell the program to
03 `respect all the events that are associated
04 `a CommandButton such as the click event.
05 Private WithEvents cmdMyButton As CommandButton
06
07 Private Sub Form_Load()
08 Set cmdButton = Controls.Add("VB.CommandButton", _ "Button")
09 With mdButton
10 .Visible = True
11 .Width = 3000
12 .Caption = "A real surprise"
12 .Top = 1000
14 .Left = 1000
15 End With
16 End Sub
 
Thanks Bob but I could not get that code to work with a DataGrid only: buttons and text box's...
Code:
Option Explicit
' If you are adding an ActiveX control at run-time that is
' not referenced in your project, you need to declare it
' as VBControlExtender.
Dim WithEvents ctlDynamic As VBControlExtender
Dim WithEvents ctlText As VB.TextBox
Dim WithEvents ctlCommand As VB.CommandButton
 
This works for me:
Code:
 Me.Controls.Add "MSDataGridLib.DataGrid", "NewGrid"
though I've not tried to do much with it other than verify that it places a new entry in the Controls collection and I can make it visible.
 
Thanks Glasgow, but I'm still having problems...
Code:
Set rs = New ADODB.Recordset
    With rs
        .ActiveConnection = conn
        .CursorLocation = adUseServer
        .CursorType = adOpenKeyset
        .LockType = adLockOptimistic
        .Properties("IRowsetIdentity") = True 'remove comment to display data
        .Open statement, , , , adCmdText
    End With
    Me.Controls.Add "MSDataGridLib.DataGrid", "DataGrid2"
Set DataGrid2.DataSource = rs
"Variable not defined" "DataGrid2". I have not posted posted the whole code obviously. Can you help? TIA
 
I don't think you can refer to DataGrid2 in that way.

But something like this may work:
Code:
Me.Controls.Add "MSDataGridLib.DataGrid", "DataGrid2"
Dim DataGrid2 As DataGrid
Set DataGrid2 = Me.Controls(Me.Controls.Count-1)
Set DataGrid2.DataSource = rs
 
This works too:
Code:
Set DataGrid2 = Me.Controls("DataGrid2")
 
Thanks for your help guys. I'm now not getting any error messages but the grid is not displying any data. Here is my code. P.S There is nothing on the form in object view.
Code:
Option Explicit
Dim db_file As String
Dim statement As String
Dim conn As ADODB.Connection
Dim rs As ADODB.Recordset
Dim Time1 As Date
Dim Time2 As Date
Private Sub MDIForm_Load()
MDIForm1.Height = Screen.Height
MDIForm1.Width = Screen.Width
MDIForm1.Left = 0
MDIForm1.Top = 0
    ' Get the data.
    db_file = App.Path
    If Right$(db_file, 1) <> "\" Then db_file = db_file & _
        "\"
    db_file = db_file & "topping_jobs.mdb"
    ' Open a connection.
    Set conn = New ADODB.Connection
    conn.ConnectionString = _
        "Provider=Microsoft.Jet.OLEDB.4.0;" & _
        "Data Source=" & db_file & ";" & _
        "Persist Security Info=False"
    conn.Open
    ' Select the data.
    Time1 = Format(Now(), "dd/mm/yyyy hh:n")
    Time1 = DateAdd("h", -4, Time1)
    Time2 = DateAdd("h", 8, Time1)
    Time1 = Format(Time1, "dd/mm/yyyy hh:nn")
    Time2 = Format(Time2, "dd/mm/yyyy hh:nn")
    statement = "SELECT format(Docket.Date,'dd/mm/yyyy') AS [Date],  format(Docket.Time,'hh:mm') " & _
    " as [Time], Docket.Name, Docket.From, Docket.To, Docket.Notes," & _
    " format(Docket.Price,'Currency') AS Price, Controllers.Controller, " & _
    "Drivers.DriverNumber+' ('+Drivers.FirstName+')' AS DriverNumber, Vehicle.Vehicle, " & _
    "AccountCustomers.AccountNumber+' ('+AccountCustomers.CompanyName+')' AS AccountNumber, " & _
    "IIf([Docket.cancelled]=False,'No','Yes') As Cancelled FROM Vehicle " & _
    "INNER JOIN (Drivers RIGHT JOIN (AccountCustomers RIGHT JOIN " & _
    "(Controllers INNER JOIN Docket ON Controllers.ID = Docket.Controller) " & _
    "ON AccountCustomers.CustomerID = Docket.AccountID) ON Drivers.EmployeesID = Docket.Driver_No) " & _
    "ON Vehicle.ID = Docket.Vehicle  WHERE Docket.Date > #" _
    & Format(Time1, "dd/mm/yyyy hh:n") & "# AND Docket.Date < #" & Format(Time2, "dd/mm/yyyy hh:n") & "# ORDER BY [Docket.Time];"
    Set rs = New ADODB.Recordset
    With rs
        .ActiveConnection = conn
        .CursorLocation = adUseServer
        .CursorType = adOpenKeyset
        .LockType = adLockOptimistic
        .Properties("IRowsetIdentity") = True 'remove comment to display data
        .Open statement, , , , adCmdText
    End With
    Me.Controls.Add "MSDataGridLib.DataGrid", "DataGrid2"
    Dim DataGrid2 As DataGrid
    'Set DataGrid2 = Me.Controls(Me.Controls.Count - 1)
    Set DataGrid2 = Me.Controls("DataGrid2")
    Set DataGrid2.DataSource = rs
'rs.Close
'conn.Close
End Sub
 
A couple of questions:

1. Is the grid itself visible?
2. Have you checked the value of rs.recordcount - i.e. are you convinced that your query is returning data?
 
Ok this is weird:
Code:
Me.Controls.Add "MSDataGridLib.DataGrid", "DataGrid1"
    Dim DataGrid1 As DataGrid
    Set DataGrid1 = Me.Controls(Me.Controls.Count - 1)
    'Set DataGrid1 = Me.Controls("DataGrid1")
    DataGrid1.Visible = True
    MsgBox (DataGrid1.Visible & " " & rs.RecordCount)
Set DataGrid1.DataSource = rs
Gives this message "False 1". So there is a result from the query but the grid is not visible?
P.S I was getting a message saying that "MsDataGridLib.DataGrid" cannot be added because it is referenced but not in use by any items in the project. To correct this uncheck "Remove information about unused activeX controls in Project options", which I have done and that particular warning message has stopped
 
>Gives this message "False 1".

This will show False in the Load event until the form is shown.

>Private Sub MDIForm_Load()
Also, It looks like you are doing this on a MDI form. You will need to do this on a normal form, or on a mdi form by adding a picturebox container to the mdi form and assign the grid's .Container property to the picturebox.
 
I pasted your code (driven by a different query) into an MDI child form's activate event and it worked fine.

You may need to set the grid's .Width property to ensure you are getting a 'reasonable view' of the data.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top