How to determine the order of displayed columns in a datagridview bound to a data source

Is there a way to determine the order of columns displayed in a datagridview when bound to a data source contains the underlying IList? I thought that for this there is a certain attribute of the property but can’t remember what it really was.

eg:

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

The type bound 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 do not want to add columns manually to the list of columns in the designer. I would like to keep this as dynamic as possible.

+3
source share
4 answers

DataGridView , . Design View Visual Studio, . , , . , .

, DisplayOrder , , DataGridView.

+1

DataGridView DisplayIndex DataGridViewColumn -s. , .

: , , , .

0

, , .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;}
}
0

Source: https://habr.com/ru/post/1728149/


All Articles