15,967,427 members
See more:
Assets Allocations Assets Percentage
Conservative eqity 10
Conservative debt 10
Conservative cash 30
Conservative commodities 10
Conservative gold 10
Conservative Liqiud 30
Aggressive eqity 10
Aggressive debt 10
Aggressive cash 40
Aggressive commodities 10
Aggressive gold 10
Aggressive Liqiud 20

i want show the Messgebox if total Percentage is not equal to 100
so i dont know how to check through Loop for Conservative and Aggressive percentages .plz can you tell how i can check throught loop and show messagebox .
using C# in the windows applications
Posted
Jibesh 27-Jan-13 0:25am
how did you add the items to the gridviewitem?
i made view for that 3 fields and than i defined the method in the clientbl
return datatable .i bind view in the datatable .

## Solution 1

i dont know how to check through Loop for Conservative and Aggressive percentage
Where ever you are getting this data, maintain a flag for each entry if it is a Conservative type or Aggresive type. Post this, all you have to do is loop through all the items and based on the flag have two counters to have sum of each type. At the end of loop, check the counters for sum to be 100. In case not, raise the message in a message box.

Something like:
C#
```float conservativeSum = 0.0;
float aggresiveSum = 0.0;
for(int i=0; i < Items.Count ; ++i)
{
if(Item[i].IsConservative)
conservativeSum += Item[i].Value;
else
aggresiveSum += Item[i].Value;
}
if(conservativeSum != 100.0)
MessageBox.Show("conservativeSum is not 100");
if(aggresiveSum != 100.0)
MessageBox.Show("aggresiveSum is not 100");```

this my datagridview can i use Items.count
Sandeep Mewara 27-Jan-13 2:13am
Yes.
i have datagridview and three fields
1)AssetsAllocations
2)Asstes
3)Percentages
means Item[i].values can i get percentage(values(10,20,30,40)) for checking 100 or not
Sandeep Mewara 27-Jan-13 2:14am
I shared the full logic and pseudo code. Please try!

Items[i].Values is for example. You need to write your substitute code that will get the values.
Conservative or Aggressive are dynamaic so how can i do ??
plz tell

## Solution 2

DataGridview has a property called Rows which is a collection of DataGridViewRows you can iterate through the datagridview as shows below.

C#
```foreach(DataGridViewRow row in datagridview.Rows)
{
string columnvalue = row.Cells["ColumnsName_AssetAllocation].ToString();//you can also use the column Index

}```

v2