Click here to Skip to main content
15,891,813 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hi,

I am a complete beginner as far as C# is concerned but to get myself going I thought I would convert a VBA Program I wrote to C#.

Using Visual Studio 2010 Pro, C# & Excel 2010 what I can not do is to input a number ( 4x - textBoxes in one Windows Form)via a textBox and store it directly into an single array to enable me to manipulate the numbers before entering the number into 4 cells in one Row.

Please accept my apologies for using the terminology - but any one could kindly point me in the right direction I would appreciate it.

kind regards & Thanks in Advace!

Clive
Posted
Updated 29-Nov-10 7:17am
v2

C#
public void DisplayValues()
{
 decimal[] TextBoxValues=new Decimal[4];
 
 // Going by what Mark said
 TextBoxValues[0]=Decimal.TryParse(TextBox1.Text);
 TextBoxValues[1]=Decimal.TryParse(TextBox2.Text);
 TextBoxValues[2]=Decimal.TryParse(TextBox3.Text);
 TextBoxValues[3]=Decimal.TryParse(TextBox4.Text);

 using xl=Microsoft.Office.InterOp.Excel; 

 xl.WorkBooks myWorkBook;  
 myWorkBook.Open("C:\MyExcelFile.xlsx");
 
 xl.WorkSheet myWrkSheet=myWorkBook.WorkSheets("Sheet1");

 int i;
 //The for loop below will load the values of the array in the Sheet1
 // starting with Range A1 and upto A4. 
 for(i=0;i<=3;i++)
  {
   myWrkSheet.Range("A1").Offset(0,i).Value=TextBoxValues[i];
  }
}


Cheers! :-D
 
Share this answer
 
protected void ProcessRows()
{
   decimal[] _dRowValues = new decimal[4];

   dRowValues[0] = Convert.ToDecimal(TextBox1.Text);
   dRowValues[1] = Convert.ToDecimal(TextBox2.Text);
   dRowValues[2] = Convert.ToDecimal(TextBox3.Text);
   dRowValues[3] = Convert.ToDecimal(TextBox4.Text);

   // Im guessing here

   DataGridView1.Rows.Clear();

   DataGridView1.Rows.Add();
   DataGridView1.Rows[0][0] = dRowValues[0].ToString();

   DataGridView1.Rows.Add();
   DataGridView1.Rows[1][0] = dRowValues[1].ToString();

   // Im too lazy to start my Visual Studio todo error checking
}
 
Share this answer
 
Comments
[no name] 29-Nov-10 13:23pm    
Almost. You should use Decimal.TryParse or wrap the Convert statements in a try/catch.
DataGridView is a databound control and you should bind the array it rather than add rows manually.
xcorporation 29-Nov-10 13:24pm    
you should pay attention to the question at hand
fjdiewornncalwe 29-Nov-10 13:32pm    
Actually, Mark's response is entirely appropriate. You're answer is making assumptions that users will only enter good data. As well, he is correct that the DataGridView is a databound control and best practice is to use it as such. Your answer is very junior, so you really shouldn't insult senior guys when they correct you, rather learn from it yourself.
xcorporation 29-Nov-10 13:33pm    
ur a f***** joke
Tarun.K.S 30-Nov-10 0:53am    
Clive is talking about storing the array values in the rows of the excel, not to a Datagridview.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900