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

In Grid, two fields as one

Status
Not open for further replies.

newtofoxpro

Programmer
Sep 16, 2007
301
IN
CREATE CURSOR myCursor (Fld1 c(40), Fld2 n(15,2))
INSERT INTO myCursor (Fld1,Fld2) VALUES ("Good",741.52)
INSERT INTO myCursor (Fld1,Fld2) VALUES ("Better",9887.52)
SELECT * FROM myCursor INTO CURSOR myCursor
oForm=CREATEOBJECT("myForm")
oForm.show()
READ EVENTS

DEFINE CLASS myForm as Form
ADD OBJECT oGrid as MyGrid
PROCEDURE destroy
CLEAR EVENTS
ENDDEFINE

DEFINE CLASS myGrid as Grid
AllowCellSelection = .T.
RecordSource = "myCursor"
ColumnCount = 1
PROCEDURE Init
SYS(2002)
WITH this.Column1
.Width=200
.RemoveObject('Text1')
.Addobject("CntFld","Cnt1")
.Sparse = .F.
ENDWITH
PROCEDURE Destroy
SYS(2002,1)
ENDDEFINE

DEFINE CLASS Cnt1 as Container
VISIBLE = .T.
BackStyle = 0
BorderWidth = 0
ADD OBJECT Text1 as TextBox WITH ;
Left = 0 ,;
Width = 150 ,;
BackStyle = 0 ,;
BorderStyle = 0 ,;
BackColor = RGB(0,100,100) ,;
CONTROLSOURCE="Fld1"
ADD OBJECT Text2 as TextBox WITH ;
Left = 151 ,;
Width = 50 ,;
BackStyle = 0 ,;
BorderStyle = 0 ,;
BackColor = RGB(0,0,100) ,;
Style = 1,;
CONTROLSOURCE="Fld2"
PROCEDURE Text1.Keypress(nKeyCode, nShiftAltCtrl)
IF INLIST(nKeyCode,5,24)
NODEFAULT
DO CASE
CASE INLIST(nKeyCode,24)
SKIP IN myCursor
IF EOF("myCursor")
GO BOTTOM
ENDIF
CASE nKeyCode=5
SKIP -1 IN myCursor
IF BOF("myCursor")
GO top
ENDIF
ENDCASE
this.Parent.Parent.parent.refresh()
ENDIF
ENDDEFINE

**************************
1. I need when I press up/down arrow Fld2 should change backcolor as Fld1 i.e. I want to show two field as one Column1.

2. AllowCellSelection = .T. I can not change this because I have to set another two fields as Column2

Thanks in advance
Best Regards.
 
I want to show two field as one Column1.

Since you are creating the cursor yourself, you can easily create an additional field which combines the 2 fields and then display it in your Grid as Column1.

Good Luck,
JRB-Bldr
 
Thanks, But it may not align properly. Fld1 is Character and Fld2 is Num.
Or Sorry, I could not understand.
 
Then use TRANSFORM() to change Fld2 from numeric to character. If it still doesn't align, pass a second parameter to TRANSFORM() to right-alight the numeric portion. For example:

[tt]Fld(1) + TRANSFORM(Fld2, "999999")[/tt]

In fact, it still won't align, because you are using a proportional font. You would need to set the column's font to a fixed-width font, such as Courier New.

But I really don't understand what you are trying to achieve. Just remind me why you want to combine the fields in this way.

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads
 

I need it for many reasons. And this is one of that.

1. Stock Sheet.
Column1 = Product​

Column2 = Opening Stock
Fld1 = Quantity
Fld2 = Rate
Fld3 = Value​

Column3 = Inward Stock
Fld4 = Quantity
Fld5 = Rate
Fld6 = Value​

Column4 = Outward Stock
Fld7 = Quantity
Fld8 = Rate
Fld9 = Value​

Column5 = Closing Stock
Fld10 = Quantity
Fld11 = Rate
Fld12 = Value​

Actually it is part of presentation and user-friendliness.

Thank you
 
One more thing.
If the user DblClick/Enter to Column5 i.e. user needs to drill/details, how this sheet has calculated Cl.Stock.
 
Build yourself a container class that contains one control each of the two you want in a single column. Instead of trying to get your individual controls into a column, use your brand new container.

There are other issues that will crop up along the way, but this is the only sane way to attempt two controls in a single column. You turn them into one control!
 
>SELECT * FROM myCursor INTO CURSOR myCursor
The only reason for this is you want your grid readonly. Simply use the readonly property.

A grid is not an excel sheet. If you want it to group columns under the same header either do as dan suggests and make a container with 2 or 4 textboxes you use as columns currentcontrol, or just remove the headers and put in your own. grid.headerheight=0

Bye, Olaf.
 

Sorry, I can not understand. Can you pls give me code example ?

Thanks & Best Regards.
 
Sorry, I can not understand. Can you pls give me code example ?

You don't understand what? Creating your own class or adding it to a grid?

While either can be done in code I don't recommend it. It isn't a code kind of thing. It's a fundamental skill within the visual design tools.

I recommend starting here: There also web-based tutorials that I'm sure someone will provide links to.
 
While you're at it, you may consider creating a container class holding controls for a whole record, use that in a single column grid as the column1 currentcontrol. The single controls then won't inherit the binding from the grid, as the grid will try to set the container.controlsource, which doesn't exist and even if, would only bind the first field. So instead of automatic binding you're setting the controlsources of the controls to the table/cursor fields on your own and you can layout this as you want with grouping, even in two rows per record, if you like.

One way to define a class like this is to layout controls in a new form, I assume you're more familiar with the form editor.
Now select all controls you want in a new class and use File->Save as Class.

The Save As Class dialog will have the save option "Selected controls", you need to specify a name for this, eg "recordcontainer", choose a file (a vcx file), you may create a new one by specifying a file name like "specialcontrols".

Adding the VCX into the project manager or adding the vcx library into your toolbox in the "my base classes" section, you can later drag the recordcontainer class into a grid column.

Bye, Olaf.
 
Thank you pls give me time to experiment the suggestions.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top