1. Define below events, and setting TreeView property AllowDrop = True
//private void dataGridView1_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
//{
// ataGridView1.DoDragDrop(e.RowIndex, DragDropEffects.Copy);
//}
private void dataGridView1_MouseDown(object sender, MouseEventArgs e)
{
dataGridView1.DoDragDrop(dataGridView1.SelectedRows, DragDropEffects.Copy);
}
private void treeView1_DragDrop(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(typeof(DataGridViewSelectedRowCollection)))
{
DataGridViewSelectedRowCollection selected = (DataGridViewSelectedRowCollection)e.Data.GetData(typeof(DataGridViewSelectedRowCollection));
foreach (DataGridViewRow row in selected)
{
string key = row.Cells["Index"].Value.ToString();
if (m_treeTable.Rows.Contains(key) == false)
{
DataRow dr = m_treeTable.NewRow();
dr["Index"] = row.Cells["Index"].Value.ToString();
dr["Name"] = row.Cells["Name"].Value.ToString();
dr["Father"] = row.Cells["Father"].Value.ToString();
m_treeTable.Rows.Add(dr);
AddTreeNode(dr);
}
}
}
this.treeView1.SelectedNode.BackColor = Color.White;
}
private void treeView1_DragEnter(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(typeof(DataGridViewSelectedRowCollection)))
{
e.Effect = DragDropEffects.Copy;
}
else
{
e.Effect = DragDropEffects.None;
}
}
private void treeView1_DragOver(object sender, DragEventArgs e)
{
TreeNode node = treeView1.SelectedNode;
if (node != null)
{
node.BackColor = Color.White;
}
Point targetPoint = treeView1.PointToClient(new Point(e.X, e.Y));
node = this.treeView1.GetNodeAt(targetPoint);
//TreeNode node = this.treeView1.GetNodeAt(e.X, e.Y);
if (node != null)
{
treeView1.SelectedNode = node;
treeView1.SelectedNode.BackColor = Color.LightBlue;
}
}
//{
// ataGridView1.DoDragDrop(e.RowIndex, DragDropEffects.Copy);
//}
private void dataGridView1_MouseDown(object sender, MouseEventArgs e)
{
dataGridView1.DoDragDrop(dataGridView1.SelectedRows, DragDropEffects.Copy);
}
private void treeView1_DragDrop(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(typeof(DataGridViewSelectedRowCollection)))
{
DataGridViewSelectedRowCollection selected = (DataGridViewSelectedRowCollection)e.Data.GetData(typeof(DataGridViewSelectedRowCollection));
foreach (DataGridViewRow row in selected)
{
string key = row.Cells["Index"].Value.ToString();
if (m_treeTable.Rows.Contains(key) == false)
{
DataRow dr = m_treeTable.NewRow();
dr["Index"] = row.Cells["Index"].Value.ToString();
dr["Name"] = row.Cells["Name"].Value.ToString();
dr["Father"] = row.Cells["Father"].Value.ToString();
m_treeTable.Rows.Add(dr);
AddTreeNode(dr);
}
}
}
this.treeView1.SelectedNode.BackColor = Color.White;
}
private void treeView1_DragEnter(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(typeof(DataGridViewSelectedRowCollection)))
{
e.Effect = DragDropEffects.Copy;
}
else
{
e.Effect = DragDropEffects.None;
}
}
private void treeView1_DragOver(object sender, DragEventArgs e)
{
TreeNode node = treeView1.SelectedNode;
if (node != null)
{
node.BackColor = Color.White;
}
Point targetPoint = treeView1.PointToClient(new Point(e.X, e.Y));
node = this.treeView1.GetNodeAt(targetPoint);
//TreeNode node = this.treeView1.GetNodeAt(e.X, e.Y);
if (node != null)
{
treeView1.SelectedNode = node;
treeView1.SelectedNode.BackColor = Color.LightBlue;
}
}
在模仿中成长,在创新中成功