Add / Remove / Select ComboBox Values ​​in a DatagridView

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++)  //read 17 lines into datagridview for field confirmation, 17 lines just so happens to fill my datagridview nicely, last row will be combobox for field name selection
        {
            string[] fields = StringFunctions.Split(fileReader.ReadLine(), delimiter, Convert.ToString("\""));
            count = fields.Count();
            if (i == 0)
            {
               // Adding Column Header to DataGridView
                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;  //remember 17 rows added, combobox will be last row in datagridview
            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)
    {
        //here we will add the combo box selected event changed 
        ComboBox cmbBox; 
        if (dataGridView1.CurrentCell is DataGridViewComboBoxCell)
        { 
            cmbBox = e.Control as ComboBox; 
            if (cmbBox == null)
                return; 
            cmbBox.SelectedIndexChanged += cmbBox_SelectedIndexChanged; 
        }
    }

    //This will display value of Select values of Combo Box 
    //which is DataGridView
    void cmbBox_SelectedIndexChanged(object sender, EventArgs e)
    {
        ComboBox cmbBox = (ComboBox)sender;
        if (cmbBox.SelectedValue != null)
        {
            MessageBox.Show(cmbBox.SelectedValue.ToString());  //testing
            bindingSource.Remove(cmbBox.SelectedValue);   //this removes it from the current combobox as well, no good.  Also run time error when clicking into a different combobox
        }          
    }

, , , . , , . / .

!

+3
1

, , , , combobox , . , , ( X combobox A .)

, combobox, "" , .

+1

All Articles