C# DataGridView未在运行时创建的列中显示值
我有一个非常棘手的问题。我将实体列表作为数据源分配给DataGridView。我在运行时创建了一些列,然后,对于DataGridView中的每一行,我根据行中某些列的一些值来完成新列的值 代码运行良好,因为我正在以其他形式显示相同的DataGridView。但是在这个新的UserControl中,它似乎没有在新列上显示任何值 wierd的事情是,值实际上在那里,因为当我执行foreach行循环时,我有一些acumulators int对象,它们在文本框中显示值,并且值是正确的 我试了试,看看是不是出了什么问题,但一切都很好 我附上了一张我得到的图片。 这两个突出显示的文本框用于计算这两列的值 正如我所说的,同样的代码在其他形式下运行良好。以防万一,此UserControl被添加到表单中的面板中 这是我用于DataGridView的代码:C# DataGridView未在运行时创建的列中显示值,c#,winforms,datagridview,C#,Winforms,Datagridview,我有一个非常棘手的问题。我将实体列表作为数据源分配给DataGridView。我在运行时创建了一些列,然后,对于DataGridView中的每一行,我根据行中某些列的一些值来完成新列的值 代码运行良好,因为我正在以其他形式显示相同的DataGridView。但是在这个新的UserControl中,它似乎没有在新列上显示任何值 wierd的事情是,值实际上在那里,因为当我执行foreach行循环时,我有一些acumulators int对象,它们在文本框中显示值,并且值是正确的 我试了试,看看是不
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;
}
}
ver_prestamo是显示实体的形式。我不知道是什么让它起作用,唯一的区别是我以前做过DataSource=null。但无论如何,我都是这样做的。您是否尝试过移动dgv_Prestamos.DataSource=lista_Prestamos;在Zar_grilla_prestamos的底部。
我认为,当您设置数据源而看不到随后创建的列时,它正在进行绑定。构建网格,设置数据源,然后更改DataBindingComplete事件中的外观。-glace除非你发布一些代码,否则任何人都不可能解决这个问题。至少是添加值的部分。可能是一个已知错误。我现在就编辑它。如果你不介意的话,我喜欢你的datagridview。您可以共享您的设计属性吗当然,我怎样才能把密码发给你?是的,我的错。我很难通过变量名和函数名准确地推断出你在做什么,因为这不是我的母语,但我会重新构造你的代码,看看这是否有帮助。构造网格,设置数据源,然后更改DataBindingComplete事件中的外观。可能lista_prestamos是一个返回表的函数,或者是一个BindingSource?是的,我试过了,但问题相同。正如我所说,这段代码以另一种形式完美地工作。此外,数据也在那里,因为文本框正在从行中收集数据。
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;
}
}
}
}