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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

DataGrid help

Status
Not open for further replies.

savok

Technical User
Jan 11, 2001
303
0
0
AT
Hi, I am loading a datagrid with data from a database using Adodc, but how can I change the width of the columns? and the name of the columns?


Also can anyone post an example of how i can load a datagrid without using the adodc control? I want to click a command button and have it load.

I can open a connection but I dont know what to do from there. Thanks in advance!

Dim strConn
Dim objConn As ADODB.Connection
Set objConn = New ADODB.Connection

strConn = "Provider=MSDASQL;Persist Security Info=False;Extended Properties=;DSN=*;UID=*;SERVER=*;PWD=*"
objConn.ConnectionString = strConn
objConn.Open


After this I guess i need to select a recordset and then do a loop and load the grid..but I am not sure of the syntax.
 
ok i found how to select what i need to get into the grid but i still need the syntax on loading data into a datagrid. Can anyone help?

Set objComm = New ADODB.Command
objComm.ActiveConnection = objConn
objComm.CommandText = "Select * from table"
objComm.CommandType = adCmdText
Set objRec = New ADODB.Recordset
objRec.ActiveConnection = objConn
Set objRec.Source = objComm
objRec.Open
 
Hi Savok,

If you are using the Microsoft DataGrid Control 6.0 (SP4) (MSDATGRD.OCX) and Microsoft ADO Data Control (MSADODC.OCX) then this is the code -

adodc1.connectionstring = something
adodc1.cursorlocation = aduseclient
adodc1.commandtype = adusecommand
adodc1.recordsource = your SQL statement
adodc1.refresh

set dbgrid.datasource = adodc1
dbgrid.refresh

This is the simplest way of doing it. You can use command objects and recordsets if you want to, but the code might get a little longer. Hope this works for you.

- Subha :) Nothing is impossible, even the word impossible says I'm possible.
 
thanks subhavs I'll try it your way as well, seems a lot shorter :)

My other question still stands though, does anyone know how to set the width of the column titles? or how to center them?

Here is a sample code of how i load the grid my problem is that a field like studentid is only 5 chars but the columns width is too large, or a student name is 100 chars but the column width is too small, I need to know how to edit the widths of the columns. Any help would be greatly appreciated :)


Set r = New ADOR.Recordset
r.Fields.Append "Student ID", adVarChar, 10
r.Fields.Append "SSN", adVarChar, 50
r.Fields.Append "Name", adVarChar, 100
r.Fields.Append "Job Title", adVarChar, 150
r.Fields.Append "Hospital ID", adVarChar, 150
r.CursorType = adOpenDynamic
r.Open

If Not objRec.EOF Then objRec.MoveFirst
Do While Not objRec.EOF
r.AddNew
r.Fields(0).Value = objRec!StudentID
r.Fields(1).Value = objRec!SSN
r.Fields(2).Value = objRec!Name
r.Fields(3).Value = objRec!Job_Title
r.Fields(4).Value = objRec!HospitalID
objRec.MoveNext
Loop
Set DataGrid1.DataSource = r

//////////
r.Fields.Append "Student ID", adVarChar, 10
//////////
The 10 in the above line I would assume would set the column width but it does not. I dont know why. There also has to be a way to center it somehow? I could be wrong though.

Thanks
 
Hi Savok,

I need some clarifications because I am lost.

You want to populate records programmatically and then assign this data to a Grid ? You are not fetching the data from a table, am I right ? Because if you are fetching the data, DBGrid will automatically align it for you.

- Subha :) Nothing is impossible, even the word impossible says I'm possible.
 
Never mind, Savok. Please ignore my previous message. I did not read through your code fully. Sorry !

Anyway, I am surprised why the aligning did not happen automatically. Could you try to use aliases for the column names during the fetch ? This way, you do not have to specify the column names and the records will get aligned right after the two controls are bound.

- Subha :) Nothing is impossible, even the word impossible says I'm possible.
 
<<Could you try to use aliases for the column names during the fetch ? >>

How exactly do i do this? This is my first time doing this ;) Not much vb experience ;)

thanks
 
ok here is the code i got now, changed it to the way you showed me, but the column width is still too large or small, the studentid column is twice its size, and the job title column is too small. Any other suggestions?


Adodc1.ConnectionString = &quot;Provider=MSDASQL;Persist Security Info=False;Extended Properties=;DSN=*;UID=*;SERVER=*;PWD=*&quot;
Adodc1.CursorLocation = adUseClient
Adodc1.CommandType = adusecommand
Adodc1.RecordSource = &quot;Select studentid,substr(ssn,1,3)||'-'||substr(ssn,4,2)||'-'||substr(ssn,6,4) ssn, lastname||', '||firstname name, job_title, hospitalid from Student where lastname = '&quot; & test & &quot;'&quot;
Adodc1.Refresh

Set DataGrid1.DataSource = Adodc1
DataGrid1.Refresh
 
hmm I just been told that its not possible to set the column width in a DataGrid, anyone know if its true or not?

I am about to give up and try using the MS FlexGrid

 
Redid everything in MS Flex Grid and it works great, so if you gonna use a grid dont go with datagrid ;)

thanks for the help
 
I thought the ADO control had to use DataGridControl and couldn't be used with MSFlexGrid?? I'm new at this, but have to get into databases big time.
 
Savok,

What database are you using ? Mine was ORACLE 8.1.7 and I used the ORACLE ODBC Driver and the width adjusted to the size of the column.

Maybe it has something to do with the ODBC driver ...

About the aliases - after every column that you specify in the select statement right before the comma, you need to specify the alias. Ex. select DayPh DayPhone, EvePh EveningPhone from Employees; In this case DayPhone and EveningPhone would be the titles of your columns instead of the usual DayPh and EvePh.

- Subha :) Nothing is impossible, even the word impossible says I'm possible.
 
yeah Subha i figured out how to do the aliases but the columns still did not adjust and i couldnt find a way to manually set them. I switched to the MSFlexGrid and it lets you set the column width etc...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top