1

There are two status in my database, they are "Arrived", "Pending". I want when my form loads I need my DataGridViewComboBoxColumn to Initialize each cells with those status. This is the code I tried:

        DataGridViewComboBoxColumn cb = new DataGridViewComboBoxColumn();
        cb.HeaderText = "Status";
        cb.Name = "Status";
        cb.Width = 100;
        cb.Items.AddRange("Arrived", "Pending");
        dgPurchase.Columns.Add(cb);
        string stat= "";

        foreach (DataGridViewRow row in dgPurchase.Rows)
        {
            connection.Open();
            try
            {
                SqlCommand cmd = new SqlCommand("SELECT Purchase_Status FROM PurchasedProducts WHERE Purchase_Order_No = " + row.Cells["Purchase Order No"].Value + "AND Purchase_Product_ID = " + row.Cells["Product ID"].Value, connection);
                SqlDataReader reader = cmd.ExecuteReader();
                while (reader.Read())
                {
                    stat= reader["Purchase_Status"].ToString();
                }
                row.Cells["Status"].Value = stat;
            }
            catch (SqlException ex)
            {
                MessageBox.Show(ex.Message, "Report", MessageBoxButtons.AbortRetryIgnore, MessageBoxIcon.Error);
            }
            finally
            {
                connection.Close();
            }
        }

When I execute this code, I don't get the default value on DataGridViewComboBoxColumn.enter image description here

Please help me!

Grant Winney
  • 65,241
  • 13
  • 115
  • 165
Dragon Warrior
  • 307
  • 3
  • 16

0 Answers0