datagridview中添加自定義按鈕


  在Winform程序中,經常需要對datagridview中的數據進行可編輯性操作。將一般控件“綁定”到datagridview中,在操作中更能體現“多功能”。下面以datagridview中輸入密碼格式的字段為例。

   一、在winform中添加datagridview,和一個textbox控件;

        

  二、在textbox的PasswordChar屬性修改為“*”,visible為False;

  三、分別添加如下事件:

 1   private void dataGridView1_CellEnter(object sender, DataGridViewCellEventArgs e)
 2  { 3 if (dataGridView1.CurrentCell.ColumnIndex == 3)//第三列為密碼 4  { 5  dataGridView1.Controls.Add(txtPassword); 6 if (txtPassword.ReadOnly==false) 7  { 8 txtPassword.Visible = true; 9  } 10  } 11 }

 

 1  private void dataGridView1_CurrentCellChanged(object sender, EventArgs e)
 2  { 3 DataGridViewCell cell = dataGridView1.CurrentCell; 4 if (cell!=null) 5  { 6 if (cell.OwningColumn.Index==3) 7  { 8 Rectangle rect = dataGridView1.GetCellDisplayRectangle(cell.ColumnIndex,cell.RowIndex,true); 9 txtPassword.Size = rect.Size; 10 txtPassword.Top = rect.Top; 11 txtPassword.Left = rect.Left; 12  } 13  } 14 }

 

 

 四、預期結果如下

     


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM