Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Clear data from Grid

Clear data from Grid

Clear data from Grid

Hi again :) I have a Clear button that when I click it should remove the data from my grid without actually deleting it. I can only clear textbox but grid stays the same. Thank you smile2

RE: Clear data from Grid


Just make the grid invisible e.g.

ThisForm.MyGrid.Visible = .F.

or you may want to toggle e.g.

ThisForm.MyGrid.Visible = !(ThisForm.MyGrid.Visible)

Don't forget to change the caption of the button accordingly

You may have a look at the code below

CODE -->

PUBLIC go_Form

go_Form = CREATEOBJECT("frmForm")

READ Events

DEFINE CLASS frmForm As Form
	Width = 420
	Height = 360
	MinWidth = 420
	MinHeight = 360
	MaxWidth = 420
	MaxHeight = 360
	AutoCenter = .T.

*!*	Add a grid to the form

	Add Object grdNames as Grid with;
		Visible = .F., ;
		Top = 48, Left = 18, Width = 390, Height = 264, DeleteMark = .F.
*!*	Add object Labels

	ADD OBJECT lblInfo as Label WITH ;
		Top = 12, Left = 280, Autosize = .T., FontSize = 8, FontItalic = .T., ;
		Caption = "Number of Records: " 
	ADD OBJECT lblSearch as Label WITH ;
		Top = 12, Left = 12, Autosize = .T., FontSize = 8, FontItalic = .T., ;
		Caption = "Enter string to search for in Name" 
*!*	Add object Textbox

	ADD OBJECT txtSearch as Textbox WITH ;
		Top = 12, Left = 192, Width = 60, Height = 24

*!*	ADD a Browse button - allows you to filter the underlying table(s)

	ADD OBJECT cmdBrowse As CommandButton WITH ;
		Width=60, Height=30, Left=84, Top=318, Caption="Browse"

		PROCEDURE cmdBrowse.Click()
			LOCAL lcAlias
			lcAlias = ALIAS()
			SELECT cName, nMeters, nSquare, nVolume ;
				FROM tblNames ;
				WHERE IIF(EMPTY(ALLTRIM(ThisForm.txtSearch.Value)), .T., AT(ALLTRIM(ThisForm.txtSearch.Value), cName) != 0) ;
				INTO CURSOR csrResults 

			Thisform.lblInfo.Caption = "Number of Records: " + TRANSFORM(_Tally, "999,999")
			WITH ThisForm.grdNames
				.ColumnCount = -1
				.RecordSource = "csrResults"
				.Visible = .T.
				.ReadOnly = .T.
				.SetAll("Sparse", .F., "Column")

				.Column1.Width = 72
				.Column1.Header1.Caption = "Name"
				.Column1.Text1.FontBold = .T.
				.Column1.Text1.FontItalic = .T.

				.Column2.Width = 72
				.Column2.Header1.Caption = "Me"
				.Column2.Text1.InputMask = "9,999.99"
				.Column3.Width = 90
				.Column3.Header1.Caption = "Sq"
				.Column3.Text1.InputMask = "999,999.99"
				.Column4.Width = 90
				.Column4.Header1.Caption = "Vo"
				.Column4.Text1.InputMask = "999,999,999.99"

			SELECT (lcAlias)

*!*	Add clearbutton to the form
	ADD OBJECT cmdClear As CommandButton WITH ;
    	Width=60, Height=30, Left=150, Top=318, Caption="Clear"
		PROCEDURE cmdClear.Click()
			ThisForm.grdNames.Visible = !(ThisForm.grdNames.Visible)
			ThisForm.cmdBrowse.Enabled = IIF(ThisForm.grdNames.Visible, .T., .F.)				
			This.Caption = IIF(ThisForm.grdNames.Visible, "Clear", "Restore")				

*!*	Add exitbutton to the form
	ADD OBJECT cmdExit As CommandButton WITH ;
    	Width=60, Height=30, Left=18, Top=318, Caption="Exit"
		PROCEDURE cmdExit.Click()
			CLEAR Events
*!*	ADD code to form's events

	PROCEDURE Destroy()
		IF !FILE("tblNames.dbf")
			Create Table tblNames (cName C(10), nMeters I, nSquare I, nVolume I )
				For li_I = 1 to 500
					INSERT INTO tblNames (cName, nMeters, nSquare, nVolume) ;
							VALUES ("Name" + PADL(li_I,3,"0"), li_I, li_I ^ 2, li_I ^ 3)
				Next li_I   

			USE tblNames




RE: Clear data from Grid

If you prefer not to make the grid invisible, you could simply remove its RecordSource, like this:

THISFORM.MyGrid.RecordSource = ""

The disadvantage of that is that will also remove all the formatting of the columns, including things like the column headers, which might not be what you want.

If you are using a cursor as the RecordSource (as opposed to a physical table), you could delete all the records from the cursor and then refresh the grid.

If you are using a physical table, you could copy the table to a cursor, and use that to populate the grid, and then proceed as above. That would be a good solution if the grid is read-only. If it is not, you would need to do some additional work to send edits to the cursor back to the original table.


Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads

RE: Clear data from Grid

I don't think you want to make it invisible. Just see empty cells.
You could SET FILTER TO .F. and I think it's the only viable option to keep all columns and be prepared for showing the next result with another filter.

If you remove the Redcordsource you lose the columns. It might be okay if you WANT the grid to reconstruct and have other columns and headers for a completely different result, not just a filter of data but the result of another query. You'll just typically want nice human-readable columns, maybe multi-line maybe with spaces ion the caption, that just setting a grid.recordsource won't give you, it'll give you the technical field names, which by rules of names in VFP a) can't start with a digit and b) can't contain spaces as the most important rules.

If you seek out a general grid like a browse window is, you're still not totally wrong about being able to get there, it's not what I would design, you design your grids visually at design time and use them for a specific result composition of columns and have different forms for the different result lists you want to show. It seems tedious, but it never took just even 1% of project time to design all the different grids necessary, there are also assistant/wizards helping you about that.

Anyway, to show the grid is as capable as BROWSE, simpler notice, that BROWSE is a grid:

CODE -->

? oBrowse.baseclass 

I mention this because legacy programmers tend to use the BROWSE command a lot as a way to display data and it's an annoyance in VFP which isn't SCREEN-program oriented, but a Winforms platform and BROWSE will be separate windows with only a grid. You just want to use a grid control. If you don't find any features of the BROWSE and EDIT commands in the grid, you just haven't read enough about how the grid works. In some sense, it is more work to set up all the grid properties and all its child objects like columns, their headers, the controls of each column and handle the quirks of it, which are by design. Using a grid as versatile control to display any list is possible also without having to use specific grids for each different result, the "bug" of grid reconstruction becomes the feature you want, then.

So just read about the grid in the help to get a better feel for it.

To get the arc back to your question: You can also set Grid.recordsource="", which blanks the grid, but at the same time means setting the recordsource to another alias, or the same after having done a query, you get new columns. It's quite the same legacy VFP programmers are used by BROWSE and EDIT, as these even create a fully new grid every time you call them. The topic of reconstruction, therefore, is just "normal", there is no reconstruction there always is a completely new construction. It just doesn't fit the Winforms platform, as each BROWSE/EDIT is a WinForm in itself and you don't want that in your own forms, you want a control on your form, not a new form.

Bye, Olaf.

Olaf Doschke Software Engineering

RE: Clear data from Grid

When clearing a grid, you can create a temporary cursor that gets assigned to the grid, then clear or rebuild the original cursor and reassign it. That way no grid specialties are removed/deleted/destroyed.

This can be done rather easily

CODE --> Foxpro

select * from myOrigCrs where 1 != 1 into cursor myOrigCrs_temp
Thisform.myGrid.Recordsorce = [myOrigCrs_Temp]
use in select( [myOrigCrs] )
select * from myOrigCrs_Temp into myOrigCrs readwrite
Thisform.myGrid.Recordsorce = [myOrigCrs]
use in select( [myOrigCrs_Temp] )


Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close