最近写的程序中需要在DataGridView中使用下拉选择的功能,首选方案是列的ColumnType属性

使用EditingControlShowing事件,

if (e.Control is ComboBox)
{
  int iColumn = dgvWorkerList.CurrentCell.ColumnIndex;
  switch (iColumn)
   {
     case 2://列
    {

      Gender.DisplayStyle = DataGridViewComboBoxDisplayStyle.DropDownButton;//原来设置的是Nothing

      List<string> list = new List<string>();
      list.Add("男");
      list.Add("女");

      Gender.DataSource = list;

      (e.Control as ComboBox).SelectedIndexChanged += new EventHandler(ComboBox_SelectedIndexChanged);

    }

  }

}

由原来的文本转为下拉框、绑定数据功能完全没有问题。唯一的问题是

黑色呀,尝试了强制刷新,无效,怀疑绑定数据问题,测试后觉得不是,因为直接在Items加内容,不使用绑定,结果一样。

郁闷很久,依然没有解决。

改方式吧,借鉴了

gldamao 的BLOG

巧用ComboBox控件实现datagridView下拉菜单功能

方法,小修改,实现效果。感谢gldamao。

放一个combox在窗体,Load中

this.cmbGender.Visible = false;
this.cmbGender.Width = 0;
this.dgvWorkerList.Controls.Add(this.cmbGender);

使用CurrentCellChanged事件中判断

private void dgvWorkerList_CurrentCellChanged(object sender, EventArgs e)
{
  try
  {
    int iIndex = this.dgvWorkerList.CurrentCell.ColumnIndex;

    switch (iIndex)
    {
      case 2:
      {
        this.cmbGender.Visible = false;
        this.cmbGender.Width = 0;
        this.cmbGender.Left = this.dgvWorkerList.GetCellDisplayRectangle(this.dgvWorkerList.CurrentCell.ColumnIndex,         this.dgvWorkerList.CurrentCell.RowIndex, true).Left;
        this.cmbGender.Top = this.dgvWorkerList.GetCellDisplayRectangle(this.dgvWorkerList.CurrentCell.ColumnIndex,         this.dgvWorkerList.CurrentCell.RowIndex, true).Top;
        this.cmbGender.Width = this.dgvWorkerList.GetCellDisplayRectangle(this.dgvWorkerList.CurrentCell.ColumnIndex,         this.dgvWorkerList.CurrentCell.RowIndex, true).Width;
        string content = Convert.ToString(this.dgvWorkerList.CurrentCell.Value);
        this.cmbGender.Text = content;
        this.cmbGender.Visible = true;

        List<string> list = new List<string>();
        list.Add("男");
        list.Add("女");

        this.cmbGender.DataSource = list;
      }
      break;

    }

  }

}

选列时出现combox添加内容

这里说下,之前用的绑定的方式加入内容,在选择时候发现无法获得选中的内容于是修改

for (int i = 0; i < list.Count; i++)
{
  this.cmbGender.Items.Add(“男”);
}

之后

private void cmbGender_SelectionChangeCommitted(object sender, EventArgs e)
{
  this.dgvWorkerList.CurrentCell.Value = ((System.Windows.Forms.ComboBox)sender).SelectedItem.ToString();
  this.cmbGender.Visible = false;//这里稍作修改,选中内容后直接隐藏了combox 
  this.cmbGender.Width = 0;
}

效果很好。

只是原来的下拉黑色的问题没有解决,欢迎有办法的大神留言、指教。

关于datagridview里使用combox的总结的更多相关文章

  1. 解决数据库datatime数据在DataGridView里不显示秒的解决

    在数据库中正确显示有分有秒,到dataset里的时候也有,但绑定到DataGridView里的时候就没有秒,解决办法: dataGridView1.Columns["record_time& ...

  2. C# WinForm程序向datagridview里添加数据

    在C#开发的winform程序中,datagridview是一个经常使用到的控件.它可以以类似excel表格的形式规范的展示或操作数据,我也经常使用这个控件.使用这个控件首先要掌握的就是如何向其中插入 ...

  3. datagridview里面有combox避免双击两次的写法

    双击两次变成单击一次的写法: void dataGridView_CellEnter(object sender, DataGridViewCellEventArgs e) { //实现单击一次显示下 ...

  4. C#里,如何模拟DataGridView里的一个Cell的Click事件。

    //假设dgv是一个DataGridView. //我要点击第3行的第二个cell. //当然,要有一个点击事件.假设dgv_CellClick是那个点击事件. dgv_CellClick(dgv,  ...

  5. datagridview里面的checkbox全选和取消全选

    全选 设置全选button,选中所有的checkbox private void selectAll_Click(object sender, EventArgs e) { //遍历datagridv ...

  6. DataGridView里CheckBox实现全选控制

    1. checkbox点击事件 private void myStyleDataGridView1_CellClick(object sender, DataGridViewCellEventArgs ...

  7. c# 如何给 dataGridView里添加一个自增长列(列名为序号)

    System.Data.DataTable table = new DataTable();                System.Data.DataColumn column = new Da ...

  8. DataGridView删除多行选中数据

    思路是找到最先选择和最后选择到的行 ,弄一个for循环,根据这些行的索引值在执行数据的删除. 我这里用了EF.             DialogResult result = MessageBox ...

  9. WinForm编程数据视图之DataGridView浅析

    学习C#语言的朋友们肯定或多或少地接触到了WinForm编程,在C#语言的可视化IDE中(如VS.NET中)使用设计器可以让我们轻松地完成窗体.按钮.标签.图片框等等控件的组合,我们可以轻易地做出界面 ...

随机推荐

  1. CentOS防火墙iptables的配置方法详解

    CentOS系统也是基于linux中的它的防火墙其实就是iptables了,下面我来介绍在CentOS防火墙iptables的配置教程,希望此教程对各位朋友会有所帮助. iptables是与Linux ...

  2. javascript删除数组某个元素

    1.首先可以给js的数组对象定义一个函数,用于查找指定的元素在数组中的位置,即索引 Array.prototype.indexOf = function(val) { for (var i = 0; ...

  3. [python面向对象]--基础篇

    1.#类 #类就是一个模板,模板里可以包含多个函数,函数里实现一些功能 #定义一个类 class bar: def foo(self,agr): print(self,agr) obj = bar() ...

  4. Mac eclipse 导入文件夹

    貌似发现了个bug: Eclipse IDE for C/C++ Developers Version: Mars.1 Release (4.5.1)Build id: 20150924-1200 M ...

  5. python:列表与元组

    1.python包含六种内建的序列,列表和元组是其中的两种,列表可以修改,元组则不能 2.通用序列操作 2.1 索引:和C#的区别是索引可以为负数,最后一个元素索引为-1,索引超出范围会报错 例:&g ...

  6. java.lang.ClassNotFoundException: org.apache.catalina.startup.VersionLoggerListener

    解决办法 找到Tomcat配置文件server.xml   apache-tomcat-7.0.57/conf 将<Listener className="org.apache.cat ...

  7. 廖雪峰js教程笔记14 file文件操作

    在HTML表单中,可以上传文件的唯一控件就是<input type="file">. 注意:当一个表单包含<input type="file" ...

  8. Only Link: Inheritance and the prototype chain

    Reference: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Inheritance_and_the_prototype_cha ...

  9. sketchup

    1. clean start 1. 删除中间人物 2. windows---style 3. Windows---Model Info 2. 好的建模习惯 1. 正面朝镜头 View---ToolBa ...

  10. 【NOI2016】优秀的拆分 题解(95分)

    题目大意: 求一个字符串中形如AABB的子串个数. 思路: 用哈希做到O(1)判断字符串是否相同,O($n^2$)预处理,ans[i]为开头位置为i的形如AA的子串个数.再用O($n^2$)枚举出AA ...