To use a foreach loop with a DataGridView in C#, you can use the Rows property of the DataGridView and loop through each row. Here is an example:

foreach (DataGridViewRow row in myDataGridView.Rows)
{
    // Access the data in the row using the Cells property
    string cellValue = row.Cells[0].Value.ToString();
    // Do something with the cell data
}

In this example, we're looping through the Rows property of the DataGridView called myDataGridView. For each row, we're accessing the value of the first cell in the row using the Cells property and the index of the cell (0). We're then converting that value to a string and storing it in a variable called cellValue. You can replace the comment with the code you want to execute for each row

c# foreach datagridview