dimanche 22 mars 2015

how to drag and drop from datagridview to richtextbox in C#?

As title, I want to drag 1 cell from datagridview to richtextbox ? I tried with my code but it doesn't work. I set rtb_ArticleTemplate.AllowDrop = true in constructor and I set EnableAutoDragDrop of richtextbox is true



private void rtb_ArticleTemplate_DragEnter(object sender, DragEventArgs e)
{
string tt = e.Data.GetData(DataFormats.Text).ToString();
if (e.Data.GetDataPresent(DataFormats.Text))

e.Effect = DragDropEffects.Copy;
else
e.Effect = DragDropEffects.None;
}



private void rtb_ArticleTemplate_DragDrop(object sender, DragEventArgs e)
{
int i;
String s;

// Get start position to drop the text.
i = rtb_ArticleTemplate.SelectionStart;
s = rtb_ArticleTemplate.Text.Substring(i);
rtb_ArticleTemplate.Text = rtb_ArticleTemplate.Text.Substring(0, i);

// Drop the text on to the RichTextBox.
rtb_ArticleTemplate.Text = rtb_ArticleTemplate.Text + e.Data.GetData(DataFormats.Text).ToString();
rtb_ArticleTemplate.Text = rtb_ArticleTemplate.Text + s;
}



private void dgv_Xpath_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
DataGridView.HitTestInfo info = dgv_Xpath.HitTest(e.X, e.Y);
if (info.RowIndex >= 0)
{
if (info.RowIndex >= 0 && info.ColumnIndex >= 0)
{
string text = (string)
dgv_Xpath.Rows[info.RowIndex].Cells[info.ColumnIndex].Value;
if (text != null)
dgv_Xpath.DoDragDrop(text, DragDropEffects.Copy);
}
}
}
}

Aucun commentaire:

Enregistrer un commentaire