Thursday, 4 December 2014

dataGridView to ListBox Drag Drop

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace Drag_and_Drop
{
    public partial class Form1 : Form
    {
        string sSelectedVal = ""; 

        public Form1()
        {
            InitializeComponent();
        }

        
        private void lstBx_DragEnter(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(DataFormats.Text))
                e.Effect = DragDropEffects.Copy;
            else
                e.Effect = DragDropEffects.None;
        }

        private void lstBx_DragDrop(object sender, DragEventArgs e)
        {
            lstBx.Items.Add(sSelectedVal);
       }

        private void Form1_Load(object sender, EventArgs e)
        {
            FillGrids();
        }

        private void dataGridView1_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
        {
            sSelectedVal = dataGridView1.SelectedCells[0].Value.ToString();
          dataGridView1.DoDragDrop(sSelectedVal, DragDropEffects.Copy | DragDropEffects.Move);
        }

        private void dataGridView2_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
        {
            sSelectedVal = dataGridView2.SelectedCells[0].Value.ToString();
           dataGridView2.DoDragDrop(sSelectedVal, DragDropEffects.Copy | DragDropEffects.Move);
        }

        private void FillGrids()
        {
            List<Student> students = new List<Student>()
             {
                  new Student{Roll = 1,Name="Rakesh" },
                  new Student{Roll = 2,Name="Suresh" },
                  new Student{Roll = 3,Name="Rajesh"},
                  new Student{Roll = 4,Name="Harish" }
             };
            dataGridView1.DataSource = students;

            List<Teacher> teachers = new List<Teacher>()
             {
                  new Teacher{id = 1,Name="dgg" },
                  new Teacher{id = 2,Name="gfdgg" },
                  new Teacher{id = 3,Name="gfdgg"},
                  new Teacher{id = 4,Name="gfdg" }
             };
            dataGridView2.DataSource = teachers;
        }
    }

    public class Student
    {
        public int Roll { get; set; }
        public string Name { get; set; }
       
    }
    public class Teacher
    {
        public int id { get; set; }
        public string Name { get; set; }
         
    }
}

No comments:

Post a Comment