✅ Solved! :D

Heya! I was getting help yesterday and I feel like it's almost fixed, but the calculate should display the sum of everything in the array but it keeps giving the wrong number :/ Any help would be greatly appreciated! Here is the old thread if you'd like context of where we left off: https://discord.com/channels/143867839282020352/1341809906256449597
32 Replies
Katt Does Art
Katt Does ArtOP2d ago
Here is the code:
namespace Group_3_Project_Final
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{

}

DataTable table = new DataTable();

private void Form1_Load(object sender, EventArgs e)
{
table.Columns.Add("Item Description");
table.Columns.Add("Order Quantity", typeof(int));
table.Columns.Add("Unit Price", typeof(int));

dataGridView1.DataSource = table;
}

private void saveBtn_Click(object sender, EventArgs e)
{
table.Rows.Add(textBoxItem.Text, textBoxQty.Text, textBoxUnit.Text);
dataGridView1.DataSource = table;

//Variable declarations:
string strDescription = textBoxItem.Text;
decimal decimalQuantity = Convert.ToDecimal(textBoxQty.Text);
decimal decimalPrice = Convert.ToDecimal(textBoxUnit.Text);


//calculate input cost and save to array
inputCost = decimalQuantity * decimalPrice;
inputCostArray = new decimal[] { inputCost };


//Clear boxes upon saving.
textBoxItem.Clear();
textBoxQty.Clear();
textBoxUnit.Clear();
}

private decimal inputCost;
private decimal[] inputCostArray = Array.Empty<decimal>();

private void calculateBtn_Click(object sender, EventArgs e)
{
decimal sum = 0;
foreach (DataGridViewRow r in dataGridView1.Rows)
{
foreach (var element in inputCostArray)
{
sum += element;
}
}
table.Rows.Add("Extimated Material Cost: ", sum);
}

private void clearBtn_Click(object sender, EventArgs e)
{
Array.Clear(inputCostArray, 0, inputCostArray.Length);
while (dataGridView1.Rows.Count > 1)
{
dataGridView1.Rows.Remove(dataGridView1.Rows[0]);
}
}
}
}
namespace Group_3_Project_Final
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{

}

DataTable table = new DataTable();

private void Form1_Load(object sender, EventArgs e)
{
table.Columns.Add("Item Description");
table.Columns.Add("Order Quantity", typeof(int));
table.Columns.Add("Unit Price", typeof(int));

dataGridView1.DataSource = table;
}

private void saveBtn_Click(object sender, EventArgs e)
{
table.Rows.Add(textBoxItem.Text, textBoxQty.Text, textBoxUnit.Text);
dataGridView1.DataSource = table;

//Variable declarations:
string strDescription = textBoxItem.Text;
decimal decimalQuantity = Convert.ToDecimal(textBoxQty.Text);
decimal decimalPrice = Convert.ToDecimal(textBoxUnit.Text);


//calculate input cost and save to array
inputCost = decimalQuantity * decimalPrice;
inputCostArray = new decimal[] { inputCost };


//Clear boxes upon saving.
textBoxItem.Clear();
textBoxQty.Clear();
textBoxUnit.Clear();
}

private decimal inputCost;
private decimal[] inputCostArray = Array.Empty<decimal>();

private void calculateBtn_Click(object sender, EventArgs e)
{
decimal sum = 0;
foreach (DataGridViewRow r in dataGridView1.Rows)
{
foreach (var element in inputCostArray)
{
sum += element;
}
}
table.Rows.Add("Extimated Material Cost: ", sum);
}

private void clearBtn_Click(object sender, EventArgs e)
{
Array.Clear(inputCostArray, 0, inputCostArray.Length);
while (dataGridView1.Rows.Count > 1)
{
dataGridView1.Rows.Remove(dataGridView1.Rows[0]);
}
}
}
}
I believe this is the problem area specifically:
private void calculateBtn_Click(object sender, EventArgs e)
{
decimal sum = 0;
foreach (DataGridViewRow r in dataGridView1.Rows)
{
foreach (var element in inputCostArray)
{
sum += element;
}
}
table.Rows.Add("Extimated Material Cost: ", sum);
}
private void calculateBtn_Click(object sender, EventArgs e)
{
decimal sum = 0;
foreach (DataGridViewRow r in dataGridView1.Rows)
{
foreach (var element in inputCostArray)
{
sum += element;
}
}
table.Rows.Add("Extimated Material Cost: ", sum);
}
Angius
Angius2d ago
You're not really using the rows you're iterating over
Katt Does Art
Katt Does ArtOP2d ago
What do you mean?
Angius
Angius2d ago
You never use the r inside of the loop So your loop does "for each row in the grid, do nothing with that row"
Katt Does Art
Katt Does ArtOP2d ago
dataGridView1.Items was not a valid thing, so I tried to do it a different way ah
Angius
Angius2d ago
Presumably, you want to get your values from that row
Katt Does Art
Katt Does ArtOP2d ago
The values should be stored in the array that's what I made it for
Angius
Angius2d ago
inputCostArray only ever contains a single value, though
Katt Does Art
Katt Does ArtOP2d ago
All I'm trying to do is get the sum of the array and display it mmm
SG97
SG972d ago
then why iterate the data grids rows?
Katt Does Art
Katt Does ArtOP2d ago
that was not the intent Was a suggestion made yesterday
Angius
Angius2d ago
Perhaps with each save button click you want to add an item to that array?
Katt Does Art
Katt Does ArtOP2d ago
yes?
Angius
Angius2d ago
It would be best to use a list then, not an array You would have a List<decimal> inputCosts field, then you would inputCosts.Add(value) new values to it Then you'd loop over it and sum it up like you're doing now Without the datagrid rows, that is
Katt Does Art
Katt Does ArtOP2d ago
:PepeS: Okay I'll try Under the save button?
Angius
Angius2d ago
calculateBtn_Click does the calculations right now So, presumably, here
Katt Does Art
Katt Does ArtOP2d ago
oh man I have to undo like everything? Also, would (value) be substituted for inputCosts?
Angius
Angius2d ago
No, you wouldn't need to undo much And yes value would be whatever you want to add
MODiX
MODiX2d ago
Angius
REPL Result: Success
List<int> list = new List<int>();

list.Add(67);
list.Add(661);
list.Add(41);

list
List<int> list = new List<int>();

list.Add(67);
list.Add(661);
list.Add(41);

list
Result: List<int>
[
67,
661,
41
]
[
67,
661,
41
]
Compile: 282.535ms | Execution: 28.624ms | React with ❌ to remove this embed.
Katt Does Art
Katt Does ArtOP2d ago
List<decimal> inputCost = new List<decimal>(inputCost); inputCosts.Add(inputCost); Like that? oh wait List<decimal> inputCost = new List<decimal>(inputCost); List.Add(inputCost); ?
Angius
Angius2d ago
No List<decimal> inputCost = new List<decimal>(); would be the field You would use inputCost.Add(...) in the save button code And you would loop overt inputCost to calculate the sum in the calculate button code
Katt Does Art
Katt Does ArtOP2d ago
No description
Angius
Angius2d ago
Which part of "would be the field" did you miss? We went over fields and scopes yesterday
Katt Does Art
Katt Does ArtOP2d ago
oh, I misunderstood sorry
Angius
Angius2d ago
Also, note that you're trying to .Add() to your existing field that's an array Arrays don't have .Add()
Katt Does Art
Katt Does ArtOP2d ago
It worked! :D Thank you so much!!!!
Angius
Angius2d ago
:Ok:
Katt Does Art
Katt Does ArtOP2d ago
It fully works now!!! You have no idea how much you've helped me, this was supposed to be a 4 person project and none of my partners showed up Thank you so much for real
Angius
Angius2d ago
Make sure to sign the project with your name and your name only lol
Katt Does Art
Katt Does ArtOP2d ago
Yeah, 100% I already told my teacher, and the only consolation was that they would not get any points. Still had to do it all alone in the same timeframe so I've been STRESSED
Angius
Angius2d ago
Nice, a decent teacher it seems $close
MODiX
MODiX2d ago
If you have no further questions, please use /close to mark the forum thread as answered

Did you find this page helpful?