none
drag option in datagridview RRS feed

  • Question

  •     

    Starting time

    Ending time

    Loading time

    remarks

    10.00

    10.15

    0.06

    production

    11.00

    11.15

    0.06

    production

    12.15

    12.30

    0.06

    production

    i want to drag the loading time and remarks next rows and continuously

    Tuesday, January 1, 2019 10:06 AM

All replies

  • <style type="text/css">@page { margin: 2cm } p { margin-bottom: 0.25cm; line-height: 120% } a:link { so-language: zxx } </style>

    You can try this steps to get results. 

    Step 1
    Create a new Windows Forms Application and add a DataGridView and a ListBox control on the form.

    Step 2
    Write the following in the form load event to populate data in the DataGridView. AllowUserToAddRows is set to false to disable the option to add rows as it is not required. An asterisk (*) is displayed in the row header of the last row of the DataGridView if it is by default set to true:

    private void Form3_Load(object sender, EventArgs e)

    {

    string ConString = @"Data Source=DEEPAK\SQLSERVER2005;Initial Catalog=Employees;User ID=sa;Password=******";

    SqlConnection con = new SqlConnection(ConString);

    using (SqlDataAdapter sda = new SqlDataAdapter("SELECT DepartmentID, Name FROM Departments", con))

    {

    DataSet ds = new DataSet();

    sda.Fill(ds);

    if (ds.Tables.Count > 0)

    {

    dataGridView1.DataSource = ds.Tables[0].DefaultView;

    }

    }

    dataGridView1.AllowUserToAddRows = false;

    }

    Step 3

    Set the AllowDrop property of the ListBox to true, as in:

    listBox1.AllowDrop = true;

    The AllowDrop property is used to enable or disable users to drop data onto this control.

    Step 4

    • Write the following in the MouseDown event of the DataGridView:

    private void dataGridView1_MouseDown(object sender, MouseEventArgs e)
    {
    dataGridView1.DoDragDrop(dataGridView1.SelectedRows, DragDropEffects.Move);
    }

    The DoDragDrop method of a control is used to start a drag and drop operation. We call it from MouseDown event of the DataGridView. The first parameter is the data that we want to send in drag and drop operation. Here we are sending selected rows of the DataGridView. The second parameter is a DragDropEffects enumeration that provides the drag and drop operation effect. The cursor style changes accordingly while the drag and drop is being performed. Possible values are DragDropEffects.All, DragDropEffects.Copy, DragDropEffects.Link, DragDropEffects.Move, DragDropEffects.None and DragDropEffects.Scroll.

    • Write the following in the DragEnter event of the ListBox:

    private void listBox1_DragEnter(object sender, DragEventArgs e)
    {
    if (e.Data.GetDataPresent(typeof(DataGridViewSelectedRowCollection)))
    {
    e.Effect = DragDropEffects.Move;
    }
    }

    The DragEnter event is fired when an object is dragged into the control's area. The DragEventArgs.Data.GetDataPresent() method is used to check if the dragged data is a type of DataGridViewSelectedRowCollection. Then the drag effect is set using the DragEventArgs.Effect property. It is set to DragDropEffects.Move to show the dragged operation as move.

    • Write the following in the DragDrop event of the ListBox:

    private void listBox1_DragDrop(object sender, DragEventArgs e)
    {
    DataGridViewSelectedRowCollection rows = (DataGridViewSelectedRowCollection)e.Data.GetData(typeof(DataGridViewSelectedRowCollection));

    foreach (DataGridViewRow row in rows)
    {
    listBox1.Items.Add(row.Cells[1].Value);
    dataGridView1.Rows.Remove(row);
    }
    }

    The DragDrop event is fired when a drag and drop operation is finished. Here, selected rows of the DataGridView is returned into a DataGridViewSelectedRowCollection variable using the DragEventArgs.Data.GetData() method. Then a foreach loop is used to add rows of this collection to items of the ListBox using the ListBox.Items.Add() method. Finally that row is removed from the DataGridView using the DataGridView.Rows.Remove() method.

    Tuesday, January 1, 2019 10:17 AM
  • @ShrutiSigh you need to use code blocks when posting code, please do this as your current code is going to be difficult to anyone to read.

    Also clean up your post in general for things like

    <style type="text/css">@page { margin: 2cm } p { margin-bottom: 0.25cm; line-height: 120% } a:link { so-language: zxx } </style>


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Tuesday, January 1, 2019 12:41 PM
    Moderator
  • Hi Shiji Lalu,

    Is there any update? do you try the method that ShrutiSigh mentioned, if the issue still exists, please feel free let us know.

    Best regards,

    Zhanglong


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, January 8, 2019 2:43 AM
    Moderator