运行时禁用datagridviewcombobox [英] Runtime disable the datagridviewcombobox

查看:126
本文介绍了运行时禁用datagridviewcombobox的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何运行时使datagridviewcombobox有这样的特点:

How do i runtime make the datagridviewcombobox to have this features:

1)如何在datagridviewcombobox为默认结果
2)禁用/使datagridviewcombobox是只读,同时显示的第一个值作为默认值。意思就是说,如果我在datagridviewcombobox 3项,我想让它显示只有第一个。 (可以禁用组合框是下拉,或改变它成为在运行文本框)。

1) How do i Set the first value in the datagridviewcombobox as default
2) Disable/make the datagridviewcombobox to be readonly while showing the first value as default. Meaning to say, if i have 3 items in the datagridviewcombobox, i want it to show only the first one. (Either disable the combobox to be drop down, or change it to become textbox at runtime).

原因:结果
我之所以这样做,是因为我的枚举我有状态{新建= 1,停止= 2,温度= 3},当我想注册的学生,状态始终如新。所以,当我保存,它会自动保存状态= 1

Reason:
The reason i do that is because for my enum i have Status{New=1,Stop=2,Temp=3}, when i want to register a student, the status is always as New. So when i save, it will auto save the Status = 1

请咨询。

推荐答案

下面是如何设置的默认值,禁用电池:

Here is how to set the default value and disable the cell:

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

这篇关于运行时禁用datagridviewcombobox的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆