Solved

binding data to datagrid control in visual basic 6.0

Posted on 2006-06-11
14
352 Views
Last Modified: 2013-11-25
can anybody provide sample code of how to bind data to datagrid using visual basic 6.0. i am getting customers list from database and want to bind it to datagrid.

thanks,
sandeep
0
Comment
Question by:cognos79
  • 8
  • 2
14 Comments
 
LVL 8

Expert Comment

by:hiteshgupta1
Comment Utility
0
 
LVL 15

Expert Comment

by:JackOfPH
Comment Utility
if you are using microsoft access then here:
Option Explicit()
dim cn as ADODB.Connection
dim rs as Adodb.RecordSet
dim strDataBasePath


Private Sub Form Load()
`****Setting the connection*****

Set cn = new AdoDB.connection
Set rs = new Adodb.Recordset

`****specify the PAth

strDataBasePath = "c:\Nwind.mdb"

`******setting the provider to use

cn.Open "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=`" & strDataBasePath & "`"

`*** important****
cn.CursorLocation = adUseClient


rs.open "Select * From tblOwners", cn, adOpenKeySet, adLockOptimistic

`binding the control

Set DataGrid1.DataSource = rs

Datagrid1.refresh

End sub



0
 
LVL 15

Expert Comment

by:JackOfPH
Comment Utility
If you are using SQL Server then change the Provider...

goto www.connectionstring.com

for a list of connection strings...

I hope I help you

cheers
0
 
LVL 3

Author Comment

by:cognos79
Comment Utility
hey jackofPH,

i am using this code..i did wht you said....there is no error but i dont see any data binded to the datagrid. its strange.

Dim objConn As New adodb.Connection
Dim objCmd As New adodb.Command
Dim objParm1 As New adodb.Parameter
Dim objRs As New Recordset
Dim strSQL

    objConn.Open "Provider=SQLOLEDB;User ID=xxx;Password=xxx;Initial Catalog=xxx;Server=xxxx;Connect Timeout=60;Use Procedure for Prepare=1;Auto Translate=True;Packet Size=4096;Workstation ID=WEBTRACKER"

    ' Set CommandText equal to the stored procedure name.
    objCmd.CommandText = "getcustomeraddr"
    objCmd.CommandType = adCmdStoredProc
   
    strSQL = "SELECT first_name + char(13) + last_name as 'name' From customer where customer_id = 1"
   
    objRs.CursorLocation = adUseClient
    objRs.Open strSQL, objConn, adOpenKeyset, adLockOptimistic
    Set dgCustomers.DataSource = objRs
   
    dgCustomers.Refresh


    objRs.Close
    objConn.Close
    Set objRs = Nothing
    Set objConn = Nothing

any ideas,
sandeep
0
 
LVL 15

Expert Comment

by:JackOfPH
Comment Utility
did you try executing your query in the SQL server? does it returns any records?..
strSQL = "SELECT first_name + char(13) + last_name as 'name' From customer where customer_id = 1"
0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 15

Expert Comment

by:JackOfPH
Comment Utility
change this code...

    objRs.CursorLocation = adUseClient
   
to this

objconn.Cursorlocation = adUseClient

it should be connectionstring not the recordset...
0
 
LVL 15

Expert Comment

by:JackOfPH
Comment Utility
>>>>it should be connectionstring not the recordset...

sorry, it is connection not recordset...
0
 
LVL 3

Author Comment

by:cognos79
Comment Utility
that didnt help me. i solved it like this.

  Dim db As Connection
  Set db = New Connection
  db.CursorLocation = adUseClient
  db.Open "Provider=SQLOLEDB;xxxxx"

  Set adoPrimaryRS = New Recordset
  adoPrimaryRS.Open "select customer from customer", db, adOpenStatic, adLockOptimistic

  Set dgCustomers.DataSource = adoPrimaryRS



can you guys tell me how can i put a radiobutton column in datagrid. for all the rows in datagrid
a radiobutton column has to be added. i want the user to select one radiobutton in the grid on submit button and i want to grab the value of the record he selected. (validation should happen to make sure he selected only one record).

i need to do this ASAP. help me

how to do this.
0
 
LVL 15

Accepted Solution

by:
JackOfPH earned 250 total points
Comment Utility
you mat need to use a commercial grid for that... WHY? becauase datagrid has lack of functionality on what you are aiming for...
have alook... here

http://www.drdelphi.com/orderform.html#bennet-tec

http://www.fpoint.com

1.Ultimate Grid 97 ActiveX (probably the one that we will buy, LOTS of features) at www.dundas.com.
2.GridWiz at www.mcraesoft.com.
3.Objective Grid CCE at www.stingsoft.com

There are also the ones without source code by apex (www.apexsc.com), XGrid at www.green-tree.com, spread 2.5 (a very good one, too) at www.fpoint.com, dbcomplete at www.visualcomp.com, Data widgets 2.0 at www.shersoft.com.



0
 
LVL 15

Expert Comment

by:JackOfPH
Comment Utility
0
 
LVL 15

Expert Comment

by:JackOfPH
Comment Utility
use the true dbgrid...
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

A theme is a collection of property settings that allow you to define the look of pages and controls, and then apply the look consistently across pages in an application. Themes can be made up of a set of elements: skins, style sheets, images, and o…
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now