2015.1.3 DataGridView中嵌入其它控件
1、按正常方法绑定待嵌入列的值,先赋值为空也行。
2、添加combbox到datagrivdview中 dvaw.Controls.Add(cb_dir);
3、添加DataGridView MouseClick事件
private void dvaw_MouseClick(object sender, MouseEventArgs e)
{
int row = GetRowIndexAt(e.Y); //点击行
int comcol = dvaw.Columns.Count - 1;//要出现combox的列
if (row>=0 && btn_alter.Text == "确认修改")
{
Rectangle rect = dvaw.GetCellDisplayRectangle(comcol, row, false);//列,行,是否只要显示部分
cb_dir.Text = dvaw.Rows[row].Cells[comcol].Value.ToString(); //原来的值作为嵌入combbox的初始值
//将嵌入控件显示在当前cell中
cb_dir.Left = rect.Left;
cb_dir.Top = rect.Top;
cb_dir.Width = rect.Width;
cb_dir.Height = rect.Height;
cb_dir.Visible = true;
if (dvaw.CurrentCell != null && dvaw.CurrentCell.ColumnIndex == comcol)
{
//当点中方向列时,combbox自动下拉************
cb_dir.DroppedDown = true;
}
}
else
cb_dir.Visible = false;//不满足combbox显示条件时,隐藏combbox
}
4. 添加判断鼠标点击时所在行函数,比CellClick的好处是点击空白处也能判断-1
public int GetRowIndexAt(int mouseLocation_Y)
{
if (dvaw.FirstDisplayedScrollingRowIndex < 0)
{
return -1;
}
if (dvaw.ColumnHeadersVisible == true && mouseLocation_Y <= dvaw.ColumnHeadersHeight)
{
return -1;
}
int index = dvaw.FirstDisplayedScrollingRowIndex;
int displayedCount = dvaw.DisplayedRowCount(true);
for (int k = 1; k <= displayedCount; )
{
if (dvaw.Rows[index].Visible == true)
{
Rectangle rect = dvaw.GetRowDisplayRectangle(index, true); // 取该区域的显示部分区域
if (rect.Top <= mouseLocation_Y && mouseLocation_Y < rect.Bottom)
{
return index;
}
k++;
}
index++;
}
return -1;
}
5. 添加combbox的SelectedIndexChanged 事件
//每次选择combox值时,自动修改对应cell的显示值,虽然此时cell隐藏在combox后面,当combox隐藏时,该新值自动显示出来
private void cb_dir_SelectedIndexChanged(object sender, EventArgs e)
{
int comcol = dvaw.Columns.Count - 1;//要出现combox的列
//注意,下行不能用Currentcell来做判断,因为当点击第一列时,也能选择combbox,但此时的当前cell就不是第二列,会导致选择无效。所以用currentrow来判断
if(btn_alter.Text == "确认修改")
{
if (dvaw.CurrentRow != null)
dvaw.CurrentRow.Cells[comcol].Value = ((ComboBox)sender).Text;
else
MessageBox.Show("请先选中航路所在行,再选择方向");
}
}
最新文章
- Java2_JDK的安装和配置
- python数字图像处理(9):直方图与均衡化
- Easyui使用记录
- css+div盒模型研究笔记
- 随机产生字母a--z, A-Z 的任意组合
- centos6 kvm网卡桥接
- Translate one
- 关于php判断中文字符的问题
- D - 小晴天老师系列——晴天的后花园
- 《算法导论》习题2.3-5 二分搜索 Binary Search
- python glob fnmatch 用于文件查找操作
- JFinal中使用QuartzPlugin报ClassCastException解决方法
- Hive分区
- mybatis generator使用方式
- pooling的几种形式(转)
- javascript 体验倒计时:距离国庆还有多长时间
- (转)Fabric 1.0 读写集
- [CALayer release]: message sent to deallocated instance
- Android手机使用广播监听手机收到的短信
- (13)如何使用Cocos2d-x 3.0制作基于tilemap的游戏:第一部分