help adding numbers from a datagridview
-
2012年2月21日 15:28
These are my two forms. Every time I click on the total button I want all the total to be displayed on the toal form.
すべての返信
-
2012年2月23日 9:37モデレータ
Hi,
first of all I want to recommand the Windows Forms General forum for your questions. (It is not related to the developer documentation or MS Help system): http://social.msdn.microsoft.com/Forums/en-US/winforms/threads
I am not sure, what kind of help you need - In general it is a bad idea to have dependencies between 2 forms - is there a reason why you placed the totals inside it's own form and not added it into the other form? The generic design in my eyes would always be:
You have business objects behind who hold the data. So the data is not held inside the GUI. The GUI only represents them. So there is no need to calculate a sum through going the cells of the datagridview. Instead you use the objects that the gridview is bound to. So the 2nd form just has it's own business objects witch could tell them the totals. On clicking the button an event could be raised which goes into an application object or so which then informs all Total Forms that the totals should be renewed. (There could be 0...n of these forms!)That would be the way I would build it - simply through using events.
With kind regards,
Konrad
-
2012年2月23日 10:21
Hi,
I totally understand what you saying. I just need a way to show the total for each person. It doesn't matter if it is on the same form. I just need a way to show the total for each person. those totals could be on the same form as the datagrid, meaning I will only have one form but I just need a way to let the user know what the total for each person is after all the values are inserted
-
2012年2月23日 10:59モデレータ
Can you give more details how you filled the datagridview? How do you store the data in your client? Or is it only inside the datagridview?
What I didn't suggest but what is possible is to simply go through the rows and have something like this:
Dictionary<String, double> Totals = new Dictionary<String, double>();
foreach (var row in dataGridView1.Rows)
{
Totals[(string)row.Cells[1].Value] += (double)row.Cells[2].Value;
}I didn't test the code right now but that logic could work where the row.Cells[1] should contain the name. (Maybe you have to do a cast!) and row.Cells[2] should have the value to sum up!). But check the types you store inside the Cells - maybe you do not store the value as double so it might need a change ...
And Maybe you have to initialize the Totals dictionary a little bit more e.g.
If (!Totals.Keys.Contains((string)row.Cells[1].Value))
Totals[(string)row.Cells[1].Value] = 0;Once you got teh values, you can set the values to the TextBoxes in the form (on whatever way you like!).
WIth kind regards,
Konrad

