datagridview with combo box

1

Hi, I'm trying to make a datagridview where one of the columns has a combo box TcType {Fuera de Servicio, Normal) ( Canton Type) . The code to fill in the datagrid view is as follows:

    dataGridViewTcCnf.DataSource = ConfData2.TcCnf.TcCnfArray.ToList();

Where my TcCnfArray array is an array of 16-position sTcCnf objects where the attributes of sTcCnf are: IdTc, TcType (is the one that I have linked with the combobox), NumUv, NumUvNormalizacion, IdUvNormalizacion and TcFlags.

When in my array in the position of TcType there is a 0 I want it in the comboBox to put out of service and when there is a 1 that sets Normal. How do I assign it? I have created the column in the datagridview with the combobox and the collection is what I said.

To do the conversion in reverse I do the following:

ConfData.TcCnf.TcCnfArray[index].TcType = Convert.ToByte(dataGridViewTcCnf.Rows[index].Cells[1].Value);

It tells me that the value is not valid for the datagridview. I do not know how to assign to the combo box column of the data grid view that when you get a 0 it means that the combo box selected index is 0 and you write Out of Service.

The combobox column Canton Type has the following properties. I have to configure one of these fields so that when an integer arrives at that position of the datagridview, the corresponding item is written with the integer truth? What would it be?

My datagridview is as follows:

How do I assign that when I read 0 write / select Out of service and that when I get 1 type / select normal? What should I put in ValueMember or DisplayMember to make this assignment?

When instead of having a combo box in the Canton Type column I have a text box, my result of reading the information of the TcCnfArray array is as follows:

This result would be correct only I have filled the first three positions of the array what happens is that I do not want to see a 1 in that column I want to see "Normal". I have all the DataPropertyName of the columns assigned with the values of the sTcCnf object.

    
asked by Xim123 19.02.2018 в 13:06
source

1 answer

3

Probably the problem is because of the way you fill the DataGridComboBoxColumn options. You do not specify it, but I guess you're using the Items collection. Doing so there is no way to make the link, since you only add a description.

I explain to you how I would do it. First, let's put the property AutoGenerateColumns to false so that the DataGridView does not try to generate something weird, and we create a DataTable with two columns, one for the description and the other for the value that will link to the column data:

this.dataGridViewTcCnf.AutoGenerateColumns = false;
DataTable dtValores = new DataTable();
dtValores.Columns.Add("descripcion");
dtValores.Columns.Add("indice", typeof(int));

Fill the DataTable with the possible values:

dtValores.Rows.Add("Fuera de servicio", 0);
dtValores.Rows.Add("Normal", 1);

Now we add the DataSource to the column so that the values are loaded in the combos:

((DataGridViewComboBoxColumn)this.dataGridViewTcCnf.Columns[1]).DataPropertyName = "TcType";
((DataGridViewComboBoxColumn)this.dataGridViewTcCnf.Columns[1]).DisplayMember = "descripcion";
((DataGridViewComboBoxColumn)this.dataGridViewTcCnf.Columns[1]).ValueMember = "indice";
((DataGridViewComboBoxColumn)this.dataGridViewTcCnf.Columns[1]).DataSource = dtValores;

(here I used the index of the column, you can do the same or use the name)

Properties are very important. DataPropertyName must be the name of the column / property with which we are going to link, and the other two specify the columns of value and to show that they refer to the DataTable that we have created.

Once this is done, everything should work automatically. Example:

sTcCnf[] TcCnfArray = new sTcCnf[] { new sTcCnf(){ IdTc=1, TcType=0 },
                                     new sTcCnf(){ IdTc=2, TcType=1 },
                                     new sTcCnf(){ IdTc=3, TcType=0 }};

this.dataGridView3.DataSource = TcCnfArray;
    
answered by 22.02.2018 / 16:42
source