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

Clear data from Grid

Status
Not open for further replies.

TheLazyPig

Programmer
Sep 26, 2019
106
PH
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]
 
Hi,

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")
go_Form.Show

READ Events
CLEAR ALL


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"

			ENDWITH
			
			SELECT (lcAlias)
			
		ENDPROC 
		

[highlight #FCE94F]*!*	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")				
				
		ENDPROC[/highlight]

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

	PROCEDURE Destroy()
		ThisForm.cmdExit.Click()
		
	ENDPROC
    
	PROCEDURE Load()
		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   
			
		ELSE

			USE tblNames
		ENDIF 
					

	ENDPROC
ENDDEFINE

hth

MarK
 
If you prefer not to make the grid invisible, you could simply remove its RecordSource, like this:
[tt]
THISFORM.MyGrid.RecordSource = ""[/tt]

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


__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads
 
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:
USE ?
BROWSE NAME oBrowse NOWAIT
? 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
 
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:
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] )
Thisform.Refresh



-Tom
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top