Say your text file contains:
Australia PNG, India Africa
Bali Indonesia Indonesia
France England Scotland Ireland Greenland
Germany Bahama Hawaii
Greece Colombia Mexico Mexico Peru Argentina - New Zealand Russia USA
And let's say that your DataGridView is set to 3 columns, and the second is a combo box.

When you fill the grid and fill the combobox column incorrectly, you will get an error.
The way to solve it is to "handle / declare explicitly" the DataError event and, more importantly, populate the list column correctly.
private void dataGridView1_DataError(object sender, DataGridViewDataErrorEventArgs e) {
So, imagine that the 2nd column contains a drop-down list of countries, and the 1st and 3rd columns contain text fields.
For the 1st and 3rd columns, these are just rows, so I create a class to represent each row:
public class CountryData { public string FirstCountry { get; set; } public string ThirdCountry { get; set; } }
For the 2nd column with the "Countries" list, I created a separate class, because I will bind it to the data source of the 2nd column.
public class MultiCountryData { public string[] SeceondCountryOption { get; set; } }
Filling the grid with combobox columns and the like, as shown here: fooobar.com/questions/230604 / ... is not good practice. You want to separate your business logic from your presentation for a more encapsulated, polymorphic and abstract approach that will facilitate unit testing and maintenance. Hence DataBinding.
Here is the code:
namespace BusLogic { public class ProcessFiles { internal List<CountryData> CountryDataList = new List<CountryData>(); internal List<MultiCountryData> MultiCountryDataList = new List<MultiCountryData>(); internal void foo(string path,string choosenFile) { var custIndex = new List<int>();
In your presentation project, the button code is:
imports BusLogic; private void button1_Click(object sender, EventArgs e) { var pf = new ProcessFiles(); pf.foo(@"C:\temp","countries.txt"); dataGridView2.AutoGenerateColumns = false; dataGridView2.DataSource = pf.CountryDataList; multiCountryDataBindingSource.DataSource = pf.MultiCountryDataList; }
I set dataGridView2.AutoGenerateColumns = false; because during development I added 3 columns; 1st text column, 2nd column with a list and columns of 3rd text.
The trick of linking the second column to the list is BindingSource . During development> right-click on DataGridView> select "Edit Columns"> select the second column> select "DataSource"> "Add DataSource Project> select" Object "> then mark the multiCountry class and click" Finish ".


Also set the 1st column of DataPropertyName for FirstCountry and the 3rd column of DataPropertyName for ThirdCountry, so when you bind the data, the mapping is done automatically.

Finally, remember to set the BindingSource DataMember property for a member of the SeceondCountryOption class of multiCountry.

Here is the code demo http://temp-share.com/show/HKdPSzU1A