By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,502 Members | 1,620 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,502 IT Pros & Developers. It's quick & easy.

Assign dataGridView cell value to variable

P: 1
I am using VC++2008 and am trying to read data enetered in unbound datagridview to implement in function. Under button_click event I entered the following:
Expand|Select|Wrap|Line Numbers
  1. // initialize varaibles from dataGridView
  2. double C1;
  3. double INF1;
  4. double T1;
  5.  
  6. //Read String values from each dataGridView Cell
  7. String^C1s = safe_cast<String^>(dataGridView1[0,0]->Value);
  8. String^INF1s = safe_cast<String^>(dataGridView1[0,1]->Value); 
  9. String^T1s = safe_cast<String^>(dataGridView1[0,2]->Value); 
  10.  
  11. //Convert from String Value to Double 
  12. C1 = Convert::ToDouble(C1s);
  13. INF1 = Convert::ToDouble(INF1s);
  14. T1 = Convert::ToDouble(T1s);
  15.  
The variables are used in a function. Everything compiles fine but when I enter values on first row of datagridview and hit button I get

"An unhandled exception of type 'System.ArgumentOutOfRangeException' occurred in mscorlib.dll Additional information: Index was out of range. Must be non-negative and less than the size of the collection."

I have added following code to form to restrict number of rows as defined in numericupdown box
Expand|Select|Wrap|Line Numbers
  1. private: System::Void dataGridView1_RowEnter(System::Object^  sender, System::Windows::Forms::DataGridViewCellEventArgs^  e)     //raises event handler 
  2.  
  3. {    
  4.   int NUMST;
  5.   NUMST = safe_cast<int>(this->numericUpDown1->Value); //Input from numericUpDown box
  6.   if (this->dataGridView1->RowCount > NUMST) 
  7.          this->dataGridView1->AllowUserToAddRows = false; 
  8. }
  9.  
The number of rows is restricted ok, but I believe that once the AllowUserToAddRows = false the RowIndex is set to -1 and its throwing the out of range error.

Would anybody have a suggestion on how to make this work, or is there a better way to read data from unboubnd datagrid and assign each cell to double numeric variable to be used in function? Thank you.
Nov 19 '08 #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.