【发布时间】:2012-07-23 18:09:04
【问题描述】:
这是我的示例代码,其中包含一个数据网格,其中需要根据数据网格中更改的状态计算两个组合框列(状态和当前状态)
<Window.Resources>
<staticData:StatusList x:Key="StatusList"/>
</Window.Resources>
<Grid>
<my:DataGrid x:Name="dgData" AutoGenerateColumns="False" Margin="0,-6,0,6">
<my:DataGrid.Columns>
<my:DataGridTextColumn Binding="{Binding Subject}" Header="Subject" Width="*"/>
<my:DataGridTemplateColumn Header="Status" Width="100">
<my:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Status}"/>
</DataTemplate>
</my:DataGridTemplateColumn.CellTemplate>
<my:DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<ComboBox Height="22"
ItemsSource="{StaticResource StatusList}"
SelectedItem="{Binding Status}"/>
</DataTemplate>
</my:DataGridTemplateColumn.CellEditingTemplate>
</my:DataGridTemplateColumn>
<my:DataGridTextColumn Binding="{Binding RaisedBy}" Header="Raised By" Width="100"/>
<my:DataGridTemplateColumn Header="PresentStatus" Width="100">
<my:DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<ComboBox Height="22"
ItemsSource="{StaticResource StatusList}"
SelectedItem="{Binding Status}"/>
</DataTemplate>
</my:DataGridTemplateColumn.CellEditingTemplate>
</my:DataGridTemplateColumn>
</my:DataGrid.Columns>
</my:DataGrid>
</Grid>`
C#代码
public class StatusList : List<string>
{
public StatusList()
{
this.Add("Assigned");
this.Add("Closed");
this.Add("In Progress");
this.Add("Open");
this.Add("Resolved");
}
}
所以最后我需要在保存数据网格数据时计算状态。如果状态值(即相似值)选择了多次,那么它应该作为单个计数返回。例如,假设3个组合框选择,1打开,2被解析,3打开,然后计数应该是2,因为对于选择多次相同的值时,应计入单个值(或计数)。
编辑:在这里我尝试了代码,因为组合框状态是可编辑的,因此如果更改值和重复值应计为一个值,但我不确定。
bool isDuplicate;
int count;
for (int nbRow = 0; nbRow < dgData.Rows.Count; nbRow++)
{
for (int nbRowCompare = nbRow; nbRowCompare < dgData.Rows.Count; nbRowCompare++)
{
isDuplicate = true;
for (int nbCol = 0; nbCol < dgData.Rows[nbRow].Cells.Count; nbCol++)
{
if (dgData[nbCol, nbRow].Value != dgData[nbCol, nbRowCompare])
{
isDuplicate = false;
count++;
break; //Exit for each column if they are not duplicate
}
}
if (isDuplicate)
{
//Do something
count++;
}
}
}
在这里我尝试了代码,因为组合框状态是可编辑的,因此如果更改值和重复值应计为一个值,但我不确定,请以这种方式帮助我
bool isDuplicate;
int count;
for(int nbRow = 0; nbRow < dgData.Rows.Count; nbRow++){for(int nbRowCompare = nbRow; nbRowCompare < dgData.Rows.Count; nbRowCompare++){isDuplicate = true;
for(int nbCol = 0; nbCol < dgData.Rows[nbRow].Cells.Count; nbCol++)
{if(dgData[nbCol, nbRow].Value != dgData[nbCol, nbRowCompare]){isDuplicate = false;
count++;break; //Exit for each column if they are not duplicate
}}if(isDuplicate){//Do something count++;}}}
【问题讨论】:
-
嗨亲爱的,你的问题没有多大意义。您能否举例说明您获得的输出以及您的预期输出应该是什么?
-
例如选择了 4 个组合框 1row-open 2row-Resolved 3row-Closed 4row-open 所以我需要这些选定的组合框计数,因为上面的计数应该是 4,但是打开状态是重复的,所以计数应该是 3