在datagridview里添加自定义控件列

1、定义一个控件如:commbox

  

  1. //定义下拉列表框、隐藏控件、在datagridview中添加控件  
  2.        private ComboBox datacmb=new ComboBox();  
  3.       datacmb.Visible = false;  
  4.       this.dataGridView1.Controls.Add(datacmb);  

 

 

2、添加datagridview的CurrentCellChanged事件

  

  1. private void dataGridView1_CurrentCellChanged(object sender, EventArgs e)  
  2.         {  
  3.             try  
  4.             {  
  5.                 if (this.dataGridView1.CurrentCell.ColumnIndex == 4)  
  6.                 {  
  7.                     Rectangle rec = this.dataGridView1.GetCellDisplayRectangle(dataGridView1.CurrentCell.ColumnIndex, dataGridView1.CurrentCell.RowIndex, false);  
  8.                     datacmb.Left = rec.Left;  
  9.                     datacmb.Top = rec.Top;  
  10.                     datacmb.Width = rec.Width;  
  11.                     datacmb.Height = rec.Height;  
  12.                     datacmb.Visible = true;                    
  13.                 }  
  14.                 else  
  15.                 {  
  16.                     datacmb.Visible = false;  
  17.                 }  
  18.             }  
  19.             catch  
  20.             {  
  21.                 //MessageBox.Show(ex.Message);  
  22.             }  
  23.         }  

 

 
posted @ 2011-11-09 16:34  Bruce-He  阅读(1761)  评论(1编辑  收藏  举报
成都共享办公室