【问题标题】:Counting the number of unique image occurences in a DataGridViewImageColumn计算 DataGridViewImageColumn 中唯一图像出现的次数
【发布时间】:2008-11-05 10:22:54
【问题描述】:

在绑定到 DataView 的 DataGridView 中,列中的单元格将包含 ImageList 中的两个图像之一(例如绿色的“在线”图像或红色的“离线”图像),您如何计算出现次数遍历 DataGridView 的行时的每个图像?

DataGridViewImageCell 的 Value 和 FormattedValue 属性返回不同的引用,即使基础 DataTable 中的条目是通过引用 ImageList 中的相同 Image 创建的。

因此,与其计算 4 次“在线”和 6 次“离线”,我得到了 10 次据称不同的图像。

【问题讨论】:

    标签: c# winforms datagridview


    【解决方案1】:

    鉴于描述,我可以建议的最好方法是手动跟踪单元格的 .Tag 中的图像键/索引,然后在 .Tag 上运行您的不同计数。或者,如果您在列表/集合中也有数据,请计算原始来源中的数据。

    [更新以下回复] 由于数据是数据绑定的,您应该能够迭代DataGridView.Rows,获得每个.DataBoundItem。那么对于每个底层对象,你应该可以使用TypeDescriptor获取实际的属性值:

            string propName = col.DataPropertyName;
            // could also be a dictionary/hashset etc
            // could be typed if you know the type
            List<object> values = new List<object>();
            foreach(DataGridViewRow row in dgv.Rows)
            {
                object obj = row.DataBoundItem;
                // could be typed (via cast) if you know the type
                object val = TypeDescriptor.GetProperties(obj)[propName].GetValue(obj);
                if (!values.Contains(val))
                {
                    values.Add(val);
                }
            }
    

    有什么帮助吗?

    【讨论】:

    • 嗨,马克 - 感谢您的回复。当 DataGridView 是数据绑定并自动填充时,您将在什么时候分配给单元格的 Tag 属性?
    • + DataGridView 显示整个 DataTable 的一个子集(使用 DataView.RowFilter),在其中我只想在当前突出显示的行中计数(MultiSelect)。所以回溯到原始数据源似乎很困难
    • 看起来必须为每一行在内存中复制位图,因为使用您的方法,返回的位图引用对于每一行都是不同的,即使它们使用图像列表中的相同图像.
    • + 我将不得不重新考虑这一点,因为我不想最终进行像素级平等测试!不过,感谢您的回答,看来它肯定可以工作。我希望我能给你更多的分数,但我不能将其标记为答案
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-12
    • 1970-01-01
    • 1970-01-01
    • 2016-11-11
    相关资源
    最近更新 更多