How can i drag a datarow from a datagrid to other control?

  • Thread starter Thread starter NewSun
  • Start date Start date
N

NewSun

How can i drag a datarow from a datagrid to a other control?
e.g.
Drag a datarow from a datagrid to a TextBox,Then display one colnumn' value
of the datarow .
Or Drag a cell's value to a controll and display/handle the value in the
other controll.


Thanks in advance.
 
Hi,

When the drag operation begins, retrieve the underlying DataRow (there was a
recent thread in the windowsforms.controls newsgroup where I have given an
example on how to do that). Then extract the data from the data row and add
a data object being dragged (you can probably determine the column index for
the cell being dragged and add the plain cell text as the data object).

The destinatation object, if it can accept the data being dragged, will
respond accordingly to the drop operation.
 
Thank you for your kind answer
I could get the "current" data row .
grdDir is inherited from the class defined by me .I add a method called
GetCurrentRow.
The code can run into grdDir_MouseDown, and can get the "current" data
row .
But the code can not run into tbID_DragEnter or tbID_DragDrop.
Here is my code:


private void grdDir_MouseDown(object sender,
System.Windows.Forms.MouseEventArgs e)
{
if(e.Button == MouseButtons.Left)
{
DataRow pDataRow = grdDir.GetCurrentRow();
if(pDataRow != null)
{

grdDir.DoDragDrop(pDataRow["ModuelID"].ToString(),DragDropEffects.Move);
}
}
}

private void tbID_DragEnter(object sender,
System.Windows.Forms.DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.Text))
e.Effect = DragDropEffects.Move;
else
e.Effect = DragDropEffects.None;
}
private void tbID_DragDrop(object sender,
System.Windows.Forms.DragEventArgs e)
{
tbID.Text = e.Data.GetData(DataFormats.Text).ToString();
}
 
Dumb safety suggestion: ensure the TextBox's AllowDrop property is set to
"true".
 
Back
Top