现在的位置: 首页 > 综合 > 正文

在datagridview里添加自定义控件列

2012年02月28日 ⁄ 综合 ⁄ 共 1096字 ⁄ 字号 评论关闭

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.         }  

 

 

抱歉!评论已关闭.