Datagridviewcomboboxcolumn默认选择

时间:2014-09-27 02:52:59

标签: c# winforms datagridview datagridviewcombobox

我的数据库中有两种状态,它们是"到达","待定"。我希望当我的表单加载时,我需要我的DataGridViewComboBoxColumn来初始化具有这些状态的每个单元格。这是我试过的代码:

        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();
            }
        }

当我执行此代码时,我没有在DataGridViewComboBoxColumn上获取默认值。enter image description here

请帮助我!

0 个答案:

没有答案