datagridview數據拖到textbox中

datagridview數據拖到textbox中

View Code
 private void Form1_Load(object sender, EventArgs e)
         {
             List<users> list = new List<users>();
             for (int i = 0; i < 10; i++)
             {
                 users u = new users() { uid = i, name = i.ToString() };
                 list.Add(u);
             }
             this.dataGridView1.DataSource = list;
             this.textBox1.AllowDrop = true;//設置可接受拖放到上面數據
         }
 
  private void dataGridView1_MouseDown(object sender, MouseEventArgs e)
         {
             if (e.Button == System.Windows.Forms.MouseButtons.Left)
             {
                 DataGridView.HitTestInfo info = this.dataGridView1.HitTest(e.X, e.Y); //獲取行、列信息
                 if (info.RowIndex > -1 && info.ColumnIndex > -1)
                 {
                     string text = this.dataGridView1.Rows[info.RowIndex].Cells[info.ColumnIndex].Value.ToString();///獲取值
                     if (text != null)
                     {
                         this.dataGridView1.DoDragDrop(text, DragDropEffects.Copy);///開始拖放操作
                     }
                 }
             }
         }
 
 private void textBox1_DragEnter(object sender, DragEventArgs e)
         {
             e.Effect = DragDropEffects.Copy;
         }
 
         private void textBox1_DragDrop(object sender, DragEventArgs e)
         {
             if (e.Data.GetDataPresent(typeof(String)))
             {
                 this.textBox1.Text = e.Data.GetData(DataFormats.Text).ToString();
             }
         }

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章