如何DataGridViewComboBoxColumn在运行时更改以下内容:
理由:
我这样做的原因是因为Enum我拥有Status{New=1,Stop=2,Temp=3}.当我想注册学生时,状态始终设置为New.所以当我保存时,它会自动保存Status = 1.
以下是设置默认值并禁用单元格的方法:
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)