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!

Fixing columns in a grid

Status
Not open for further replies.

Jerim65

Technical User
Aug 8, 2010
99
AU
I am very keen to 'fix' the first three columns of a grid which will always display 4 columns - Y,M,D,and a Memo field.

I have found thread184-1255205 but I am unsure how to get it to work.

My grid is created programmaticly from a form.

In the methods I have things like Procedure BtnExit.click() which is run by clicking the BtnExit objectI have added and programmed code for.

I have tried both Procedure Scrolled and Procedure AfterRowColChange but I don't know how to get them recognised in my grid.

Can anyone help please.

Ultimately I would like the first 3 columns to be fixed so If an edited version of the code for 2 columns given in the thread could be given I would appreciate that to.

Regards

Coldan
 
Hi,

I dont understand what you are trying to say with -my grid is created programmaticly from a form-
Anyway I would suggest to make a gridclass and add some properties/ methods
ScrolledFreezed() method to perform the scrolling
FreezeColumn placeholder for the number of Columns counting from the left, to be freezed.

native Scrolled method you write:
This.ScrolledFreezed(nDirection)

in your ScrolledFreezed method you write:
Code:
Parameters tnColIndex

Local lnCol, lnCurrOrder, lnColnmbr, lcCol2Focus
Local lnColToFocus

lnCol = 1
* tnColIndex
*!*	0  Up arrow on vertical scroll bar.
*!*	1  Down arrow on vertical scroll bar.
*!*	2  Vertical scroll bar in area above the scroll box.
*!*	3  Vertical scroll bar in area below the scroll box  / right arrow
*!*	4  Left arrow on horizontal scroll bar.
*!*	5  Right arrow on horizontal scroll bar.
*!*	6  Horizontal scroll bar in area to the left of the scroll box.
*!*	7  Horizontal scroll bar in area to the right of the scroll box.


lnColNmbr = 0
lnCol = This.ActiveColumn
lnColToFocus = 0
If lnCol > 0
	For lnColNmbr = 1 To This.ColumnCount
		If This.Columns( m.lnColNmbr ).ColumnOrder = m.lnCol
			lnColToFocus = m.lnColNmbr
			Exit
		Endif
	Endfor
Endif
* Code from Vlad Grynchyshyn
* to be downloaded at UT - FAQ 8151

* Check if we do not need to set focus to the last column in grid
* This will happen when we did not focused column explicitly by
* mouse and pressed either Shift+TAB, left arrow key or Ctrl+left
* arrow key. In the last condition we check if column focused is
* the locked column. When you have 2 columns, you should check for
* both (for example,
* "AND (m.lnColToFocus = 1 OR m.lnColToFocus = 2)")
ThisForm.Lockscreen = .t.
If Not Mdown() And Inlist(Lastkey(), 15, 19, 26) And (m.lnColToFocus = 1)
	This.Columns(This.ColumnCount).SetFocus
	m.lnColToFocus = This.ColumnCount
Endif

Do Case

	Case This.FreezeColumn > 1

		m.lnCurrOrder = This.Columns(1).ColumnOrder
		If This.Columns(1).ColumnOrder < This.LeftColumn  && we scroll forward
			For lnColNmbr = m.lnCurrOrder To This.LeftColumn - 1
				This.Columns(2).ColumnOrder = This.Columns(2).ColumnOrder + 1
			Endfor
			For lnColNmbr = m.lnCurrOrder To This.LeftColumn - 1
				This.Columns(1).ColumnOrder = This.Columns(1).ColumnOrder + 1
			Endfor
		ELSE  && we scroll backward
			For lnColNmbr = This.LeftColumn + 1 To m.lnCurrOrder
				This.Columns(1).ColumnOrder = This.Columns(1).ColumnOrder - 1
			Endfor
			For lnColNmbr = This.LeftColumn + 1 To m.lnCurrOrder
				This.Columns(2).ColumnOrder = This.Columns(2).ColumnOrder - 1
			Endfor
		Endif
	OTHERWISE  && This.FreezeColumn =< 1

		m.lnCurrOrder = This.Columns(1).ColumnOrder
		If This.Columns(1).ColumnOrder < This.LeftColumn  && we scroll forward
			For lnColNmbr = m.lnCurrOrder To This.LeftColumn - 1
				This.Columns(2).ColumnOrder = This.Columns(2).ColumnOrder + 1
			Endfor
			For lnColNmbr = m.lnCurrOrder To This.LeftColumn - 1
				This.Columns(1).ColumnOrder = This.Columns(1).ColumnOrder + 1
			Endfor
		ELSE  && we scroll backward
			For lnColNmbr = This.LeftColumn + 1 To m.lnCurrOrder
				This.Columns(1).ColumnOrder = This.Columns(1).ColumnOrder - 1
			Endfor
			For lnColNmbr = This.LeftColumn + 1 To m.lnCurrOrder
				This.Columns(2).ColumnOrder = This.Columns(2).ColumnOrder - 1
			Endfor
		Endif
Endcase
ThisForm.Lockscreen = .F.
drop the grid on your form and on the form you make a method CreateGrid

here you mark the to be freezed columns with Movable=.f.
something like:

Code:
loGrid.Columns[1].Movable = .F.
loGrid.Columns[2].Movable = .F.

Now when you run your form the first 2 Columns will stay put and all the other can be moved freely around. Even when your grid is wider than the visible part you can pan through the grid by means of using the Tab Key.

Regards,

Jockey(2)
 
Mike,

I have experienced that when you use these proporties the moving facilities, - to move columns around in the grid - is enabled. Can you do freeze columns and move the others around?

Best regards,

Jockey(2)
 
Jockey,

You talk about moving columns around the grid. That has got nothing to do with partitioning. The Movable property is independent of partitions.

Or did you mean that you don't want to let the user scroll the left-hand partition horizontally? If so, then I agree that would be a limitation of this technique.

Mike



__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips, training, consultancy
 
Wait wait wait. Why so complicated? Use Grid.Partition = 3 to partition the grid and split it after the 3rd column. Both partitions are then individually scrollable.

Bye, Olaf.
 
Mike,
I am referring to the coding in thread184-1255205 - which part of the initial question in this thread.
This coding ( from Vlad) is specifically made with the intention to keep the moving methods of grids enabled and also to lock one or more columsn. Indeed if you dont mind the moving is disabled than of course it is more efficient to just lock one or more columns with the native properties of a grid as you mentionned.

Regards,

Jockey(2)
 
I'm not Mike, but I see he already suggested to use the Partition property. It enables to do all you say and is much less complicated. Why would you follow the root of a solution, if you know or get to learn a much simpler one?

Bye, Olaf.
 
Hi Olaf,

Maybe I dont understand it but Jerim65 was referring to the class of Vlad (Thread 184-1255205) and his original question was : "I would like the first 3 columns to be fixed". So I tried to give him reply by using Vlad's 'freeze' class to freeze n Number of Columns by means of making an extra property on the Grid: 'FreezeColumns'.

The beauty of Vlad's class is that it combine's a lock of n number of columns AND enabling the moving of the others.

Obviously there will and should be more ways to skill the fox, but since Jerim65 has already implented Vlad's class it seemed to me a simple thing extra to make it work for 3 Columns.

Regards,

Jockey(2)
 
Actually I'd still argue Partition being easier than extending that class.

You can simply set the number of columns in Partition. So still easier than to modify a complicated class just for the feature of fixing the first partition. you can also do that by setting no scrollbar for the left partition.

It's up to Coldan to decide.

Bye, Olaf.
 
I also still think that using partitions is easier. It's true that you can't prevent the user scrolling the left-hand partition horizontally, but is that really such a problem? Given that the aim is to let them see the left-most columns while srolling the others, it seems the obvious way of doing it.

Mike


__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips, training, consultancy
 
Thanks for the conversation everyone.

Whilst I had tried to achieve my aim adding the code my REAL question hasn't been answered , viz

<I have tried both Procedure Scrolled and Procedure AfterRowColChange but I don't know how to get them recognised in my grid.>

Whilst I had tried and failed with that approach I was simply looking for a method of achieving the fixed columns a la spreadsheet.

so I have added the partition method thus in my form code

<code>

Add Object grdEdit As Grid With Height=330, Width =200,Anchor=15,;
deletemark=.F.,SplitBar=.F.,RecordMark=.F.,ScrollBars=2,GridLines=0,;
AllowCellSelection=.T.,Partition = 3

</code>

Partition 3 did not work afaik so I am trying 1 col fixed but I cannot achieve even that.

With the above grid my right tabing takes cols 1 to 3 to the left ( thus hidden ) with only the col 4 (Memo field) in view.

I am obviously doing something wrong so please help me here..

Coldan
 
Coldan,

I will concentrate on the class made by Vlad and help you with that, will leave the other method Partition to other participants.

your Question:
<I have tried both Procedure Scrolled and Procedure AfterRowColChange but I don't know how to get them recognised in my grid.>
1. Add a newmethod in your gridclass : ScrolledFreezed
in this method you write the code from Vlad -little bit modified - as I have shown you here in my previous reply.

2. Add a property in your gridclass : FreezeColumn

3. in the Scrolled method you write:
Code:
This.ScrolledFreezed(nDirection)

4. drop the gridclass on your form

5. in the gridclass on your form you set the number of columns you want to freeze - in your case 3. So write this value in the FreezeColumn property.

6. in the init of the gridclass you set the columns you want to freeze to movable=.f. so in this case something like:

Code:
loGrid = ThisForm.myGridclass
loGrid.Columns[1].Movable = .f.
loGrid.Columns[2].Movable = .f.
loGrid.Columns[3].Movable = .f.

now populate your grid, viz. set the recordsource a.s.o.
your done.

Hope this works for you. If not switch to plan B (Partition)

Regards,

Jockey(2)

P.S. For my understanding what do you actualy want to say with --My grid is created programmaticly from a form.-- ??
 
Major correction: Partition = 3 will make a partition quite invisibly, as it's not the number of columns but rather the number of pixels. Make it something like PArtition = 200 and you'll see.

If your main problem is columns scrolling when you tab to the 4th memo column, then I think I already told you to have an eye on the exact pixel width of the columns, the last right gridline of the rightmost column must be visible in the grid, otherwise you get scrolling as that 4th column you tab into is not fully visible. The VFP grid is that pednatic on the pixels and so you have to be.

Bye, Olaf.
 
Hi Jockey2,

When I say my grid is placed on a form programatically I mean that the whole code is in a prg file.

Create form
Create grid 1
Create grid 2

So as I don't have extensive knowledge I am not sure how to include your suggestions -


1. Add a newmethod in your gridclass : ScrolledFreezed
- do you mean by adding a procedure just as I did with my button actions?

2. Add a property in your gridclass : FreezeColumn

This I do not know how to express in code unless you mean

<code>
Add Object grdEdit As Grid With Height=330, Width =700,Anchor=15,;
deletemark=.F.,SplitBar=.F.,RecordMark=.F.,ScrollBars=2,GridLines=0,AllowCellSelection=.T.,FreezeColumn = .t.
</code>
Am I correct? If so I will carry on with the coding.

Many Thanks

Coldan

 
Coldan said:
When I say my grid is placed on a form programatically I mean that the whole code is in a prg file.

Well, there's where you are going wrong. Totally wrong.

Code:
Add Object grdEdit As Grid With Height=330, Width =700,Anchor=15, deletemark=.F.,SplitBar=.F.,RecordMark=.F.,ScrollBars=2,GridLines=0,AllowCellSelection=.T.,FreezeColumn = .t.

This should be Add Object grdEdit AS YourGridClass

When you define your own classes, you can easily add your own properties and methods to those classes. That is what people have been trying to tell you to do. See the DEFINE CLASS help topic.

But if you INSIST on working only in code, I fear you're using the wrong product. You've jettisoned the best features of the tool and chosen to work in the least productive and most frustrating way.
 
Coldan,

Ah I had a suspection like that. VFP has a possibility to make and add object visualy to a form. This also means you can make classes - subclasses a.s.o.
I am afraid this will go way beyond this originalquestion 'Fixing columns in a grid'. And I suggest you to buy and study e.g. the book '1001 Things You wanted to know from Visual FoxPro' by Andy Kramek c.s.
Meanwhile: a very short how to do:
1. Mainmenu: Open->New select Form save it as myFristForm
2. Mainmenu: Open->New select Class give classname: myGrid, class to be based on Grid and stored in a classlibrary - you name it myFirstLibrary
3. Now you will see a grid. You have to add the methods and the properties as per my previous posts. For this you select in the MainMenu Class -> New Property. Name -as per my example: FreezeColumn you give as default 0 and save.
MainMenu CLass -> New Method. Name - as per my example: ScrolledFreeze and save.
4. Select the grid, right click and select from the ContextMenu: Properties
Now you will be presented with a grid with all the properties and methods to that grid.
You select methods Scrolled and ScrolledFreeze and write the coding as advised.
save the grid.
5. Select / open the myFirstForm. From the MainMenu you look for the button 'view classes' most probably left next to an arrow. Click Add and locate for the library containing your grid class, in this case locate for myFirstLibrary
Now you will find an extra icon indicating your gridClass.
Click that class and visualy drag and drop it on your form.
6. Open your form properties (right click) and look for the Grid and look for the property FreezeColumn in your Grid, change 0 to 3
7. In the init of the form you have to write some code to instantiate the grid and make the first 3 columns locked:

loGrid = ThisForm.myGrid1
loGrid.Column[1].Movable = .f.
loGrid.COlumn[2].Movable = .f.
loGrid.Column[3].Movable = .f.
loGrid.ControlSource = myTable

and if I haven't forgotten anything you are supposed to be done.
Save your form see to it that myTable is in the actual path and you should be able to run.

Regards,

Jockey(2)

However I am afraid this whole thing is far from complete. On the other hand the big advantage is that once you get to know how this works you are able to make use of this class on all other new forms. And ofcourse not only this class but virtualy any class you make.


 
Jockey2,Dan,Olaf and Mike

Thanks for your kind efforts to help me.

Firstly,whilst Jockey2 says I am wrong to use the method I showed above - I have to say that I got that code here and have modified the code extensively as I understand it.

I am a 'beginner' of some 69years of age and have been attempting for some 10 years to build applications using older methods that I learned many years ago with dBase and Clipper. So I ask questions here and incorporate the given solutions into my code where I can.

And for me it works pretty well. I know there are better ways but I use what knowledge I have and am not complaining.

I have learned heaps here and and my applications are appreciated by those who use them. I need to use VFP because the 3rd party I write my utilities for is written in VFP.

Notwithstanding that I will always try and incorporate the ideas given here.

Thanks Coldan
 
Goldan,

do understand me correctly, I dont say you are using a wrong method, there are different ways to skill a fox and myself only a youngster of 65 I have seen lots of ways. What I was trying to tell you that my reply to your answer was based on the fact you knew how to make classes and you knew how to handle the visual part of VFP. Surely you can also make an application by typing all the ness. commands into one or more prg files.
I would strongly advise you to buy the book I have advertised and I can promiss you that it will not take you more than a week before you fully understand how VFP could also, and how it is actualy planned to, work. After you have gotten the first experience you will not want to write your applications anymore like you have done in dBase or Clipper.

Good luck lots of success and dont hesitate to aks for help with learning the Visual VFP.

Best regards,

Jockey(2)
 
Hi Jockey(2)

<Good luck lots of success and dont hesitate to aks for help with learning the Visual VFP.>

Thanks for your kind words!

With both of my utilities nearly 6 years old - after constant revisions I can't imagine changing the way they are structured right now.

Maybe when I 'give up' development I will attempt to get into the 'Visual FoxPro' way of life you describe.

Whilst I use many many 'snippets' of code given here that contain classes I have never created any from scratch. However the snippets are heavily modified to meet my needs.

I'm not sure how I can force my head around 180 degrees to think in those terms - maybe it is too late in life. I know its supposedly never too late but my memory isn't what it used to be.

Oh! for a 'mentor'... but that's how I view tek-tips forums.

It surprises me that my 'simple' howto questions often lead to many correspondent's inputs here.

Regards

Coldan
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top