Limit Rows in datagridView

Limit Rows in datagridView

Page 1 of 1

3 Replies - 6371 Views - Last Post: 18 November 2008 - 03:06 PM

#1 hydro123  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 24
  • Joined: 20-June 08

Limit Rows in datagridView

Post icon  Posted 16 November 2008 - 02:01 PM

Im using Visual Express C++ 2008 and have created an unbound dataViewGrid in form1. I would like to limit the number of rows that data can be enetered. I tried to raise the Rowleave event handler and then using if-elseif to query the number of rows, if number of rows is less than certain number the user allowed to add rows, if > then cannot. I have placed the following code in the existing cellContentClick event handler;


#pragma endregion
 private: System::Void dataGridView1_CellContentClick(System:bject^  sender, System::Windows::Forms:ataGridViewCellEventArgs^  e) // existing event handler
	{
   
	 
 private: System::Void dataGridView1_RowLeave(System:bject^  sender, System::Windows::Forms:ataGridViewCellEventArgs^  e)	  //raises event handler 
	{
	 int RowCount;	   //initialize RowCount Variable
	 
	 this->dataGridView1->RowCount = RowCount;  //Counts current number of rows in dataGrid and assigns to RowCount
		  {
		  if (RowCount <4)
				 this->dataGridView1->AllowUserToAddRows = true; // if RowCount < 4 use can add row
		 else if (RowCount >=4)
				this->dataGridView1->AllowUserToAddRows = false;// if RowCount >= 4 use cannot add row
		 } 
	}  
	}
  


I continue to get the following errors for the RowLeave event handler;

1>c:\c++ programs\hps1\hps2\hps2\Form1.h(114) : error C2143: syntax error : missing ';' before 'private'
1>c:\c++ programs\hps1\hps2\hps2\Form1.h(115) : error C2143: syntax error : missing ';' before '{'



I also tried to add a constructor in the form above;


public:
  Form1(void)
  {
   InitializeComponent();
   //
   //this->dataGridView1->RowLeave += gcnew System::Windows::Forms:ataGridViewCellEventHandler(this, &Form1:ataGridView1_RowLeave);
   //TODO: Add the constructor code here
   //
  }



but this created a number of other errors. Could anyone offer some suggestions as to how to accomplish this and what Im doing wrong. I've nver used datagridView before and am at a loss. Thank you.

Is This A Good Question/Topic? 0
  • +

Replies To: Limit Rows in datagridView

#2 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: Limit Rows in datagridView

Posted 16 November 2008 - 02:10 PM

View Posthydro123, on 16 Nov, 2008 - 01:01 PM, said:

Im using Visual Express C++ 2008 and have created an unbound dataViewGrid in form1. I would like to limit the number of rows that data can be enetered. I tried to raise the Rowleave event handler and then using if-elseif to query the number of rows, if number of rows is less than certain number the user allowed to add rows, if > then cannot. I have placed the following code in the existing cellContentClick event handler;


#pragma endregion
 private: System::Void dataGridView1_CellContentClick(System:bject^  sender, System::Windows::Forms:ataGridViewCellEventArgs^  e) // existing event handler
	{
   
	 
 private: System::Void dataGridView1_RowLeave(System:bject^  sender, System::Windows::Forms:ataGridViewCellEventArgs^  e)	  //raises event handler 
	{
	 int RowCount;	   //initialize RowCount Variable
	 
	 this->dataGridView1->RowCount = RowCount;  //Counts current number of rows in dataGrid and assigns to RowCount
		  {
		  if (RowCount <4)
				 this->dataGridView1->AllowUserToAddRows = true; // if RowCount < 4 use can add row
		 else if (RowCount >=4)
				this->dataGridView1->AllowUserToAddRows = false;// if RowCount >= 4 use cannot add row
		 } 
	}  
	}
  


I continue to get the following errors for the RowLeave event handler;

1>c:\c++ programs\hps1\hps2\hps2\Form1.h(114) : error C2143: syntax error : missing ';' before 'private'
1>c:\c++ programs\hps1\hps2\hps2\Form1.h(115) : error C2143: syntax error : missing ';' before '{'

You have the RowLeave event handler nested inside the CellContentClick event handler. Separate the routines.
Was This Post Helpful? 0
  • +
  • -

#3 hydro123  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 24
  • Joined: 20-June 08

Re: Limit Rows in datagridView

Posted 17 November 2008 - 08:31 AM

View Postn8wxs, on 16 Nov, 2008 - 01:10 PM, said:

View Posthydro123, on 16 Nov, 2008 - 01:01 PM, said:

Im using Visual Express C++ 2008 and have created an unbound dataViewGrid in form1. I would like to limit the number of rows that data can be enetered. I tried to raise the Rowleave event handler and then using if-elseif to query the number of rows, if number of rows is less than certain number the user allowed to add rows, if > then cannot. I have placed the following code in the existing cellContentClick event handler;


#pragma endregion
 private: System::Void dataGridView1_CellContentClick(System:bject^  sender, System::Windows::Forms:ataGridViewCellEventArgs^  e) // existing event handler
	{
   
	 
 private: System::Void dataGridView1_RowLeave(System:bject^  sender, System::Windows::Forms:ataGridViewCellEventArgs^  e)	  //raises event handler 
	{
	 int RowCount;	   //initialize RowCount Variable
	 
	 this->dataGridView1->RowCount = RowCount;  //Counts current number of rows in dataGrid and assigns to RowCount
		  {
		  if (RowCount <4)
				 this->dataGridView1->AllowUserToAddRows = true; // if RowCount < 4 use can add row
		 else if (RowCount >=4)
				this->dataGridView1->AllowUserToAddRows = false;// if RowCount >= 4 use cannot add row
		 } 
	}  
	}
  


I continue to get the following errors for the RowLeave event handler;

1>c:\c++ programs\hps1\hps2\hps2\Form1.h(114) : error C2143: syntax error : missing ';' before 'private'
1>c:\c++ programs\hps1\hps2\hps2\Form1.h(115) : error C2143: syntax error : missing ';' before '{'

You have the RowLeave event handler nested inside the CellContentClick event handler. Separate the routines.




Thank you for the help. I tried placing outside of cellclickhandler as follows:

#pragma endregion
	private: System::Void dataGridView1_CellContentClick(System::Object^  sender, System::Windows::Forms::DataGridViewCellEventArgs^  e) {
			 }

	private: System::Void dataGridView1_RowLeave(System::Object^  sender, System::Windows::Forms::DataGridViewCellEventArgs^  e)	 //raises event handler   
	{  
	 int RowCount;	 //initialize RowCount Variable  
	   
	 this->dataGridView1->RowCount = RowCount;  //Counts current number of rows in dataGrid and assigns to RowCount  
		  {  
		  if (RowCount <4)  
				 this->dataGridView1->AllowUserToAddRows = true; // if RowCount < 4 use can add row  
		 else if (RowCount >= 4)  
				this->dataGridView1->AllowUserToAddRows = false;// if RowCount >= 4 use cannot add row  
		 }   
	}	



Evrything compiles ok, but has no affect on the datagrid. I can add as many rows as I want???? Is there a different place to put the code? Thank you for any help.
Was This Post Helpful? 0
  • +
  • -

#4 hydro123  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 24
  • Joined: 20-June 08

Re: Limit Rows in datagridView

Posted 18 November 2008 - 03:06 PM

I managed to get this to work;


add constructor to InitializeComponent(); section in form
	 //
	//TODO: Add the constructor code here
   this->dataGridView1->RowLeave += gcnew System::Windows::Forms::DataGridViewCellEventHandler(this, &Form1::dataGridView1_RowLeave);
//



and then within a groupbox I added a NumericUpDown box with the dataviewgrid and added the following in seperate section below the dataGridView1_CellContentClick event handler;

private: System::Void dataGridView1_RowLeave(System::Object^  sender, System::Windows::Forms::DataGridViewCellEventArgs^  e)	 //raises event handler   	
{	
int NUMST;
NUMST = static_cast<int>(this->numericUpDown1->Value); //Input from numericUpDown box
  {	  
	 if (this->dataGridView1->RowCount > NUMST)
	   this->dataGridView1->AllowUserToAddRows = false;// if RowCount >= NUMST use cannot add row  
  }
}



Was This Post Helpful? 0
  • +
  • -

Page 1 of 1