【问题标题】:Repeated values count as single value of combobox in datagrid重复值计为数据网格中组合框的单个值
【发布时间】: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

标签: c# .net wpf datagrid


【解决方案1】:

如果我正确理解您的问题,您需要计算 DataGrid 中显示的各个行使用的不同 Status 值的数量。

您的 DataGrid 可能正在显示对象的 ObservableCollection(或其他一些集合),表的每一行都有一个对象。 (您的代码示例中没有为ItemsSource 显示任何值,但我假设有一个。)如果此列表称为items,并且您有using System.Linq;,您可以获得一个列表不同的状态使用:

List<string> distinctStatuses = items.Select(item => item.Status)
                                     .Distinct()
                                     .ToList();

如果你只想要计数,你可以进一步简化:

int distinctStatusCount = items.Select(item => item.Status).Distinct().Count();

我不能确定您的PresentStatus 列,因为它与您的Status 绑定到同一列,因此它只会显示相同的数据。 DataGrid 中的PresentStatus 列也缺少其CellTemplate

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-01-05
    • 1970-01-01
    • 2018-05-21
    • 1970-01-01
    • 1970-01-01
    • 2012-05-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多