这是我的示例代码,其中包含一个数据网格,其中需要根据数据网格中更改的状态计算两个组合框列(状态和当前状态)
<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");
}
}
所以最后我需要在保存数据网格数据时计算状态。如果多次选择状态值(即相似值),则它应作为单个计数返回。For example, suppose 3 comboboxes are selected, 1 is Open, 2 is Resolved and 3 is Open, then count should be 2 because for open it should count as single value (or count) when multiple times same value selected.
编辑:在这里我尝试了代码,因为组合框状态是可编辑的,因此如果更改值和重复值应计为一个值,但我不确定。
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++;}}}