运行时禁用datagridviewcombobox

Vee*_*ech 7 c# winforms

如何DataGridViewComboBoxColumn在运行时更改以下内容:

  1. 如何将组合框的第一个值设置为默认值?
  2. 禁用组合框/使其成为只读,同时将第一个值显示为默认值.这意味着,如果我在组合框中有3个项目,它应该只显示第一个项目.(禁用组合框下拉,或将其更改为运行时的文本框).

理由:
我这样做的原因是因为Enum我拥有Status{New=1,Stop=2,Temp=3}.当我想注册学生时,状态始终设置为New.所以当我保存时,它会自动保存Status = 1.

Jos*_* M. 9

以下是设置默认值并禁用单元格的方法:

using System;
using System.Windows.Forms;

namespace WindowsFormsApplication3
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            Column1.DataSource = new int[] { 1, 2, 3 };
            Column1.DataPropertyName = "Number";
            dataGridView1.DataSource = new[] 
            { 
                new { Number=1 },
                new { Number=2 },
                new { Number=3 },
                new { Number=1 }
            };
        }

        private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            if (e.ColumnIndex == Column1.Index && e.RowIndex == (dataGridView1.Rows.Count - 1))
            {
                DataGridViewComboBoxCell cell = (DataGridViewComboBoxCell)dataGridView1[e.ColumnIndex, e.RowIndex];

                cell.Value = 2;
                cell.DisplayStyle = DataGridViewComboBoxDisplayStyle.Nothing;
                cell.ReadOnly = true;
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)