Problem: Set SelectedValue in DataGridViewComboBoxColumn, C #

0

A DataGridViewComboBoxColumn does not have the properties SelectedIndex , and SelectedValue . However, I want to get the same behavior of SelectedValue

  

I have a DataGridView that contains several columns of which I have a type ComboBoxColumn , it happens that to fill that DatagridView I involve 3 queries including the one that fills the ComboBoxColumn , I explain:

Query ComboBoxColumn:

//Asuma que este Dataset, YA contiene los datos...
//dataset_estatus

if (dataset_estatus.Tables[0].Rows.Count > 0)
{
    dgrid_combobox_estatus.DataSource = dataset_estatus.Tables[0];
    dgrid_combobox_estatus.DisplayMember = "descripcion";
    dgrid_combobox_estatus.ValueMember = "codigo_estatus";
    dgrid_combobox_estatus.DataPropertyName = "codigo_estatus";
} 

The resulting data would be these:

codigo_estatus | descipcion
       1            A
       2            B
       3            C

This loads the data correctly, what happens is that the DataGridView full of combined data from two separate queries therefore before assigning the DataSet to the DataSource that will load the data to DataGridView add 2 colunas at runtime at Dataset Then while I combine the data of both DataSet with a value of one of its columns I must Select a specific item of ComboBoxColumn All this before assigning the DataSource of DataGridView .

Here is the code to represent this paragraph:

//Agregando 2 columnas que no pertenecen al Dataset
dataset_datos.Tables[0].Columns.Add("entrada", typeof(string));
dataset_datos.Tables[0].Columns.Add("salida", typeof(string));

for (int indice = 0; indice < dataset_datos.Rows.Count; indice++)
{
     //A las nuevas columnas le agrego los datos correspondiente del dataset de la otra consulta
     dataset_datos.Tables[0].Rows[indice]["entrada"] = otro_dataset.Rows[indice]["entrada"];
     dataset_datos.Tables[0].Rows[indice]["salida"] = otro_dataset.Rows[indice]["salida"];

     //Aqui debo asignar el SelectedValue del ComboBox 
     DataGridViewComboBoxCell combo = dataGridView1.Rows[indice].Cells[0] as DataGridViewComboBoxCell;
     combo.Value = Convert.ToInt32(otro_dataset.Rows[indice]["codigo_estatus"]);
}

//Luego de todo asigno el DataSource y se muestran los datos en el DataGridView
dataGridView1.DataSource = dataset_datos.Tables[0];
  

The PROBLEM is here: DataGridViewComboBoxCell combo = dataGridView1.Rows[indice].Cells[0] as DataGridViewComboBoxCell; because as the DataGridView still does not contain data it says: "That the Index is out of range" and it is obvious. What I need is to select the value of the ComboBoxColumn with the value provided by: otro_dataset.Rows[indice]["codigo_estatus"]

The final result should be:

Estatus | Entrada     | Salida      | Nombre | ...
   A      08:00 a.m.    12:00 p.m.    nombre1
   C      08:00 a.m.    12:00 p.m.    nombre2
   C      08:00 a.m.    12:00 p.m.    nombre3
  

The data thrown by otro_dataset.Rows[indice]["codigo_estatus"] in this case were: 1 , 3 , 3 that have to be selected when loading the data.

Environment: Visual Studio 2010 and .NET NetFramework 4

    
asked by J. Rodríguez 09.01.2018 в 21:23
source

2 answers

1

Attempt to assign the values in the combobox after assigning the datasource to the datagridview. With a for serious like this:

dataGridView1.DataSource = dataset_datos.Tables[0];


for (int indice = 0; indice < otro_dataset_datos.Rows.Count; indice++)
{
     DataGridViewComboBoxCell combo = dataGridView1.Rows[indice].Cells[0] as DataGridViewComboBoxCell;
     combo.Value = Convert.ToInt32(otro_dataset.Rows[indice]["codigo_estatus"]);
}
    
answered by 10.01.2018 в 00:20
0

test when the data is loaded

    dataGridView1.DataBindingComplete += DataGridView1_DataBindingComplete;
    }

    private void DataGridView1_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
    {
        DataGridViewComboBoxCell combo = dataGridView1.Rows[indice].Cells[0] as DataGridViewComboBoxCell;
        combo.Value = Convert.ToInt32(otro_dataset.Rows[indice]["codigo_estatus"]);
    }
    
answered by 09.01.2018 в 22:17