Andres
Andres

Reputation: 2899

DataGridView not showing values in runtime created columns

I´m having a really wierd issue. I assign to my DataGridView a list of entities as a DataSource. I create some columns in runtime, and then, for each Row in the DataGridView, I complete the values of that new columns base on some values of some columns of the row.

The code works fine, because I´m displaying that same DataGridView in other forms. But in this new UserControl, it seems that it´s not showing any values on that new columns.

The wierd thing, is that the values are actually there, because when I do the foreach row loop, I have some acumulators int objects that shows the values in a textbox, and the values are correct.

I used a try and catch to see if something was wrong, but everything is fine.

I attached an image of what I´m getting. enter image description here

Those two highlighted textboxes are the one that acummulates the values of those two columns..

As I said, the same code is working fine in other forms. Just in case, this UserControl is added to a panel in a form.

This is the code I use for the DataGridView:

public void Actualizar_grilla_prestamos()
    {
        dgv_Prestamos.DataSource = null;
        dgv_Prestamos.Columns.Clear();
        dgv_Prestamos.DataSource = lista_prestamos;

        dgv_Prestamos.RowHeadersVisible = false;

        //Agregar columna cuotas restantes
        DataGridViewColumn cuotas_restantes = new DataGridViewColumn();
        {
            cuotas_restantes.HeaderText = "C. Rest.";
            cuotas_restantes.Name = "cuotas_restantes";
            cuotas_restantes.AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells;
            cuotas_restantes.CellTemplate = new DataGridViewTextBoxCell();
            cuotas_restantes.ToolTipText = "Cantidad de cuotas restantes por cobrar";
        }
        dgv_Prestamos.Columns.Add(cuotas_restantes);

        //Agregar columna tipo de tasa
        DataGridViewColumn tipo_tasa = new DataGridViewColumn();
        {
            tipo_tasa.HeaderText = "Tipo tasa";
            tipo_tasa.Name = "tipo_tasa";
            tipo_tasa.AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells;
            tipo_tasa.CellTemplate = new DataGridViewTextBoxCell();
        }
        dgv_Prestamos.Columns.Add(tipo_tasa);

        //Agregar columna garantes

        DataGridViewColumn garantes = new DataGridViewColumn();
        {
            garantes.HeaderText = "Garantes";
            garantes.Name = "garantes";
            garantes.AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells;
            garantes.CellTemplate = new DataGridViewTextBoxCell();
        }
        dgv_Prestamos.Columns.Add(garantes);
        dgv_Prestamos.Columns["garantes"].DisplayIndex = dgv_Prestamos.Columns["Cliente1"].Index;

        //Agregar columna cuotas mora
        DataGridViewColumn cuotas_mora = new DataGridViewColumn();
        {
            cuotas_mora.HeaderText = "C. Venc.";
            cuotas_mora.Name = "cuotas_mora";
            cuotas_mora.AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells;
            cuotas_mora.CellTemplate = new DataGridViewTextBoxCell();
            cuotas_mora.ToolTipText = "Cantidad de cuotas vencidas";
        }
        dgv_Prestamos.Columns.Add(cuotas_mora);

        int cant_total_cuotas_mora = 0;
        int total_cuotas_restantes = 0;

        foreach (DataGridViewRow r in dgv_Prestamos.Rows)
        {
            Estado_prestamo estado = (Estado_prestamo)dgv_Prestamos.Rows[r.Index].Cells["Estado_prestamo"].Value;
            if (estado.id_estado_prestamo != 3)
            {
                var lista_cuotas = (System.Data.Objects.DataClasses.EntityCollection<Sistema_financiero.Cuota>)dgv_Prestamos.Rows[r.Index].Cells["Cuota"].Value;
                dgv_Prestamos.Rows[r.Index].Cells["cuotas_mora"].Value = lista_cuotas.Where(x => x.pagada != true && x.fecha_vencimiento < DateTime.Now.Date).Count();
                if (Convert.ToInt32(dgv_Prestamos.Rows[r.Index].Cells["cuotas_mora"].Value) > 0)
                {
                    dgv_Prestamos.Rows[r.Index].Cells["cuotas_mora"].Style.ForeColor = Color.Red;
                }
                dgv_Prestamos.Rows[r.Index].Cells["cuotas_restantes"].Value = lista_cuotas.Where(x => x.pagada != true).Count();
            }
            else
            {
                dgv_Prestamos.Rows[r.Index].Cells["cuotas_mora"].Value = 0;
                dgv_Prestamos.Rows[r.Index].Cells["cuotas_restantes"].Value = 0;
                dgv_Prestamos.Rows[r.Index].Cells["cuotas_restantes"].Style.ForeColor = Color.Green;
            }
            if (Convert.ToBoolean(dgv_Prestamos.Rows[r.Index].Cells["tasa_fija"].Value) == true)
            {
                dgv_Prestamos.Rows[r.Index].Cells["tipo_tasa"].Value = "FIJA";
            }
            else
            {
                dgv_Prestamos.Rows[r.Index].Cells["tipo_tasa"].Value = "VARIABLE";
            }
            dgv_Prestamos.Rows[r.Index].Cells["garantes"].Value = ((System.Data.Objects.DataClasses.EntityCollection<Sistema_financiero.Cliente>)dgv_Prestamos.Rows[r.Index].Cells["Cliente1"].Value).Count;
            cant_total_cuotas_mora = cant_total_cuotas_mora + Convert.ToInt32(dgv_Prestamos.Rows[r.Index].Cells["cuotas_mora"].Value);
            total_cuotas_restantes = total_cuotas_restantes + Convert.ToInt32(dgv_Prestamos.Rows[r.Index].Cells["cuotas_restantes"].Value);
        }

        tbx_Cuotas_adeudadas_vencidas.Text = cant_total_cuotas_mora.ToString();
        tbx_Total_cuotas_restantes.Text = total_cuotas_restantes.ToString();

        //Agregar columna ver prestamo

        DataGridViewImageColumn ver_prestamo = new DataGridViewImageColumn();
        {
            ver_prestamo.HeaderText = "";
            ver_prestamo.Name = "ver_prestamo";
            ver_prestamo.AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells;
            ver_prestamo.CellTemplate = new DataGridViewImageCell();
            ver_prestamo.Image = Properties.Resources.eye_small_grid;
            ver_prestamo.ToolTipText = "Ver préstamo";
        }
        dgv_Prestamos.Columns.Add(ver_prestamo);
        dgv_Prestamos.Columns["ver_prestamo"].DisplayIndex = 0;

        dgv_Prestamos.Columns["id_prestamo"].AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells;
        dgv_Prestamos.Columns["num_cuotas"].AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells;
        dgv_Prestamos.Columns["cuotas_mora"].AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells;
        dgv_Prestamos.Columns["cuotas_restantes"].AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells;
        dgv_Prestamos.Columns["importe"].AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells;
        dgv_Prestamos.Columns["Moneda"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
        dgv_Prestamos.Columns["Sistema_amortizacion"].AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells;

        dgv_Prestamos.Columns["cuotas_mora"].DisplayIndex = dgv_Prestamos.Columns["num_cuotas"].Index + 1;
        dgv_Prestamos.Columns["cuotas_restantes"].DisplayIndex = dgv_Prestamos.Columns["num_cuotas"].Index + 1;
        dgv_Prestamos.Columns["importe"].DisplayIndex = dgv_Prestamos.Columns["garantes"].DisplayIndex;

        dgv_Prestamos.Columns["num_cuotas"].DisplayIndex = dgv_Prestamos.Columns["cuotas_restantes"].DisplayIndex;
        dgv_Prestamos.Columns["Estado_prestamo"].DisplayIndex = dgv_Prestamos.Columns[dgv_Prestamos.Columns.Count - 1].Index;
        dgv_Prestamos.Columns["Moneda"].DisplayIndex = dgv_Prestamos.Columns[dgv_Prestamos.Columns.Count - 2].Index;
        dgv_Prestamos.Columns["tipo_tasa"].DisplayIndex = dgv_Prestamos.Columns["tasa_fija"].Index;

        List<int> lista_columnas_visibles = new List<int> { dgv_Prestamos.Columns["Estado_prestamo"].Index, dgv_Prestamos.Columns["garantes"].Index, dgv_Prestamos.Columns["importe"].Index, dgv_Prestamos.Columns["Sistema_amortizacion"].Index, dgv_Prestamos.Columns["tipo_tasa"].Index, dgv_Prestamos.Columns["Moneda"].Index, dgv_Prestamos.Columns["id_prestamo"].Index, dgv_Prestamos.Columns["num_cuotas"].Index, dgv_Prestamos.Columns["cuotas_mora"].Index, dgv_Prestamos.Columns["cuotas_restantes"].Index, dgv_Prestamos.Columns["ver_prestamo"].Index };

        Mostrar_ocultar_columnas(dgv_Prestamos, lista_columnas_visibles);

        dgv_Prestamos.Columns["num_cuotas"].HeaderText = "Cuotas";
        dgv_Prestamos.Columns["id_prestamo"].HeaderText = "Nº";
        dgv_Prestamos.Columns["tasa_fija"].HeaderText = "Tipo tasa";

        dgv_Prestamos.Columns["importe"].HeaderText = "Importe";
        dgv_Prestamos.Columns["Estado_prestamo"].HeaderText = "Estado";
        dgv_Prestamos.Columns["Sistema_amortizacion"].HeaderText = "Amortización";

        dgv_Prestamos.Columns["importe"].DefaultCellStyle.Format = String.Format("$ ##0.##");

        if (dgv_Prestamos.Columns["Moneda"].Width > 99)
        {
            dgv_Prestamos.Columns["Moneda"].AutoSizeMode = DataGridViewAutoSizeColumnMode.None;
            dgv_Prestamos.Columns["Moneda"].Width = 99;
        }
    }

You can see that the DataGridView has a column with an eye. If you click the eye, you can see that entity in another form. If you change the entity state in that form, it returns a value (borrado) with true if changes has been made, or false if no change has been made. If changes were detected, then I call the above method again. Magically, it shows all that missing values!

private void dgv_Prestamos_CellContentDoubleClick(object sender, DataGridViewCellEventArgs e)
    {
        if (e.RowIndex != -1)
        {
            if (e.ColumnIndex == dgv_Prestamos.Columns["ver_prestamo"].Index)
            {
                frm_Ver_Prestamo ver_prestamo = new frm_Ver_Prestamo();
                ver_prestamo.prestamo_seleccionado = (Prestamo)dgv_Prestamos.Rows[e.RowIndex].DataBoundItem;
                ver_prestamo.db = db;
                ver_prestamo.ShowDialog();
                if (ver_prestamo.borrado == true)
                {
                    dgv_Prestamos.DataSource = null;
                    Cursor.Current = Cursors.WaitCursor;
                    Actualizar_grilla_prestamos();
                    Cursor.Current = Cursors.Default;
                }
            }
        }
    }

ver_prestamo is the form that shows the entity. I don´t know what make that work, the only difference is that I do a DataSource = null before. But I do that in the method anyways..

Upvotes: 2

Views: 1678

Answers (2)

Andres
Andres

Reputation: 2899

"Construct the grid, set the DataSource, then change the appearances in the DataBindingComplete event." - glace

Upvotes: 0

Threekill
Threekill

Reputation: 189

Have you tried moving the dgv_Prestamos.DataSource = lista_prestamos; to the bottom of Actualizar_grilla_prestamos. I think that it is doing the binding when you set the datasource and can't see the columns you have created afterward.

Upvotes: 0

Related Questions