I am trying to automate the data processing task for files of various formats and fields. I created a program that defines a delimited file and uploads a piece of the file into a DataGridView in the form so that the user can confirm some fields of the file before the file is loaded into the SQL table. The table will be created on the fly using some field names. which the user selected in the combo box in the datagrid.
This is my goal, but I'm not sure that I approached the problem correctly.
At this point, I created a BindingSource for comboboxes ...
BindingSource bindingSource = new BindingSource();
Here I show the DataGridView of the selected file, adding a column for each field in the data file
private void ShowDataGridView(string file, string delimiter, string[] fieldNames, string[] fieldLengths)
{
StreamReader fileReader = new StreamReader(file);
if (bindingSource.Count == 0)
{
bindingSource.Add("FIRSTNAME");
bindingSource.Add("LASTNAME");
bindingSource.Add("ADDRESS1");
bindingSource.Add("ADDRESS2");
bindingSource.Add("CITY");
bindingSource.Add("STATE");
bindingSource.Add("ZIP");
bindingSource.Add("COMPANY");
bindingSource.Add("EMAIL");
bindingSource.Add("");
}
dataGridView1.Rows.Clear();
dataGridView1.Columns.Clear();
int count = 0;
for (int i = 0; i < 17; i++)
{
string[] fields = StringFunctions.Split(fileReader.ReadLine(), delimiter, Convert.ToString("\""));
count = fields.Count();
if (i == 0)
{
for (int x = 0; x < count; x++)
{
DataGridViewTextBoxColumn columnDataGridTextBox = new DataGridViewTextBoxColumn();
columnDataGridTextBox.Name = fieldNames[x];
columnDataGridTextBox.HeaderText = fieldNames[x];
dataGridView1.Columns.Add(columnDataGridTextBox);
}
}
dataGridView1.Rows.Add(fields);
}
for (int x = 0; x < count; x++)
{
DataGridViewComboBoxCell combobox = new DataGridViewComboBoxCell();
combobox.DataSource = bindingSource;
dataGridView1[x, 16] = combobox;
combobox = null;
}
dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;
fileReader.Close();
fileReader = null;
}
, . ( BindingSource). , . combobox, BindingSource, . , (FirstName, Field2, LastName, Address1, Field5, Field6, Address2 ..)
Combobox - , :)
, , -, datagridview . , , , , . ...
InitializeComponent();
dataGridView1.EditingControlShowing += new DataGridViewEditingControlShowingEventHandler(DataGridViewEditingControlShowing);
private void DataGridViewEditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
ComboBox cmbBox;
if (dataGridView1.CurrentCell is DataGridViewComboBoxCell)
{
cmbBox = e.Control as ComboBox;
if (cmbBox == null)
return;
cmbBox.SelectedIndexChanged += cmbBox_SelectedIndexChanged;
}
}
void cmbBox_SelectedIndexChanged(object sender, EventArgs e)
{
ComboBox cmbBox = (ComboBox)sender;
if (cmbBox.SelectedValue != null)
{
MessageBox.Show(cmbBox.SelectedValue.ToString());
bindingSource.Remove(cmbBox.SelectedValue);
}
}
, , , . , , . / .
!