首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >运行时禁用datagridviewcombobox

运行时禁用datagridviewcombobox
EN

Stack Overflow用户
提问于 2011-03-21 03:30:54
回答 2查看 10.6K关注 0票数 7

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

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

原因:

我这么做的原因是因为我的EnumStatus{New=1,Stop=2,Temp=3}。当我想注册一个学生时,状态总是被设置为New。所以当我保存时,它将自动保存Status = 1

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2011-03-21 03:34:58

下面是如何设置默认值和禁用单元格:

代码语言:javascript
复制
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;
            }
        }
    }
}
票数 9
EN

Stack Overflow用户

发布于 2011-03-21 04:03:51

有SelectedIndex for DataGridView组合框控件,如下文所示:

http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.editingcontrolshowing.aspx

http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridviewcomboboxeditingcontrol(v=vs.80).aspx

1.我就是这样做的:

代码语言:javascript
复制
private void dgv_EditingControlShowing(object sender, System.Windows.Forms.DataGridViewEditingControlShowingEventArgs e)
{
if (e.Control is DataGridViewComboBoxEditingControl) {


    DataGridViewComboBoxEditingControl control = e.Control as DataGridViewComboBoxEditingControl;


    BindingSource bs = control.DataSource as BindingSource;
    if (!IsNothing(bs)) {

        // set the filteredChildBS as the DataSource of the editing control
         ((ComboBox)e.Control).DataSource = filteredChildBS;

        //Set the dgv's combobox to the first item
         ((ComboBox)e.Control).SelectedIndex = 1

    }
}

}

filtereredChildBS是一个绑定源,如果您需要澄清,请告诉我?

2.禁用datagridview可以控制其更复杂的功能。我使用这个示例禁用DataGridView复选框:http://social.msdn.microsoft.com/forums/en-US/winformsdatacontrols/thread/988c7e3f-c172-467d-89b7-b80a60b7f24f/,但是对于组合框,更容易的方法是禁用列带:

代码语言:javascript
复制
foreach (DataGridViewBand band in dgvTransactions.Columns)  { if (i !=7) band.ReadOnly = (bool)i == 0;       i+= 1;  band.Frozen = false;  }

如果你需要进一步澄清的话请告诉我?

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5373548

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档