1. 这个问题有两种方法 第一种是用DataGridview中自带的DataGridViewTextBoxColumn 控件,第二种是动态添加combobox控件
  2.  
  3. 方法一:
  4.  
  5. 首先 窗体上拖拽一个 DataGridview
  6.  
  7. 然后在这个DataGridview中添加两列DataGridViewTextBoxColumn (第一列叫A,第二列叫B
  8.  
  9. 然后绑定A代码
  10.  
  11. A.DataSource = ds.Tables[].DefaultView;
  12. A.DisplayMember = "table_name";
  13. A.ValueMember = "table_name";
  14. ((DataGridViewComboBoxColumn)dataGridView1.Columns[]).DefaultCellStyle.NullValue = "--请选择--"; //默认值
  15.  
  16. 其次是绑定B代码
  17.  
  18. //当前选中行的第二列赋值
  19.  
  20. ((DataGridViewComboBoxCell)dataGridView1.Rows[dataGridView1.CurrentCell.RowIndex].Cells[]).DataSource = ds.Tables[].DefaultView;
  21. ((DataGridViewComboBoxCell)dataGridView1.Rows[dataGridView1.CurrentCell.RowIndex].Cells[]).DisplayMember = "comments";
  22. ((DataGridViewComboBoxCell)dataGridView1.Rows[dataGridView1.CurrentCell.RowIndex].Cells[]).ValueMember = "column_name";
  23. ((DataGridViewComboBoxColumn)dataGridView1.Columns[]).DefaultCellStyle.NullValue = "--请选择--";
  24.  
  25. 然后添加SelectedIndexChanged事件代码
  26.  
  27. private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
  28. {
  29. DataGridView dgv = (DataGridView)sender;
  30. if (dgv.CurrentCell.OwningColumn.Name == "A")
  31. {
  32. ComboBox cb = (ComboBox)e.Control;
  33. cb.SelectedIndexChanged += new EventHandler(comboBox_SelectedIndexChanged);
  34. }
  35. }
  36.  
  37. SelectedIndexChanged事件代码
  38.  
  39. public void comboBox_SelectedIndexChanged(object sender, EventArgs e)
  40. {
  41. ComboBox comboBox = (ComboBox)sender;
  42. if (dataGridView1.CurrentCell.OwningColumn.Name == "表名")
  43. {
  44. if (comboBox.Text != "")
  45. {
  46.  
  47. //这是绑定B的方法
  48. DBFieldNote(comboBox.Text);
  49. }
  50. }
  51. }
  52.  
  53. 方法二:
  54.  
  55. 首先实例化combobox对象
  56.  
  57. private ComboBox comboBox = new ComboBox();
  58.  
  59. private ComboBox cb = new ComboBox();
  60.  
  61. 其次:
  62.  
  63. this.dataGridView1.Controls.Add(comboBox);//将控件添加到DataGridview中
  64. DBTableName();//绑定comboBox
  65. comboBox.Visible = false;
  66. comboBox.SelectedIndexChanged += new EventHandler(comboBox_SelectedIndexChanged);//添加事件
  67.  
  68. private void comboBox_SelectedIndexChanged(object sender, EventArgs e)
  69. {
  70. string name = ((ComboBox)sender).Text;
  71. dataGridView1.CurrentCell.Value = name;//将选中的值添加到DataGridview当前选中的单元格里
  72. DBFieldNote(name);//绑定第二个combobox(也就是cb)
  73. }
  74.  
  75. public void DBFieldNote(string tablename)
  76. {
  77. this.dataGridView1.Controls.Add(cb);
  78. cb.Visible = false;
  79. cb.SelectedIndexChanged += new EventHandler(cb_SelectedIndexChanged);
  80. ......
  81. }
  82.  
  83. private void cb_SelectedIndexChanged(object sender, EventArgs e)
  84. {
  85. dataGridView1.CurrentCell.Value = cb.Text;
  86. }
  87. private void dataGridView1_CurrentCellChanged(object sender, EventArgs e)
  88. {
  89. if (dataGridView1.CurrentCell != null)
  90. {
  91. if (dataGridView1.CurrentCell.ColumnIndex == )//如果选中的是第一列 就显示第一个combobox
  92. {
  93. System.Drawing.Rectangle rect = dataGridView1.GetCellDisplayRectangle(dataGridView1.CurrentCell.ColumnIndex, dataGridView1.CurrentCell.RowIndex false);//获取当前选中的单元格的属性(宽 ,高等)
  94. comboBox.Left = rect.Left;
  95. comboBox.Top = rect.Top;
  96. comboBox.Width = rect.Width;
  97. comboBox.Height = rect.Height;
  98. comboBox.Visible = true;
  99. }
  100. else if (dataGridView1.CurrentCell.ColumnIndex==)//如果是选中第二列就显示cb
  101. {
  102. System.Drawing.Rectangle rect1 = dataGridView1.GetCellDisplayRectangle(comboxIndex + , dataGridView1.CurrentCell.RowIndex, false);
  103. cb.Left = rect1.Left;
  104. cb.Top = rect1.Top;
  105. cb.Width = rect1.Width;
  106. cb.Height = rect1.Height;
  107. cb.Visible = true;
  108. }
  109.  
  110. }
  111. else
  112. {
  113. comboBox.Visible = false;
  114.  
  115. }
  116. }

winfrom datagridview中DataGridViewTextBoxColumn的联动处理的更多相关文章

  1. Winfrom DataGridView中使用Tooltip

    第一步:添加DataGridView.Tooltip控件,略 第二步:设置ToolTip 相关属性,略,参考下图 第三步:DataGridView 添加 CellMouseEnter.CellMous ...

  2. DataGridView 中添加CheckBox和常用处理方式 .

    DataGridView 中添加CheckBox和常用处理方式 文章1 转载:http://blog.csdn.net/pinkey1987/article/details/5267934 DataG ...

  3. Datagridview 添加checkbox列,并判断Datagridview 中的checkbox列是否被选中

    Solution1://In Fill DataGridViewEvent : DataGridViewCheckBoxColumn ChCol = new DataGridViewCheckBoxC ...

  4. 将listBox中信息显示在dataGridview中,操作datagridview后删除listBox信息和SQL数据库信息 续(浅谈listBox..)

    应用场景      对datagridview控件使用了解,以及操作datagridview选中的信息删除,并且有二次确认后才删除用户信息.相应的删除listbox中用户信息,下面一起看看需要哪些准备 ...

  5. 在Datagridview中添加datagridviewComboBox列并显示下拉列表

    在DataGridView中自动的添加Column. private void button_autoAddColumn_Click(object sender, EventArgs e) { try ...

  6. [Winform] DataGridView 中 DataGridViewComboBox 的可编辑

    在 DataGridView 中设置的 DataGridViewComboBox,默认是不可编辑的,即使将其列属性 DisplayStyle 设置成 ComboBox 或其他,也无法编辑: 故作如下处 ...

  7. 禁用datagridview中的自动排序功能

    把datagridview中的自动排序功能禁用自己收集的两种方法,看看吧①DataGridView中的Columns属性里面可以设置.进入"EditColumns"窗口后,在相应的 ...

  8. C#读取Excel表格数据到DataGridView中和导出DataGridView中的数据到Excel

    其实想在datagridview中显示excel表格中的数据跟读取数据库中的数据没什么差别,只不过是创建数据库连接的时候连接字段稍有差别. private void btnShow_Click(obj ...

  9. DataGridView中实现checkbox全选的自定义控件

    在DataGridView中实现Checkbox的全选的方法就是在列头画一个checkbox, 并给其一个事件. 这个之前很多blog都有写, 这里就不多废话了,  codeproject上面有示例代 ...

随机推荐

  1. 解决linux下svn update 产生Node remains in conflict的问题

    提交一个文件 服务器上死活更新不了 是因为有冲突,解决办法:svn revert --depth=infinity /var/SvnProject/APITest 再次执行更新 进有改动的文件夹,更新 ...

  2. Python中字符串的截取,列表的截取

    字符串的截取 Python中的字符串用单引号 ' 或双引号 " 括起来,同时使用反斜杠 \ 转义特殊字符. 字符串的截取的语法格式如下: 变量[头下标:尾下标] 索引值以 0 为开始值,-1 ...

  3. axure—日期函数

    日期函数 日期函数中实现倒计时的关键点:1)gettime()函数可以取到1970年1月1日的时间,我们用倒计时结束的时间减去当前时间就能得到倒计时需要循环显示的所有时间.2)此处的“d”是倒计时结束 ...

  4. win(64位)环境下oracle11g的安装方法

    将压缩文件解压到一个目录中,该目录结构如下: 安装步骤(摘自网络): 1.进入数据库解压目录,双击其中的“setup.exe”文件,稍等片刻出现如下“配置安全更新“界面,取消“我希望通过My Orac ...

  5. CSS3D写3d画廊滚动

    CSS样式表 *{ margin: 0; padding: 0; } .wrapper{ width: 800px; height: 600px; background: #87CEEB; margi ...

  6. Python函数系列之eval()

    1.作用:将字符串str当成有效的表达式来求值并返回计算结果. 2.语法:eval(source[, globals[, locals]])  3.说明:参数:source:一个Python表达式或函 ...

  7. [转] js实现对图片的二进制流md5计算

    //计算图片md5 function img_MD5(img_path,callback) { plus.io.resolveLocalFileSystemURL(img_path, function ...

  8. Maya闪退

    电脑上装的Maya2015突然就打不开了,窗口闪一下就关闭,也没有任何提示. 将15卸载装了Maya2016还是一样. 再彻底卸载16,装了15,还不行... 将系统环境变量中PYTHONHOME和P ...

  9. python之requests 乱七八糟

    1.预配置 import requests ss = requests.Session() ss.headers.update({'user-agent':'Mozilla/5.0 (Windows ...

  10. python全栈开发day58-mysql存储过程,权限,索引,慢日志,执行计划,分页优化处理

    1.存储过程 delimiter // create procedure insert_data(in rows int) begin DECLARE n INT DEFAULT 1; drop ta ...