how to fill datagrid by using this code..?

tommyboy

Active member
Joined
Jun 21, 2005
Messages
34
Programming Experience
Beginner
how to fill datagrid by using this code..? [RESOLVED!!!]

can sum1 help me how can I fill the datagrid (dg1) with data through this code..:)

Try
'SET UP A SQL COMMAND
strSQL = "select SOCSO_id,salary_from,salary_to,Emplyr_Contrbn,Emplyee_Contrbtn from SOCSO "
myConnection = New OleDb.OleDbConnection(sConnString)
Dim myCommand As New OleDbCommand(strSQL, myConnection)
myConnection.Open()
'DO THE QUERY
Dim dr As OleDbDataReader = myCommand.ExecuteReader
dr.Read()
'/////////////////////////////////////
'IS THAT I SUPPOSE FILL HERE IF SO CAN GUIDE ME
'/////////////////////////////////////
dr.Close()
myConnection.Close()
Catch exceptionObject As Exception
MessageBox.Show(exceptionObject.Message)
End Try

=====================

or if u hv any alternative way plz let me know ..:confused:

fyi im using sql server2000 as backend and i prefer oledbConnection...
help me n God may Bless u :rolleyes:
 
Last edited:
Hey,

Its not possible (or so I've heard) to bind a datagrid to a datareader in a winforms application. If it's the extra performance you're after then you could bind the datagrid to a datatable then populate the datatable with the datareader. However I'd suggest binding the datagrid to a dataset then filling the dataset with a dataadapter.
 
erm..gues ur rite...
no wonder keep failin..
do u mind write me an example..:)
 
Use this instead...

Try
'SET UP A SQL COMMAND
strSQL = "select SOCSO_id,salary_from,salary_to,Emplyr_Contrbn,Empl yee_Contrbtn from SOCSO "
myConnection = New OleDb.OleDbConnection(sConnString)
Dim myCommand As New OleDbCommand(strSQL, myConnection)
Dim dt AsNew DataTable
Dim da As New OleDbDataAdapter

myConnection.Open()
'DO THE QUERY
da = myCommand.ExecuteDataAdapter
da.Fill(dt)
myConnection.Close()


'//This assumes You've set up the grid columns already.
datagridName.datasource = dt
datagridName.databind()


dt.dispose()
da.dispose()
myConnection.dispose()
Catch exceptionObject As Exception
MessageBox.Show(exceptionObject.Message)
End Try
 
Back
Top