如何确定绑定到数据源的 datagridview 中显示列的顺序

发布于 2024-08-17 17:20:19 字数 962 浏览 5 评论 0原文

有没有办法确定显示的列的顺序 将 datagridview 绑定到包含以下内容的数据源时 底层 IList ? 我认为有一个特定的属性用于此目的 但不记得它实际上是什么。

例如:

 public void BindToGrid(IList<CustomClass> list)
        {
            _bindingSource.DataSource = list;
            dataGridView1.DataSource = _bindingSource.DataSource;
        }

类型绑定应该是这样的

class CustomClass
{
        bool _selected = false;
        //[DisplayOrder(0)]
        public bool Selected
        {
            get { return _selected; }
            set { _selected = value; }
        }

        string _name;
         //[DisplayOrder(2)]
        public string Name
        {
            get { return _name; }
            set { _name = value; }
        }

        string _value;
         //[DisplayOrder(1)]
        public string Value
        {
            get { return _value; }
            set { _value = value; }
        }
}

编辑: 我想补充一点,我不想将列手动添加到设计器中的列列表中。我想尽可能保持动态。

Is there a way to determine the order of the columns displayed in
a datagridview when binding it to a datasource whitch contains an
underlying IList ?
I thought there was a specific property attribute for this purpose
but can't recall what it actually was.

eg:

 public void BindToGrid(IList<CustomClass> list)
        {
            _bindingSource.DataSource = list;
            dataGridView1.DataSource = _bindingSource.DataSource;
        }

Type binded should be something like this

class CustomClass
{
        bool _selected = false;
        //[DisplayOrder(0)]
        public bool Selected
        {
            get { return _selected; }
            set { _selected = value; }
        }

        string _name;
         //[DisplayOrder(2)]
        public string Name
        {
            get { return _name; }
            set { _name = value; }
        }

        string _value;
         //[DisplayOrder(1)]
        public string Value
        {
            get { return _value; }
            set { _value = value; }
        }
}

Edit:
I would like to add that I rather not want to add the columns manually to columns list in the designer. I'd like to keep this as dynamic as possible.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

二货你真萌 2024-08-24 17:20:19

在 DataGridView 中指定实际的列列表,而不是允许其自动数据绑定。您可以在 Visual Studio 的设计视图中通过选择控件并添加列来执行此操作。确保在每一列中指定应绑定到哪个属性。然后,您可以按照您想要的方式重新排列列以及进行其他自定义。

我认为 DisplayOrder 属性相对较新,并且可能在 DataGridView 控件中不受支持。

In the DataGridView specify an actual list of columns instead of allowing it to auto-databind. You can do this in Design View in Visual Studio by selecting the control and adding the columns. Make sure you specify in each column which property it should bind to. Then you can rearrange the columns any way you want as well as do other customizations.

I think that the DisplayOrder attribute is relatively new and probably not supported in the DataGridView control.

贩梦商人 2024-08-24 17:20:19

DataGridView 中列的显示顺序由 DataGridViewColumn-s 的 DisplayIndex 属性确定。您必须在网格的列上设置这些属性,才能更改它们的顺序。

我也同意 Eilon 的回答:您可以自己创建列列表,而不是自动数据绑定,这样您就可以确定它们的显示顺序。

The display order of the columns in the DataGridView is determined by the DisplayIndex properties of the DataGridViewColumn-s. You would have to set these properties on the columns of the grid, in order to change their order.

I also agree with Eilon's answer: you can create the list of the columns yourself, instead of auto-databinding, and that way you can determine the order in which they will be displayed.

奢华的一滴泪 2024-08-24 17:20:19

我不确定这是否是 .Net 提供的功能,但如果您只是更改类中属性的顺序,网格将以相同的顺序呈现列。

下面的两个类将按照它们在类中键入的顺序呈现。奇怪的!!

class CustomClass
{
    public bool Selected {get;set;}
    public string Name{get;set;}
}

class CustomClass
{
    public string Name{get;set;}       
    public bool Selected {get;set;}
}

I am not sure whether this is a functionality that .Net Offers, but if you just change the order of your properties in the class, the grid renders the columns in the same order.

The below two classes will render in the order they are typed in the class. Strange!!

class CustomClass
{
    public bool Selected {get;set;}
    public string Name{get;set;}
}

class CustomClass
{
    public string Name{get;set;}       
    public bool Selected {get;set;}
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文