I want to drop some data from treeview to the datagridview temporarily, but the datagrid view is already loaded some data from a xml file.
Someone Please explain to me the mechanism of this.
This is my drag/drop function: //It works perfectly if there is no data in the datagrid view.
private void DataGridView1OnDragDrop(object sender, DragEventArgs e)
{
Point dscreen = new Point(e.X, e.Y);
Point dclient = dataGridView1.PointToClient(dscreen);
DataGridView.HitTestInfo hitTest = dataGridView1.HitTest(dclient.X, dclient.Y);
if (hitTest.ColumnIndex == 0 && hitTest.Type == DataGridViewHitTestType.Cell)
{
e.Effect = DragDropEffects.Move;
//dataGridView1.Rows.Insert(hitTest.RowIndex, "hitTest", "hitTest", "hitTest", "hitTest");
var data = (object[]) e.Data.GetData(typeof(string[]));
dataGridView1.Rows.Insert(hitTest.RowIndex, data);
}
else
{
e.Effect = DragDropEffects.None;
}
dataGridView1.AllowUserToAddRows = false;
}
For datagrid:
XmlReader xmlFile;
xmlFile = XmlReader.Create("Product.xml", new XmlReaderSettings());
DataSet ds = new DataSet();
ds.ReadXml(xmlFile);
dataGridView1.DataSource = ds.Tables[0];
For Treeview:
var filename = @"C:\Check.xml";
//First, we'll load the Xml document
XmlDocument xDoc = new XmlDocument();
xDoc.Load(filename);
Instead of adding the drag-drop-data to the control you should add it to the datasource of the control.
Error Message in DebugMode
worked fine for me – if any error shows up in your code, let me know