关于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中)使用设计器可以让我们轻松地完成窗体.按钮.标签.图片框等等控件的组合,我们可以轻易地做出界面 ...
随机推荐
- oracle常用脚本
自动启动oracle sqlplus -S sys/123456 as sysdba <<EOF startup quit EOF lsnrctl start lsnrctl reload
- 安装KB3132372补丁后,WIN10中IE内核加载flash崩溃
今天(2015年12月30日)突然很多人反馈在WIN10上IE内核的PC端应用崩溃.经过一番查找,最终定位到问题.WIN10今天发布了新的补丁KB3132372,64位系统更新该补丁后,打开IE内核的 ...
- Eclipse下无法自动编译,或者WEB-INF/classes目录下没文件,编译失败的解决办法(转载)
文章来源:http://www.cnblogs.com/xfiver/archive/2010/07/07/1772764.html 1. IOException parsing XML docum ...
- js根据不同浏览器(分辨率)调用不同css
根据不同浏览器: <script type="text/javascript"> var version = navigator.userAgent; if (vers ...
- 监测程序运行的时间,stopWatch
ArrayList arrInt = new ArrayList(); //用stopwatch来计时 运行的时间 Stopwatch watch = new Stopwatch(); watch.S ...
- IP地址,子网掩码,默认网关,DNS服务器详解
为了更深入的学习TCP/IP协议,最近看了不少有关资料,收集整理记录如下,以备后面的使用和方便各位学习: IP地址,子网掩码,默认网关,DNS服务器是什么意思? (一) 问题解析 001. 问: ...
- SQLite -- 分页查询
原文:http://blog.csdn.net/lu1024188315/article/details/51734514 参考:http://www.runoob.com/sqlite/sqlite ...
- HDU3571 N-dimensional Sphere(高斯消元 同模方程)
每个点到中心距离相等,以第0个点为参考,其他n个点到中心距等于点0到中心距,故可列n个方程 列出等式后二次未知数相消,得到线性方程组 将每个数加上1e17,求答案是再减去,求解时对一个2 * (1e1 ...
- 【CentOS】虚拟机网络配置与远程登录
////////////////////////////////////11月16日更新////////////////////////////////////////////////////// 一 ...
- one_person年轻的程序员
回顾大学三年,通过良师的教导和自身的刻苦学习,我已初步掌握如何运用计算机编程,也养成了认真对待学习和工作的好习惯! 在思想品德上,本人有良好道德修养,并有坚定的政治方向.我热爱祖国,热爱人民,遵纪守法 ...