【问题标题】:Displaying a list of object containing a list in a grid view在网格视图中显示包含列表的对象列表
【发布时间】:2012-10-25 08:01:18
【问题描述】:

我目前正在开发具有高级搜索功能的书签管理器应用程序(Windows 窗体)。

我创建了一个Links 类,每次用户输入一个URL 时,我都会创建一个Link 对象并将详细信息存储在那里。它目前具有属性NameURLTags,其中Tags 是一个列表。

在运行时,当我将 gridview 的 DataSource 属性设置为 List<Links> 对象时,NameURL 会出现,但 标签不会

如何在 gridview 中显示 List<Links> 对象内的标签列表?

编辑:刚刚有了一个想法。如果我写一个函数将List<Links>转换为DataTable,然后将DataGridDataSource属性设置为DataTable呢?

这样做的问题是,每次对 List<Links> 进行更改时,我都必须再次生成 DataTable,从性能的角度来看,这似乎并不理想。

编辑 2:我希望将列表中的每个项目显示为 DataGridViewTextBox 列。

谢谢,

阿比吉特。

【问题讨论】:

  • 您的 GridView 中是否有 ,与 Links 类属性匹配?如果没有,请为 gridview 设置 AutoGenerateColumns=true,并且应该生成列。将您的 HTML 和代码发布在后面以进行绑定。看看这里:forums.asp.net/t/1109165.aspx/1
  • 对不起,我忘了说,这是一个 WinForm 应用程序。
  • 在这种情况下,您需要创建一个 DataGridViewComboBoxColumn,并将标签列表分配为数据源。然后应该出现第三列,并带有一个组合框。看看这里:arsalantamiz.blogspot.co.uk/2008/09/…
  • @tranceporter,感谢该链接将来会有所帮助,但在这种情况下,我希望将列表中的每个项目显示为 DataGridViewTextBox 列。
  • 如果您有关于如何创建书签的代码。你能给我一个完整的编码部分吗,我不知道如何创建一个书签。提前谢谢...我的电子邮件 ID 是 j.s.banger@hotmail.com

标签: c# .net winforms c#-4.0 datagridview


【解决方案1】:

也许这就是你想要的……一个看起来比实际拥有更多属性的对象。

DataGridView 控件支持ComponentModel 命名空间,因此您可以创建看似具有不存在属性的类。这与PropertyGrid 使用的机制相同。

示例代码

此示例是一个完全正常工作的 Windows 窗体类,其窗体包含 DataGridView。我将一些对象添加到 is,使用然后设置为 DataSource 属性的列表。

该列表中的对象根本没有属性...但它们使用组件模型向DataGridView 描述“虚拟”属性。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            this.dataGridView1.Dock = DockStyle.Fill;
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            dataGridView1.DataSource = new List<MyClass>
                    {
                      new MyClass("value 1", "value 2", "value 3"),
                      new MyClass("value 1", "value 2"),
                    };
        }

        class MyClass : CustomTypeDescriptor
        {
            public MyClass(params string[] tags)
            {
                this.tags = new List<string>(tags);
            }

            public override PropertyDescriptorCollection GetProperties(Attribute[] attributes)
            {
                var listProps = new List<PropertyDescriptor>();

                // adding properties dynamically
                for (int i = 0; i < tags.Count; i++)
                    listProps.Add(new PropDesc("Tag" + i, i));

                return new PropertyDescriptorCollection(listProps.ToArray());
            }

            private List<string> tags = new List<string>();

            class PropDesc : PropertyDescriptor
            {
                private int index;

                public PropDesc(string propName, int index)
                    : base(propName, new Attribute[0])
                {
                    this.index = index;
                }

                public override bool CanResetValue(object component) { return false; }

                public override Type ComponentType { get { return typeof(MyClass); } }

                public override object GetValue(object component)
                {
                    if (index >= ((MyClass)component).tags.Count)
                        return null;

                    return ((MyClass)component).tags[index];
                }

                public override bool IsReadOnly { get { return true; } }

                public override Type PropertyType { get { return typeof(string); } }

                public override void ResetValue(object component) { }

                public override void SetValue(object component, object value) { }

                public override bool ShouldSerializeValue(object component) { return false; }
            }
        }

        private void InitializeComponent()
        {
            this.dataGridView1 = new DataGridView();
            ((ISupportInitialize)(this.dataGridView1)).BeginInit();
            this.SuspendLayout();
            // dataGridView1
            this.dataGridView1.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.AutoSize;
            this.dataGridView1.Dock = DockStyle.Fill;
            this.dataGridView1.Location = new System.Drawing.Point(0, 0);
            this.dataGridView1.Name = "dataGridView1";
            this.dataGridView1.Size = new System.Drawing.Size(284, 262);
            this.dataGridView1.TabIndex = 1;
            // Form1
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(284, 262);
            this.Controls.Add(this.dataGridView1);
            this.Name = "Form1";
            this.Text = "Form1";
            ((ISupportInitialize)(this.dataGridView1)).EndInit();
            this.ResumeLayout(false);

        }

        private DataGridView dataGridView1;
    }
}

【讨论】:

    【解决方案2】:

    如果您像我认为的那样使用 WPF,为什么不使用 DataGridView 行详细信息模板?您可以将第二个网格作为与标签列表绑定的行的详细信息模板。

    【讨论】:

      猜你喜欢
      • 2018-12-21
      • 1970-01-01
      • 1970-01-01
      • 2011-11-02
      • 1970-01-01
      • 2011-07-13
      • 2017-01-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多