Título: Problema incluyendo un Combobox en un DataGridView. Publicado por: talisman46 en 25 Octubre 2018, 11:07 am Buenas a todos.
Tengo un datagridview en mi proyecto,y he incluído dentro de este un Combobox. La situación es que tengo un fichero secuencial de entrada, con información relativa a clientes, y deseo extraer información del mismo, tal como el nombre, el código de operación, la cuenta y el importe. Pero claro, un cliente puede tener una o más cuentas a su nombre, por lo que necesito un Combobox para mi DataGridView. Mediante un botón, cargo el fichero en el DGV, y con otro me salgo de la aplicación. El fichero .txt tiene 10 registros y es este: ANTONIO MARCH GONZALEZ OP0024840000010000 JOHN SMITH GANTZ OP0049190000010550 FRANCISCO JOSE ABADES DE GONZALEXZ OP0022730000123456 SANTIAGO AVILA PIZARRO OP0050340000002500 CESAR ROMERO BONILLA 0000003456 MANUEL GARCIA ESPARTANO OP0019440000005000 AGUSTIN GOMEZ GARCIA OP0021280000007000 ALBERT CAVA CISNEROS OP0022930000006510 SAMUEL GONZALEZ RODRIGUEZ OP0017240000000050 SERGIO GARCIA HURTADOS OP0023160000010000 Las 40 primeras posiciones pertenecen al nombre, las 8 siguientes a la operación, y las 10 últimas a la cuenta. Los datos de las columnas de mi DGV son estáticos, y su contenido es el siguiente: Header text Data PropertyName ColumnType Width MaxDropDownItems ----------------------------------------------------------------------------------------------------------- Customer Name CustomeName DataGridViewTextBoxColumn 275 Customer Op. CustomeOp DataGridViewTextBoxColumn 105 Customer Account CustomeAccount DataGridViewComboBoxColumn 220 15 Customer Amount CustomeAmount DataGridViewTextBoxColumn 100 Y mi programa principal (.cs) es este: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace DataGridViewconComboBox { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button2_Click(object sender, EventArgs e) { Close(); } public class TransferAction { public string CustomeName { get; set; } public string CustomeOp { get; set; } public DataGridViewComboBoxColumn CustomeAccount { get; set; } public decimal CustomeAmount { get; set; } } decimal Amount; string ChainCustomer; string ChainOperation; public List<string> AccountCmr = new List<string>(); private void button1_Click(object sender, EventArgs e) { int counter = 0; string line; // Read the file and display it line by line. System.IO.StreamReader file = new System.IO.StreamReader(@"C:\EsferaManagements\bia\Q43\ESTEBAN-PRUEBAS.txt"); List<TransferAction> actios = new List<TransferAction>(); while ((line = file.ReadLine()) != null) { System.Console.WriteLine(line); ChainCustomer = line.Substring(0, 40).Trim(); ChainOperation = line.Substring(40, 8); Amount = Convert.ToDecimal(line.Substring(48, 10)); DataGridViewComboBoxColumn ComboAccount = dataGridView1.Columns[2] as DataGridViewComboBoxColumn; AccountCmr = FindAccounts(ChainCustomer); ComboAccount.Items.AddRange(AccountCmr.ToArray()); actios.Add(new TransferAction() { CustomeName = this.ChainCustomer, CustomeOp = this.ChainOperation, CustomeAccount = ComboAccount, CustomeAmount = this.Amount }); counter++; } dataGridView1.AutoGenerateColumns = false; dataGridView1.DataSource = actios; file.Close(); System.Console.WriteLine("There were {0} lines.", counter); // Suspend the screen. System.Console.ReadLine(); } public List<string> FindAccounts(string ChainCustomer) { List<string> NewAccounts = new List<string>(); if(ChainCustomer == "ANTONIO MARCH GONZALEZ" || ChainCustomer == "JOHN SMITH GANTZ") { NewAccounts.Add("ES00-1111-2222-33-0444444444"); NewAccounts.Add("ES00-5555-6666-77-1888888888"); NewAccounts.Add("ES00-9999-0000-11-2222222222"); } if (ChainCustomer == "FRANCISCO JOSE ABADES DE GONZALEXZ" || ChainCustomer == "SANTIAGO AVILA PIZARRO") { NewAccounts.Add("ES01-1234-2000-33-3444444444"); NewAccounts.Add("ES01-5678-6000-77-4888888888"); } if (ChainCustomer == "CESAR ROMERO BONILLA" || ChainCustomer == "MANUEL GARCIA ESPARTANO") { NewAccounts.Add("ES02-1111-2222-33-5444444444"); NewAccounts.Add("ES02-5555-6666-77-6888888888"); NewAccounts.Add("ES02-9000-0000-11-7111111111"); NewAccounts.Add("ES02-9000-0000-22-8222222222"); } if (ChainCustomer == "AGUSTIN GOMEZ GARCIA" || ChainCustomer == "ALBERT CAVA CISNEROS") { NewAccounts.Add("ES03-1111-2222-33-9444444444"); } if (ChainCustomer == "SAMUEL GONZALEZ RODRIGUEZ" || ChainCustomer == "SERGIO GARCIA HURTADOS") { NewAccounts.Add("ES04-9999-0000-11-0222222222"); NewAccounts.Add("ES04-5555-6666-77-1888888888"); NewAccounts.Add("ES04-1111-2222-33-2444444444"); } return NewAccounts; } private void dataGridView1_DataError(object sender, DataGridViewDataErrorEventArgs e) { // Don't make anything if (e.Exception.Message == "El valor de DataGridViewComboBoxCell no es válido.") { object value = dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value; if (!((DataGridViewComboBoxColumn)dataGridView1.Columns[e.ColumnIndex]).Items.Contains(value)) { ((DataGridViewComboBoxColumn)dataGridView1.Columns[e.ColumnIndex]).Items.Add(value); e.ThrowException = false; } } } } } Pues bien, si eliminara el control del evento del DataError de mi DGV, el mensaje de error que me saldría por pantalla sería el siguiente: "La siguiente excepción ocurrió en dataGridView: System.ArgumentException. El valor del datagridview no es válido. Para reemplazar este cuadro de diálogo predeterminado controle el evento dataerror." Pero al incluirlo y ejecutarlo, me he dado cuenta de que en la columna de mi combobox del primer registro que leo, se están incluyendo todas las cuentas, por lo que deduzco que cuando instancio ComboAccount antes de informarlo, este apunta a esa posición, y todo lo está rellenando en esa celda. ¿Qué tengo que hacer para mi programa funcione y visualice todos mis datos de cuentas de cliente en el Combobox?.... Deduzco que debería de instanciarme un ComboboxColumn por cada celda de cliente para informarlo pero, ¿cómo puedo hacer esto?. Muchas gracias. |