【问题标题】:Getting Images and bindingSource.Filter working together in a DataGridView让图像和 bindingSource.Filter 在 DataGridView 中一起工作
【发布时间】:2009-06-20 00:32:08
【问题描述】:

我从序列化为 XML 字符串的集合填充 DataGridView(示例代码:*)。

我的要求是 (1) 将图像放入 DataGridView 和 (2) 设置 bindingSource.Filter 字符串并根据列中的字符串动态过滤表的能力(可能有数千个条目)。下面我奇怪的 XML to string hack 适用于 Filter 但我无法将图像反序列化到字符串/从字符串反序列化,因此我无法创建一个 .Filter 正常工作的魔术 DataView。

问题:(a) 有没有比序列化为 XML 字符串(以获取 DataView)更好的方法从 RAM 中获取对象的 DataView 集合到 dataGridView 中需要注意的是 .Filter 仍然工作。 (b) 是否有另一种方法可以在运行时向 bindingSource/DataView 添加内容(尤其是 Image 列),从而保留 .Filter 的使用?

根据我的测试,做事情this way (How to: Bind Objects to Windows Forms DataGridView Controls) 使设置过滤器字段无法操作,即什么都不做,没有异常,没有魔法过滤,nada。

(*)

    // objects in each row
    [Serializable]
    public class GradorCacheFile
    {
        public Bitmap image;
        public string filename;

        // to make it serializable
        public GradorCacheFile()
        {
        }

        public GradorCacheFile(GradorCacheFile old)
        {
            this.filename = old.filename;
            this.image = old.image;
        }
    }

// snippet of class:
public List<GradorCacheFile> files = null;
void Process()
{
    GradorCacheFiles gcf = new GradorCacheFiles();
    gcf.AddRange(this.files);

    XmlSerializer xs = new XmlSerializer(typeof(GradorCacheFiles));
    StringWriter sw = new StringWriter();
    xs.Serialize(sw, gcf);
    sw.Close();

    string xml = sw.ToString();

    StringReader reader = new StringReader(xml);
    DataSet ds = new DataSet();
    ds.ReadXml(reader);
    if (ds.Tables.Count < 1)
        return;

    DataTable dt = ds.Tables[0];
    DataView dv = new DataView(dt);
    this.bindingSource = new BindingSource();
    this.bindingSource.DataSource = dv;
    this.dataGridView.DataSource = this.bindingSource;

    int rows = this.dataGridView.Rows.Count;
    if (rows == 0)
        return;
    this.dataGridView.Columns[0].HeaderText = "Image";
    this.dataGridView.Columns[1].HeaderText = "File";
}

【问题讨论】:

    标签: c# .net user-interface datagridview


    【解决方案1】:

    (完全重写)你甚至不需要xml;如果您使用ToDataTable,则以下工作正常:

    public class MyType
    {
        public string Name { get; set; }
        public Image Image { get; set; }
    }
    ...
    [STAThread]
    static void Main()
    {
        List<MyType> list = new List<MyType>();
        list.Add(new MyType { Image=Bitmap.FromFile(image1Path), Name="Fred" });
        list.Add(new MyType { Image=Bitmap.FromFile(image2Path), Name="Barney" });
    
        DataTable table = list.ToDataTable();
        BindingSource bs = new BindingSource(table, "");
        bs.Filter = @"Name = 'Fred'";
        Application.Run(new Form {
            Controls = {
                new DataGridView {
                    DataSource = bs,
                    Dock = DockStyle.Fill}
            }
        });
    }
    

    【讨论】:

    • 工作就像一个魅力,虽然 C# 2.0 需要手动 getter 和 setter,但它可能会更糟,因为它有效。我将重新设计一个较旧的应用程序以使用它而不是脆弱的 XML/字符串方法... blech.
    猜你喜欢
    • 2014-07-08
    • 2019-07-23
    • 1970-01-01
    • 2013-07-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多