Fill a datagridview from an array

0

I have a datagridview that I want to fill with an array, for this I do the following:

BindingSource CantonesBindingSource;
CantonesBindingSource = new BindingSource(ConfData.TcCnf.TcCnfArray, null);
oTcConfig.dataGridViewTcCnf.DataSource = CantonesBindingSource;

This way I fill in the datagridview with the array. The problem is that the title of the columns is not the one I want.

I have already created my datagridview in advance with the names of the columns I want and I want to fill in these fields not to create them again (it is an array of objects where each column is an attribute of that object). I do not know if I've explained myself well.

This is what I want to fill me

This is what creates me. I want to fill in the fields, not create a new one

Any solution? Thank you very much

    
asked by Xim123 13.02.2018 в 07:46
source

3 answers

1

When loading a datagridView through its DataSource, the columns are automatically generated with the name of the variables of the object that you pass even if you have created your by design.

One option you have is, once loaded, you can change the name to your columns by code as follows:

oTcConfig.dataGridViewTcCnf.Columns["IdTc"].HeaderText="NombreColumna";

The trouble with this is that you have to change the title of the columns one by one.

Another thing to keep in mind if you do this is that, then, if you want to refer to these columns your name has not changed so even if you have changed the name you will have to access it in the following way:

oTcConfig.dataGridViewTcCnf.Columns["IdTc"].Cells ...
    
answered by 13.02.2018 в 08:05
0

The DataGridView has a property AutoGenerateColumns that by default has the value true and that causes the columns to be generated automatically from the data source.

In this case, for each property of the object, the DataGridView checks if a column exists for that property and, if not, creates it. The way you can associate a column created by you to a property is through the property DataPropertyName of the column. If the DataPropertyName of the column has as value the name of a property of the data source the DataGridView will use this column instead of creating a new one.

In any case, if you are going to create the columns manually, it is advisable to set false to the property AutoGenerateColumns of DataGridView . This way you will avoid, for example, that you create columns for properties that you do not want to display.

Another option is to let the DataGridView automatically generate the columns and go through them later to set their properties (header text, width, ...).

Except in prototypes or tests I would recommend the first system. It's clearer, you have more control and are less prone to errors.

    
answered by 13.02.2018 в 08:42
0

To do this, check the properties of your datagridview, or rather of the columns that you added. If in the datagridview you put the header "Person's Name" and in an array you have "NomPer", all you have to do is edit the columns of the datagriview, choose the column "Person's Name" and search among the properties the one that says DataPropertyName and you assign "NomPer", this column will automatically be binded to that index of your array. All that remains to be done is to assign it to the datagridview as follows:

datagridview.DataSource = TuArreglo.ToList();

And that's all, if in your arrangement you have 6 elements such as id, name, date, etc. But you only want to show the ones you defined in your arrangement, what you should do is:

datagridview.AutoGenerateColumns = false;
datagridview.DataSource = TuArreglo.ToList();

This prevents you from generating the columns on your own and will only create the ones you binded.

I hope it serves you.

    
answered by 13.02.2018 в 18:41