繁体   English   中英

使用C#Windows Form应用程序拖放Datagridview

[英]drag and drop in datagridview using c# windows form application

这是我的项目。 为此,我需要将数据从listBox拖放到datagridview单元中。 在那我需要使消息框中包含已删除的行phone_number。

我完成了拖放选项,但我不知道如何获取带有已删除行phone_number的消息框。

我将我的datagridview和listbox连接到数据库

我的编码是:

    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);
    }
}

我尝试了很多,但是没有用。请帮助我编码。

我想您的Listbox.Items包含一个字符串列表,如果是这种情况,那么您将丢失一个有效检索从列表框拖动的数据并显示该数据而不是网格标题内容的调用

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);

    }
}

现在,如果我了解您要实现的目标,则仅在该单元格列的标题为“ phone_number”的情况下,才需要设置该单元格的内容。

在这种情况下,您必须将在DragDrop事件中传递的光标坐标转换为相对于网格的坐标。 之后,您应该向网格询问使用网格的HitTest方法单击了哪个元素。 如果它是一个单元格,则可以轻松地发现该单元格是否成为必需的列。

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;
        }
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM