Brocke
03-19-2012, 07:09 PM
How would I go about adding items into my gridivew without having controls that are binded. I know you can hide the controls but thats not the right way to do things i wouldnt think.
i think i explained well enough.
Gruff
03-20-2012, 11:06 AM
Do you mean the datagridview control in Winforms or gridview in WPF?
There are different database connnection/recordset classes for database flavors
What is the database type to which you are connecting? Access, SQL Server, Etc...
First realize that this is not a simple subject with a simple answer. You are going to have to do a lot of reading on the topic.
1) In all cases you retreive data into a Dataview object before you display it.
This can get a bit fussy so I wrap the code into a method in a class library so that I can call it easily again and again. I do not see a tutorial in our "Tutors Corner" in our "Knowlege Base" to here is a rough example of reading one kind of database with a method.
Public Class MASDB
' Simple class to read records from a MAS 200. (A ProvideX database)
Private Const MAS200_CONNECTION_STR As String = "<Your connection string goes here>"
Public Function ReadMASDB(ByVal SQL As String) As DataView
Dim Conn As Odbc.OdbcConnection
Dim cmd As Odbc.OdbcCommand
Dim DA As OdbcDataAdapter
Dim DT As DataTable
Dim DV As DataView
Conn = New Odbc.OdbcConnection(MAS200_CONNECTION_STR)
cmd = Conn.CreateCommand()
cmd.CommandText = SQL 'This is your query.
cmd.CommandTimeout = 240
DA = New OdbcDataAdapter
DT = New DataTable
Try
DA.SelectCommand = cmd
Conn.Open()
Catch ex As Exception
MessageBox.Show("My Error: " & ex.Message)
Return Nothing
End Try
Try
DA.Fill(DT)
Catch ex As Exception
MessageBox.Show("My Error: " & ex.Message)
Return Nothing
End Try
DV = New DataView
DV = DT.DefaultView
Conn.Close()
Return DV
End Function
End Class
In Use you define a Dataview variable, Define your custom database class, instatiate the class, call the method, and pass the DataView object to your Datagridview.
Dim oMAS as new MASDB
Dim sQuery as string = "Select * FROM Employees Emp WHERE Emp.Age > 40"
Dim dvReport as DataView = oMAS.ReadMASDB(sQuery)
Grid.DataSource = dvReport
2) The datagridview control requires a lot of setup whether you do it manually at design-time or programmically at run-time. The first step is setting up the columns you wish to display from the dataview. There is a property for the column source field name that is critical. data formatting and other options abound.
Brocke
03-20-2012, 01:44 PM
Thank you for the help, Im using a SQL Database for a database type.
I see that VB .net 2010 does a ton of coding for you in the background which does help to a degree. and your right ill have to do some reading on this subject.
Gruff
03-20-2012, 04:05 PM
In that case you would be using the SQLClient namespace not the ODBC namespace as I did in my example.