关于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中)使用设计器可以让我们轻松地完成窗体.按钮.标签.图片框等等控件的组合,我们可以轻易地做出界面 ...
随机推荐
- TCP/IP三次握手和HTTP过程
1.TCP连接 手机能够使用联网功能是因为手机底层实现了TCP/IP协议,可以使手机终端通过无线网络建立TCP连接.TCP协议可以对上层网络提供接口,使上层网络数据的传输建立在"无差别&qu ...
- espcms简约版的表单,提示页,搜索列表页
模板/lib/form.html <script type="text/javascript" src="{%$rootdir%}js/My97DatePicker ...
- 【转】Drawable /Bitmap、String/InputStream、Bitmap/byte[]
原文:http://wuxiaolong.me/2015/08/10/Drawable-to-Bitmap/ Drawable互转Bitmap Drawable转Bitmap 1234 Resourc ...
- Python: zip函数
zip函数接受任意多个(包括0个和1个)序列作为参数,返回一个tuple列表. 参考链接解释
- appCan uexLocation 定位功能
js的引用: <script src="../js/zy_control.js"></script> <script src="../js/ ...
- java学习第二天 回顾运算符
一.回顾运算符: 补充: 三元运算符. 代码: /* 三目运算符: 三元运算符: 结构: 条件?条件成立的结果 :条件不成立的结果 ; */ class Demo1 { public static v ...
- VR的国内研究现状及发展趋势
转载请声明转载地址:http://www.cnblogs.com/Rodolfo/,违者必究. 一.国内研究现状 我国虚拟现实技术研究起步较晚,与发达国家还有一定的差距. 随着计算机图形学.计算机系统 ...
- javaScript的function
一.函数的声明方式 1.普通的函数声明 function box(num1,num2){ return num1+num2; } 2.使用变量初始化函数 var box=function(num1,n ...
- UVA 11464 暴力+位运算 ***
题意:给你一个 n * n 的 01 矩阵,现在你的任务是将这个矩阵中尽量少的 0 转化为 1 ,使得每个数的上下左右四个相邻的数加起来是偶数.求最少的转化个数. 新风格代码 lrj书上说的很清楚了, ...
- 话说IOC(DI)
什么是IOC(DI) 书上的东东,因为说的太严谨,所以不太容易懂,其实也没那么复杂. 举几个例子: 1.文明点的:中午太热,不想出去吃饭,所以希望同事能帮忙带饭,现在有了点外卖平台,我们就可以直接在网 ...