[英]C# Form.show() Error System.InvalidOperationException
I am trying fill three ComboBox
with a Class list, but it is giving an error:我正在尝试用一个类列表填充三个
ComboBox
,但它给出了一个错误:
An unhandled exception of type 'System.InvalidOperationException' occurred in System.Windows.Forms.dll
System.Windows.Forms.dll 中发生类型为“System.InvalidOperationException”的未处理异常
"System.InvalidOperationException: This operation cannot be performed while an auto-filled column is being resized"
“System.InvalidOperationException:调整自动填充列的大小时无法执行此操作”
This error happens occasionally, but never fails to happen.此错误偶尔会发生,但永远不会发生。
And this point to form.Show();
而这指向
form.Show();
Code:代码:
Catalogo form = new Catalogo();
form.Show();
And this is the form of code:这是代码的形式:
private void btSair_Click(object sender, EventArgs e)
{
DialogResult var;
var = MessageBox.Show("Deseja fechar esta janela ? ", "", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (var == DialogResult.Yes)
{
GC.Collect(GC.MaxGeneration, GCCollectionMode.Forced);
this.Close();
}
}
private void Catalogo_Load(object sender, EventArgs e)
{
cliente = new KEYSERVICEClient();
List<Catalogo> asdf = new List<Catalogo>();
var listar = cliente.FillCombosCatalogo(Global.Id_empresa);
for (int i = 0; i < listar.Count; i++)
{
cbFabricante.Items.Add(listar[i].fabricante);
cbModelo.Items.Add(listar[i].modelo);
cbNumera.Items.Add(listar[i].numeracao);
}
}
FillComboCatalogo method: FillComboCatalogo 方法:
public List<CatalogoItens> FillCombosCatalogo(int id_empresa)
{
SqlDataReader objReader;
SqlCommand objcmd = null;
vsql = "SELECT [ID_EMPRESA], [MODELO], [FABRICANTE],[NUMERACAO] As Identificador,[MODELO],[FABRICANTE], [NUMERACAO] FROM PRODUTOS_CHAVES WHERE @ID_EMPRESA = ID_EMPRESA";
List<CatalogoItens> catag = new List<CatalogoItens>();
if (this.Conectar())
{
try
{
objcmd = new SqlCommand(vsql, objCon);
objcmd.Parameters.Add(new SqlParameter("@ID_EMPRESA", id_empresa));
objReader = objcmd.ExecuteReader();
while (objReader.Read())
{
CatalogoItens catalogo = new CatalogoItens()
{
modelo = (objReader.GetString(1)),
fabricante = (objReader.GetString(2)),
numeracao = (objReader.GetString(3))
};
catag.Add(catalogo);
}
return catag;
}
catch
{
}
finally
{
this.Desconectar();
}
}
return null;
}
If you want change combobox items, and add lots of them (in a loop) always use Suspend/ResumeLayout.如果您想更改组合框项目,并添加大量(在循环中),请始终使用 Suspend/ResumeLayout。
Eg.:例如。:
cbFabricante.SuspendLayout();
cbModelo.SuspendLayout();
cbNumera.SuspendLayout();
for (int i = 0; i < listar.Count; i++)
{
cbFabricante.Items.Add(listar[i].fabricante);
cbModelo.Items.Add(listar[i].modelo);
cbNumera.Items.Add(listar[i].numeracao);
}
cbFabricante.ResumeLayout();
cbModelo.ResumeLayout();
cbNumera.ResumeLayout();
If you don't do that, system will redraw combobox every time you add an item, instead of redraw it once - after adding all items.如果您不这样做,系统将在您每次添加项目时重绘组合框,而不是在添加所有项目后重绘一次。
It could be the cause of the exception.这可能是异常的原因。 It this will not help, provide us with parameters you set in those comboboxes
这无济于事,请向我们提供您在这些组合框中设置的参数
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.