I have a couple of databound gridviews on a form that I want to be able to drag a row from one grid to another. Gridview1 will be the grid that I drag from and Gridview2 will be were I drop the row or multiple rows. Gridview1 will contain some ID columns and full file path column that I will need to grab to drop onto Gridview2. Gridview2 also needs to be able to accept a drop from windows explorer from where I will be dragging files from also.
I found a sample of dragging rows from one grid to another but the code conflicts with the code I have that handles dragging and dropping files from windows explorer and doesn't work right with my code, mainly the dragging part… The sample provided works fine. http://www.devexpress.com/Support/Center/p/A1444.aspx
Are there some settings I need to check? I think the DragOver code makes the drag and drop from explorer not to work correctly… It causes it to ignore that ability because of the Effect being applied.
Drag and drop from windows explorer and grid
Answers approved by DevExpress Support
Hi George,
Yes, you are right. BindingSource (GridControl.DataSource) cannot be directly cast to DataTable. That is why the "table" variable value is empty. In my previous example, this variable was defined to determine whether or not rows were dragged and dropped between different GridControl instances.
If you are using BindingSource as the GridControl.DataSource property value, I suggest you utilize another approach that implies using a different check of row belonging.
I suggest you pass GridView in the DoDragDrop method as a parameter. You can use the GridView.GetSelectedRows() method to obtain dragged content and use GridView.DeleteSelectedRows() to clear it.
Attached is a modified example from my previous post, which illustrates the new approach.
Please let me know whether or not this solution meets your requirements.
Thanks,
Oleg
Hi,
In the attachment to this comment you can find the mentioned sample project, which I converted to the VB language by using the Instant VB tool.
I hope you find this sample project helpful.
Other Answers
Hello Everybody:
If you want to use this example with Visual Studio 2015 (and back a few versions) and Windows 10 (and back a few Windows versions) you will find that the drag and drop with outside files does not work inside Visual Studio if you run it with Administrative rights. You have to run it outside Visual Studio before that part will work.
Also, this project will have to be manually upgraded to framework 4 or higher in order for it to run the latest Devexpress version. You will have to go into references (after the auto-conversion) and remove the old Devexpress references and then put in your newer ones.
Hi George,
Thank you for contacting us. In your DragDrop event handler, you have the following line:
DataRow row = e.Data.GetData(typeof(DataRow)) as DataRow;
If the e.Data.GetData(typeof(DataRow)) method is not null, execute the code from the Knowledge Base article. Otherwise, it will mean that an end-user has dragged an object from Windows Explorer, and you need to execute your own code.
If you need any further assistance with this subject, feel free to reactivate this ticket.
Thanks
Dimitros
I've came across a problem in the DragDrop in my code. The table and row variables are both null. This is happening on the grid that I am dropping onto from GridView1. I suspect that DataRow isn't being populated or initialized correctly even thought everything seems to be working normally as far as databinding is concerned. The HELLO WORLD debug message never executes. The example from here http://www.devexpress.com/Support/Center/p/A1444.aspx has the datasource set as a clone of the datasource of the first grid. My grids have different information and columns and I'm dragging from the first grid to get some information from a few columns to populate the second grid. What should I be initializing first? I don't think I need to clone the dataset from the first grid. I won't ever be dropping anything on it…
private void grid_DragDrop(object sender, DragEventArgs e) {
try {
GridControl grid = sender as GridControl;
DataTable table = grid.DataSource as DataTable;
DataRow row = e.Data.GetData(typeof(DataRow)) as DataRow;
if (row != null && table != null && row.Table != table) {
Debug.WriteLine("HELLO WORLD!");
//table.ImportRow(row);
//row.Delete();
} else {
if (e.Data.GetDataPresent(DataFormats.FileDrop)) {
String[] myFiles = (String[])e.Data.GetData(DataFormats.FileDrop);
foreach (String fileName in myFiles) {
if (Path.GetExtension(fileName) == ".pdf") {
Debug.WriteLine("dragndrop=" + fileName);
gridViewAttachments.AddNewRow();
Int32 newRowHandle = gridViewAttachments.FocusedRowHandle;
gridViewAttachments.SetRowCellValue(newRowHandle, colID4, RequestID);
gridViewAttachments.SetRowCellValue(newRowHandle, colCreatedBy, ApplicationInformation.UserName);
gridViewAttachments.SetRowCellValue(newRowHandle, colCreatedOn, DateTime.Now);
gridViewAttachments.SetRowCellValue(newRowHandle, colCreatedByUserID, _userId);
gridViewAttachments.SetRowCellValue(newRowHandle, colICabRowID, 0);
gridViewAttachments.SetRowCellValue(newRowHandle, colTypeID, 2);
gridViewAttachments.SetRowCellValue(newRowHandle, colIcabDocTypeID, 29);
gridViewAttachments.SetRowCellValue(newRowHandle, colFullFileNameWithPath, fileName);
gridViewAttachments.UpdateCurrentRow();
}
}
}
}
} catch (Exception ex) {
LoggingMethods.saveErrorMessage("gridControliCabDocuments_DragDrop", 5, ex.ToString());
}
}
Hi George,
Thank you for additional clarification. I have created a simple example based on your description and sample of your code.
In this example, I have created two GridControls with different DataSources. It is possible to drag-and-drop data from one GridCOntrol to another, and also from windows explorer to GridControl.
Please take a moment to look at the attachment.
Let me know whether or not this solution meets your requirements.
Thanks,
Oleg
Your example works great however I'm still getting the null table value in grid_DragDrop and grid_DragOver when I use your code. I thinks its because I'm using a BindingSource as the datasource to my grids instead of a DataTable that's used in the example. I wound up doing something like this:
private void grid_DragDrop(object sender, DragEventArgs e) {
try {
if (e.Data.GetDataPresent(DataFormats.FileDrop)) {
String[] myFiles = (String[])e.Data.GetData(DataFormats.FileDrop);
foreach (String fileName in myFiles) {
if (Path.GetExtension(fileName) == ".pdf") {
gridViewAttachments.AddNewRow();
Int32 newRowHandle = GridControl.NewItemRowHandle;
gridViewAttachments.SetRowCellValue(newRowHandle, colID4, RequestID);
gridViewAttachments.SetRowCellValue(newRowHandle, colCreatedBy, ApplicationInformation.UserName);
gridViewAttachments.SetRowCellValue(newRowHandle, colCreatedOn, DateTime.Now);
gridViewAttachments.SetRowCellValue(newRowHandle, colCreatedByUserID, _userId);
gridViewAttachments.SetRowCellValue(newRowHandle, colICabRowID, 0);
gridViewAttachments.SetRowCellValue(newRowHandle, colTypeID, 2);
gridViewAttachments.SetRowCellValue(newRowHandle, colIcabDocTypeID, 29);
gridViewAttachments.SetRowCellValue(newRowHandle, colDocumentSenderName1, ApplicationInformation.UserName);
gridViewAttachments.SetRowCellValue(newRowHandle, colFullFileNameWithPath, fileName);
gridViewAttachments.UpdateCurrentRow();
}
}
} else {
Int32[] selectedRowCount = gridViewiCabDocs.GetSelectedRows();
for (int i = 0; i < selectedRowCount.Length; i++) {
DataRowView rowV = (DataRowView)gridViewiCabDocs.GetRow(selectedRowCount[i]);
gridViewAttachments.AddNewRow();
Int32 newRowHandle = GridControl.NewItemRowHandle;
gridViewAttachments.SetRowCellValue(newRowHandle, colID4, RequestID);
gridViewAttachments.SetRowCellValue(newRowHandle, colCreatedBy, ApplicationInformation.UserName);
gridViewAttachments.SetRowCellValue(newRowHandle, colCreatedOn, DateTime.Now);
gridViewAttachments.SetRowCellValue(newRowHandle, colCreatedByUserID, _userId);
gridViewAttachments.SetRowCellValue(newRowHandle, colICabRowID, rowV["DocumentQueueHolderID"]);
gridViewAttachments.SetRowCellValue(newRowHandle, colTypeID, 2);
gridViewAttachments.SetRowCellValue(newRowHandle, colIcabDocTypeID, rowV["DocumentTypeID"]);
gridViewAttachments.SetRowCellValue(newRowHandle, colFullFileNameWithPath, rowV["FullPickupPath"]);
gridViewAttachments.SetRowCellValue(newRowHandle, colNotes1, rowV["Notes"]);
gridViewAttachments.SetRowCellValue(newRowHandle, colSummary1, rowV["Summary"]);
gridViewAttachments.UpdateCurrentRow();
}
}
} catch (Exception ex) {
LoggingMethods.saveErrorMessage("gridControliCabDocuments_DragDrop", 5, ex.ToString());
}
}
private void grid_DragOver(object sender, DragEventArgs e) {
if (e.Data.GetDataPresent(DataFormats.FileDrop)) {
e.Effect = DragDropEffects.All;
} else {
e.Effect = DragDropEffects.Move;
}
}