user5475686
user5475686

Reputation:

drag and drop in datagridview using c# windows form application

This is my project. for this i need to drag data from listBox and drop it to datagridview cell. in that i need to get the message box contain droped row phone_number.

I complete drag and drop option but i did not know how to get the messagebox with the droped row phone_number.

i connect my datagridview and listbox to database

my coding is:

    private void listBox3_MouseDown(object sender, MouseEventArgs e)
    {
        listBox3.DoDragDrop(listBox3.SelectedItem, DragDropEffects.Copy);
    }
    private void dataGridView1_DragEnter_1(object sender, DragEventArgs e)
    {

        {
            if (e.Data.GetDataPresent(typeof(System.String)))
                e.Effect = DragDropEffects.Copy;
            else
                e.Effect = DragDropEffects.None;
        }
    }private void dataGridView1_DragDrop_1(object sender, DragEventArgs e)
    {

        if (e.Data.GetDataPresent(typeof(string)))
        {
            string dgv = dataGridView1.Columns[4].HeaderText == "phone_number" && is string;
            MessageBox.Show("data is "+ dgv);
    }
}

i tried lot but its not working.please help me in coding.

Upvotes: 2

Views: 2549

Answers (1)

Steve
Steve

Reputation: 216353

I suppose that your Listbox.Items contains a list of strings and if that's the case then you are missing a call to effectively retrieving the data dragged around from your listbox and showing that data, not the content of a grid header

private void dataGridView1_DragDrop_1(object sender, DragEventArgs e)
{

    if (e.Data.GetDataPresent(typeof(string)))
    {
        string item = (string)e.Data.GetData(typeof(System.String));
        MessageBox.Show("data is "+ item);

    }
}

Now, if I understand what you are trying to achieve, you want to set the content of the dropped-on cell but only if the header of that cell's column is "phone_number".

In this scenario, you have to convert the cursor coordinates passed in DragDrop event to coordinates relative to the grid. After that, you should ask to the grid what element has been clicked using the grid's HitTest method. If it is a cell you could easily discover if the cell belog to the column required.

private void dataGridView1_DragDrop_1(object sender, DragEventArgs e)
{
    if (e.Data.GetDataPresent(typeof(string)))
    {
        DataGridView dgv = sender as DataGridView;
        string item = (string)e.Data.GetData(typeof(System.String));

        // Conversion in coordinates relative to the data
        Point clientPoint = dgv.PointToClient(new Point(e.X, e.Y));

        // get the element under the drop coordinates
        DataGridView.HitTestInfo info = dgv.HitTest(clientPoint.X, clientPoint.Y);

        // If it is a cell....
        if (info.Type == DataGridViewHitTestType.Cell)
        {
            // and its column's header is the required one....
            if(dgv.Columns[info.ColumnIndex].HeaderText == "phone_number")
                dgv.Rows[info.RowIndex].Cells[info.ColumnIndex].Value = item;
        }
    }
}

Upvotes: 3

Related Questions