|
-
Apr 29th, 2013, 03:20 AM
#1
Thread Starter
New Member
Adding Files to DataGridView then exporting it into excel.
Hi I just finished my firs program and now I want to upgrade it. I need the program to take info from the text inserted and calculated and transfer it to a DataGridView. From there all the items in the DataGridView should be transferred to a excel document.
This is the code fro my program:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim num1, num2, num3, num4, num5, num6, num7, num8, product As Single, weight As Single, quantity As Single, total As Single
num1 = TextBox1.Text
num2 = TextBox2.Text
num3 = TextBox3.Text
num4 = TextBox4.Text
num5 = TextBox5.Text
num6 = Val(8.3)
num7 = Val(1000000)
num8 = Val(7.85)
weight = num1 * num2 * num3 * num8 / num7
product = num1 * num2 * num3 * num4 * num6 / num7
total = quantity * product
quantity = product * num5
MaskedTextBox1.Text = weight
MaskedTextBox2.Text = product
MaskedTextBox3.Text = quantity
MaskedTextBox1.Text = CStr(weight) + " kg"
Now when you click button 2 it should send the data too the DataGridView.
The data should be in this format:
Quantity(textbox5) Length(textbox1) x(just an x) Width(textbox2) x Thickness (textbox3) R(Currency in rand)product (maskedTextbox2) R Total(MaskedTextbox3). This Should Look Like This:
10 300x200x12 R74.70 R747.00
Then once all the items are there they should be transferred to an excel sheet in almost the same sequence.
10 300 300 12 R74.70 R747.00
Any help would be appreciated as I know basically nothing about coding.
-
Apr 29th, 2013, 03:56 PM
#2
Re: Adding Files to DataGridView then exporting it into excel.
Over in the .NET CodeBank I have a class that will export datatables (and datareaders) to Excel. It would make more sense to put your data into a datatable rather than directly into a DGV, so that class could be of some use to you. Once you have the data in a datatable, you can display it in a DGV very easily (just set the DGV.Datasource to be the datatable), so using a datatable makes plenty of sense.
What type are the num1...numx? They certainly sound like they are numbers, in which case you shouldn't be putting strings into them, as that isn't a safe thing to do. It will work in some cases, especially with Option Strict OFF, but it will also crash if the string can't be converted well. To fix this, look up .TryParse (wether Integer, Single, Double, or whatever type those numx variables are).
You also use Val to convert a number, but the number is already a number, so Val does nothing but convert it to a double. You never needed to use Val in the first place, so get rid of it. Val would work to convert the textbox.Text strings into doubles, but it has some odd behavior in certain cases, so TryParse is a better answer.
I'm not sure why you put the calculated values back into a masked textbox. Generally, you only put things into editable controls if the user is allowed to edit the values, which they don't appear to be allowed to do in this case. Therefore, using a label would make more sense than using a textbox. The use of a MaskedTextBox makes me think that you did it for some kind of formatting, but there are better ways to format numbers for display.
Finally, your example of the output shows everything in one line separated by spaces. Should all of those go into one cell, or do you want each item in a different cell? The latter would certainly be easier, but it almost looks like you want the former.
My usual boring signature: Nothing
 
-
Apr 30th, 2013, 03:05 AM
#3
Thread Starter
New Member
Re: Adding Files to DataGridView then exporting it into excel.
I am still a noob at this this is my firs project so I don't know what I am really doing. I did recode my program. Now I need to be able to press a button and see all the stuff in the data table.
This is my code.
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim Length, Width, Thickness, Rate, Quantity, Price, Weight,
Const steelweight As Single = 7.85
Const Decimalfix As Single = 1000000
Const steelcost As Single = 8.3
Length = Lengthbox.Text
Width = Widthbox.Text
Thickness = Thicknessbox.Text
Rate = Ratebox.Text
Quantity = Quantitybox.Text
Weight = (Length * Width * Thickness * steelweight) / Decimalfix
Price = (Length * Width * Thickness * Rate * steelcost) / Decimalfix
Totalbox.Text = (Price * Quantity)
Weightbox.Text = Weight
Pricebox.Text = Price
Weightbox.Text = CStr(Weight) + " kg"
If Single.TryParse(Lengthbox.Text, Length) AndAlso Single.TryParse(Widthbox.Text, Width) AndAlso Single.TryParse(Thicknessbox.Text, Thickness) AndAlso Single.TryParse(Ratebox.Text, Rate) Then
'Write your valid calculation code here
Else
MessageBox.Show("Invalid Data in TextBoxes!")
End If
End Sub
I know it still isn't perfect and all is not working as it should. At lease it is calculating correctly. Please help me.
Thank you.
-
Apr 30th, 2013, 11:17 AM
#4
Re: Adding Files to DataGridView then exporting it into excel.
Ok, you are using TryParse pretty well, but since you left all of these:
Code:
Length = Lengthbox.Text
Width = Widthbox.Text
Thickness = Thicknessbox.Text
Rate = Ratebox.Text
Quantity = Quantitybox.Text
they are before the call to TryParse, and they will simply crash if any are left empty. You should remove all of those, then move the subsequent calculation lines down to where you have the comment about performing the calculations. After all, those ARE your calculations, so perform them where you say you should.
My usual boring signature: Nothing
 
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|