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("请先选中航路所在行,再选择方向");

}

}

最新文章

  1. Java2_JDK的安装和配置
  2. python数字图像处理(9):直方图与均衡化
  3. Easyui使用记录
  4. css+div盒模型研究笔记
  5. 随机产生字母a--z, A-Z 的任意组合
  6. centos6 kvm网卡桥接
  7. Translate one
  8. 关于php判断中文字符的问题
  9. D - 小晴天老师系列——晴天的后花园
  10. 《算法导论》习题2.3-5 二分搜索 Binary Search
  11. python glob fnmatch 用于文件查找操作
  12. JFinal中使用QuartzPlugin报ClassCastException解决方法
  13. Hive分区
  14. mybatis generator使用方式
  15. pooling的几种形式(转)
  16. javascript 体验倒计时:距离国庆还有多长时间
  17. (转)Fabric 1.0 读写集
  18. [CALayer release]: message sent to deallocated instance
  19. Android手机使用广播监听手机收到的短信
  20. (13)如何使用Cocos2d-x 3.0制作基于tilemap的游戏:第一部分

热门文章

  1. R语言学习笔记(4)
  2. socket io 记得flush
  3. Web 应用程序项目与 Visual Studio 中的网站项目的异同
  4. DATEDIFF 的用法
  5. vue v-on监听事件
  6. codeforces 705B:Spider Man
  7. 爬虫之解析库-----re、beautifulsoup、pyquery
  8. nova Scheduling 配置
  9. review18
  10. 解析PHP中intval()等int转换时的意外异常情况