MaKhdoom Ali

MaKhdoom Ali

  • NA
  • 3
  • 1.8k

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

Apr 20 2022 9:02 AM

Hello,

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


Answers (1)