【问题标题】:DataGridView not showing values in runtime created columnsDataGridView 未在运行时创建的列中显示值
【发布时间】:2013-01-23 02:48:59
【问题描述】:

我遇到了一个非常奇怪的问题。我将实体列表作为数据源分配给我的 DataGridView。我在运行时创建了一些列,然后,对于 DataGridView 中的每一行,我根据行的某些列的某些值来完成新列的值。

代码运行良好,因为我正在以其他形式显示相同的 DataGridView。但是在这个新的 UserControl 中,它似乎没有在新列上显示任何值。

奇怪的是,值实际上是存在的,因为当我执行 foreach 行循环时,我有一些累加器 int 对象在文本框中显示值,并且值是正确的。

我使用了 try and catch 来查看是否有问题,但一切都很好。

我附上了一张我得到的图片。

这两个突出显示的文本框是累积这两列值的文本框..

正如我所说,相同的代码在其他形式中也可以正常工作。以防万一,这个 UserControl 被添加到表单中的面板中。

这是我用于 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;
        }
    }

您可以看到DataGridView 有一个带有眼睛的列。如果单击眼睛,您可以以另一种形式看到该实体。如果您更改该表单中的实体状态,它会返回一个值 (borrado),如果已进行更改,则返回 true,如果未进行更改,则返回 false。 如果检测到更改,则我再次调用上述方法。神奇的是,它显示了所有缺失的值!

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 是显示实体的形式。我不知道是什么让它起作用,唯一的区别是我之前做了一个 DataSource = null 。但无论如何我都是这样做的..

【问题讨论】:

  • 除非您发布一些代码,否则任何人都无法找出问题所在。至少是您添加值的部分。
  • 也许这是一个已知错误。我现在将对其进行编辑。
  • 如果你不介意的话,我喜欢你的datagridview。你能分享你的设计属性吗? :)
  • 好的,我怎么把代码发给你?
  • 是的,我的错。我很难通过变量和函数名称准确推断出你在做什么,因为它不是我的母语,但我会稍微重构你的代码,看看是否有帮助。构造网格,设置 DataSource,然后在 DataBindingComplete 事件中更改外观。想必lista_prestamos是一个返回表的函数,还是一个BindingSource?

标签: c# winforms datagridview


【解决方案1】:

“构造网格,设置 DataSource,然后在 DataBindingComplete 事件中更改外观。” - 糖霜

【讨论】:

    【解决方案2】:

    您是否尝试过移动 dgv_Prestamos.DataSource = lista_prestamos;到 Actualizar_grilla_prestamos 的底部。 我认为当您设置数据源并且看不到您之后创建的列时它正在执行绑定。

    【讨论】:

    • 是的,我试过了,但同样的问题。正如我所说,这段代码以另一种形式完美运行。此外,数据在那里,因为文本框正在从行中收集数据。
    猜你喜欢
    • 2012-01-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-18
    相关资源
    最近更新 更多