关于datagridview里使用combox的总结
最近写的程序中需要在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的总结的更多相关文章
- 解决数据库datatime数据在DataGridView里不显示秒的解决
在数据库中正确显示有分有秒,到dataset里的时候也有,但绑定到DataGridView里的时候就没有秒,解决办法: dataGridView1.Columns["record_time& ...
- C# WinForm程序向datagridview里添加数据
在C#开发的winform程序中,datagridview是一个经常使用到的控件.它可以以类似excel表格的形式规范的展示或操作数据,我也经常使用这个控件.使用这个控件首先要掌握的就是如何向其中插入 ...
- datagridview里面有combox避免双击两次的写法
双击两次变成单击一次的写法: void dataGridView_CellEnter(object sender, DataGridViewCellEventArgs e) { //实现单击一次显示下 ...
- C#里,如何模拟DataGridView里的一个Cell的Click事件。
//假设dgv是一个DataGridView. //我要点击第3行的第二个cell. //当然,要有一个点击事件.假设dgv_CellClick是那个点击事件. dgv_CellClick(dgv, ...
- datagridview里面的checkbox全选和取消全选
全选 设置全选button,选中所有的checkbox private void selectAll_Click(object sender, EventArgs e) { //遍历datagridv ...
- DataGridView里CheckBox实现全选控制
1. checkbox点击事件 private void myStyleDataGridView1_CellClick(object sender, DataGridViewCellEventArgs ...
- c# 如何给 dataGridView里添加一个自增长列(列名为序号)
System.Data.DataTable table = new DataTable(); System.Data.DataColumn column = new Da ...
- DataGridView删除多行选中数据
思路是找到最先选择和最后选择到的行 ,弄一个for循环,根据这些行的索引值在执行数据的删除. 我这里用了EF. DialogResult result = MessageBox ...
- WinForm编程数据视图之DataGridView浅析
学习C#语言的朋友们肯定或多或少地接触到了WinForm编程,在C#语言的可视化IDE中(如VS.NET中)使用设计器可以让我们轻松地完成窗体.按钮.标签.图片框等等控件的组合,我们可以轻易地做出界面 ...
随机推荐
- jQuery视差滚动插件,(附原理分析,调用方法)
演示地址:http://www.jq22.com/jquery-info1799 jquery插件,使用验证过可用. 分析源代码后总结原理:设置background样式为fixed,判断浏览器滚动距离 ...
- Angular2 组件
1. 组件说明 Angular2 组件是构成Angular2应用程序的核心部分,Angualr2应用程序本质上来说就是一个组件树,Angular2组件一般由模块,注解,元数据以及组件类组成,实现组件类 ...
- linux操作oracle
1.su - oracle 2.sqlplus / as sysdba; 1.登录linux,以oracle用户登录(如果是root用户登录的,登录后用 su - oracle命令切换成oracle用 ...
- iOS 图片选择器 总结
UIImagePickerController #pragma mark 从用户相册获取活动图片 - (void)pickImageFromAlbum{ imagePicker = [[UIImage ...
- form.submit(回调函数)——引用jq-form.js
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding= ...
- php中session原理及安全性问题
有一点我们必须承认,大多数web应用程序都离不开session的使用.这篇文章将会结合php以及http协议来分析如何建立一个安全的会话管理机制 我们先简单的了解一些http的知识,从而理解该协议 ...
- swiper框架,实现轮播图等滑动效果
http://www.swiper.com.cn/ 做个记录而已,这个没什么好说的,对于需要手机端开发实现触摸等方式可以看看.
- Github上安卓榜排名第2的程序员教你如何学习【转载,侵删】
来自:峰瑞资本(微信号:freesvc)文章作者:代码家(微信 ID:daimajia_share) 软件早已吞噬整个世界,程序员是关键角色.过去 40 年中,许多伟大的公司都由程序员缔造,比如比尔· ...
- [开源]用MQL4实现MD5加密
本文转载自博客园:混沌的世界 原文地址:http://www.cnblogs.com/niniwzw/archive/2009/12/05/1617685.html 在用MQL4进行金融交易的时候,经 ...
- 2016中国大学生程序设计竞赛 - 网络选拔赛 C. Magic boy Bi Luo with his excited tree
Magic boy Bi Luo with his excited tree Problem Description Bi Luo is a magic boy, he also has a migi ...