关于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中)使用设计器可以让我们轻松地完成窗体.按钮.标签.图片框等等控件的组合,我们可以轻易地做出界面 ...
随机推荐
- r-cnn学习(六):RPN及AnchorTargetLayer学习
RPN网络是faster与fast的主要区别,输入特征图,输出region proposals以及相应的分数. # ------------------------------------------ ...
- from collections import OrderedDict
在python中,dict这个数据结构由于hash的特性,是无序的,这在有时候会给我们带来一些麻烦,幸运的是, collections模块为我们提供了OrderdDict,当你要获取一个有序的字典对象 ...
- MST 001
一.String,StringBuffer, StringBuilder 的区别是什么?String为什么是不可变的? 答: 1.String是字符串常量,StringBuffer和StringB ...
- c#去掉小数点后的无效0
decimal d = 0.0500m; d.ToString("0.##")就出来了 也可以这样 string.Format("{0:0.##}",d000) ...
- 【Java EE 学习 17 下】【数据库导出到Excel】【多条件查询方法】
一.导出到Excel 1.使用DatabaseMetaData分析数据库的数据结构和相关信息. (1)测试得到所有数据库名: private static DataSource ds=DataSour ...
- foreach的用法
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- 网友分享 调用dll的语音朗读 不能变速,不好
调用 speeker.dll 这个文件被本人 放在文件里面,若有人需要可以 联系我 需要 mfc100ud.dll msvcr100d.dll 注:可以用D7 自带的ActiveX 里面的控件 ...
- 基于RXTX的串口通讯 windows64位系统可用
项目地址 http://download.csdn.net/detail/xqshishen/7739539
- myeclipse2014
myeclipse 2014-GA offline 迅雷下载 http://downloads.myeclipseide.com/downloads/products/eworkbench/201 ...
- xp系统重绘边框线不显示(首次加载没有触发paint事件)
同样是,重绘边框事件,win7系统显示正常,而xp系统却不显示,这是什么原因造成的呢? 于是,小编开始百度,不停的查找原因,通过一番查找,小编也意外的收获了一些内容: 例如:窗口的拖动,放大,缩小,等 ...