'IN C# from foreach to for loop for datagridview combobox column

The below is my code

 dataGridView1.DataSource = dt;
                    dataGridView1.AllowUserToAddRows = false;

                    foreach (DataGridViewRow row in dataGridView1.Rows)
                    {
                        DataGridViewComboBoxCell comboBoxCell = (row.Cells[1] as DataGridViewComboBoxCell);
                        foreach (DataRow drow in dt.Rows)
                        {
                            string customerId = drow[0].ToString();
                            comboBoxCell.Items.Add(drow[1]);

                            if (row.Cells[0].Value.ToString() == customerId)
                            {
                                comboBoxCell.Value = drow[1];
                            }
                        }
                    }

i want to change the foreach loop into for loop

like

  (dataGridView1.Columns[1] as DataGridViewComboBoxColumn).DataSource = dt;

            for (int i = 0; i <dataGridView1.Rows.Count; i++)
            {
               dataGridView1.Rows[i].Cells[1].Value = dt; 
            }

but it is not showing my required result Please any suggestion Regards



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source