gpt4 book ai didi

c# - 运行时禁用 datagridviewcombobox

转载 作者:太空狗 更新时间:2023-10-29 20:14:51 25 4
gpt4 key购买 nike

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

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

原因:
我这样做的原因是因为对于我的 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;
}
}
}
}

关于c# - 运行时禁用 datagridviewcombobox,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5373548/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com