I have a datagridview in my project, and I have included a Combobox inside this one. The situation is that I have a sequential entry file, with information related to clients, and I want to extract information from it, such as the name, the operation code, the account and the amount. But of course, a customer can have one or more accounts in their name, so I need a Combobox for my DataGridView.
By means of a button, I load the file in the DGV, and with another one I leave the application.
The .txt file has 10 records and it is this:
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
The first 40 positions belong to the name, the 8 following the operation, and the last 10 to the account.
The data of the columns of my DGV are static, and its content is as follows:
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
And my main program (.cs) is this:
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;
}
}
}
}
}
Well, if I removed control of the DataError event from my DGV, the error message that would appear on the screen would be as follows:
"The following exception occurred in dataGridView: System.ArgumentException. The datagridview value is not valid. For replace this default dialog control the event dataerror. "
But when including it and executing it, I have noticed that in the column of my combobox of the first record that I read, all the accounts are being included, reason why I deduce that when I instantiate ComboAccount before informing it, it points to that position, and everything is filling in that cell.
What do I have to do for my program to work and visualize all my client account data in the Combobox? ... I assume that I should instantiate a ComboboxColumn for each client cell to inform it, but how can I do this? ?.
Thank you very much.