C#Datagridview行单元格值窗口应用程序 [英] C# Datagridview row cells value windows application
问题描述
资产配置资产百分比
保守设施10
保守债务10
保守现金30
保守商品10
保守金币10
保守的Liqiud 30
激进的10级
激进的债务10
激进的现金40
激进的商品10
激进的黄金10
激进的Liqiud 20
>
i想要显示Messgebox如果总百分比不等于100
所以我不知道如何通过循环检查保守和积极的p ercentages .plz你能告诉我如何检查循环并显示消息框。
在Windows应用程序中使用C#
提前感谢
我不知道如何检查Loop for Conservative和Aggressive百分比
无论你获得这些数据,都要为每个条目保留一个标志如果它是保守型或Aggresive型。发布这个,你所要做的就是循环遍历所有项目,并根据标志有两个计数器来获得每种类型的总和。在循环结束时,检查计数器的总和为100.如果没有,请在消息框中提出消息。
类似于:
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不是100跨度>);
if (aggresiveSum!= 100 。 0 )
MessageBox.Show( aggresiveSum不是100);
DataGridview有一个名为Rows的属性,它是DataGridViewRows的集合,您可以遍历datagridview,如下所示。
foreach (DataGridViewRow row in datagridview.Rows)
{
string columnvalue = row.Cells [ ColumnsName_AssetAllocation] .ToString(); //你也可以使用索引
//在这里添加你的逻辑,如Sandeep所解释的那样
}
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
thanks in advance
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:
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");
DataGridview has a property called Rows which is a collection of DataGridViewRows you can iterate through the datagridview as shows below.
foreach(DataGridViewRow row in datagridview.Rows) { string columnvalue = row.Cells["ColumnsName_AssetAllocation].ToString();//you can also use the column Index //add your logic here as explained by Sandeep }
这篇关于C#Datagridview行单元格值窗口应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!