Earlier this week, I came across a new developer who was trying to loop through his DataGridView to get the data out of it. Usually, RAD development with the DataGridView just does not work for most of my business scenarios but here was a clear case where it worked well so I will paste my instructions here:
1. Create your form.
2. Drag a DataGridView to your form. A pop-up window entitled “DataGridView Tasks” should appear. If not, then there should be a small arrow in the top right-hand corner of the DataGrid.
3. From here, select “Choose Data Source –> Add Project Data Source.” This should trigger a wizard that will assist you in setting up your database connection. Select “Database”, then select your table, and continue to walk through the wizard.
4. If you return to your form, you should now see a DataSet, a BindingSource, and a TableAdapter. These are the tools that you will use to get and save your DataGrid data.
5. Note that in your Form_Load event, there should be a like of code similar to the following:
this.tableAdapter.Fill(this.dataSet1.table);
6. Now, add a button to your form and create a _Click event. Here you should be able to save your data by entering your version of the following lines of code:
this.Validate();
this.tableBindingSource.EndEdit();
this.tableAdapter.Update(dataSet1.table);
7. The data should now be updated in your DataGrid and database.
This code will fill your DataGridView upon load. You can modify the SQL in the DataSet to pull only select data if you wish.
http://msdn.microsoft.com/en-us/library/ms171933(v=vs.80).aspx