编辑模式下DatagridView中的ComboBox [英] ComboBox in DatagridView in Edit Mode

查看:456
本文介绍了编辑模式下DatagridView中的ComboBox的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个DataGridView只读模式设置为true(不可编辑),它从表单加载中的数据库获取值。当我将只读模式设置为false(可编辑模式)时,我想要一个特定的列(例如Department)作为ComboBox可见,因此我可以从中选择值。当我进入只读模式时,ComboBox应该消失,DataGridView应该是正常可见的。怎么实现呢
我正在使用C#框架4.0,Winforms

解决方案

请参阅具有只读行为的组合框架



您也可以更改 DisplayStyle 根据 DataGridView 属性的readonly何时更改。将 DisplayStyle 设置为 DataGridViewComboBoxDisplayStyle.Nothing 将隐藏下拉按钮。



例如,使用按钮点击更改 ReadOnly 属性将如下所示:

  private void mChangeReadOnlyButton_Click(object sender,EventArgs e)
{
ComboBoxColumn2.DisplayStyle =(dataGridView1.ReadOnly)?
DataGridViewComboBoxDisplayStyle.ComboBox:
DataGridViewComboBoxDisplayStyle.Nothing;
dataGridView1.ReadOnly =!dataGridView1.ReadOnly;
}

可编辑:



可编辑http://i51.tinypic.com/5e7o1w.jpg



ReadOnly:



ReadOnly http://i52.tinypic.com/1zgaxyt.jpg


I have a DataGridView with read-only mode set to true (non-editable) which takes values from a database on form load. When I set the read-only mode to false (editable mode), I want a particular column (eg. Department) to visible as ComboBox so i can select values from there. And when i enter the Read-Only mode, the ComboBox should disappear and DataGridView should be visible as normal. How to achieve it? I am using C# framework 4.0, Winforms

解决方案

See the implementation at ComboBox with read only behavior.

You could also change the DisplayStyle based on when the readonly of the DataGridView property is changed. Setting the DisplayStyle to DataGridViewComboBoxDisplayStyle.Nothing will hide the drop down button.

For example, changing the ReadOnly property with a button click would look something like this:

private void mChangeReadOnlyButton_Click(object sender, EventArgs e)
{
   ComboBoxColumn2.DisplayStyle = (dataGridView1.ReadOnly) ?
      DataGridViewComboBoxDisplayStyle.ComboBox :
      DataGridViewComboBoxDisplayStyle.Nothing;
   dataGridView1.ReadOnly = !dataGridView1.ReadOnly;
}

Editable:

Editable http://i51.tinypic.com/5e7o1w.jpg

ReadOnly:

ReadOnly http://i52.tinypic.com/1zgaxyt.jpg

这篇关于编辑模式下DatagridView中的ComboBox的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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