Windows Develop Bookmark and Share   
 index > Windows Forms General > Drag drop data from datagridview to a listbox
 

Drag drop data from datagridview to a listbox

Hi All Experts,


I have a datagirdview in win form, which is bound to the collections of objects that implements the interface: IList, ICollections or IEnumerable.

I have the following code
            MediaObjectCollection allMediaObjects = (new
 MediaObjectBLL()).GetAllMediaObjectsByType(object_type);
dgvObjects.DataSource = allMediaObjects;


I try to drag the selected information from the datagridview and drop to a listbox. code as attached:

        private void dgvObjects_MouseDown(object sender, MouseEventArgs e)<br/>
        {<br/>
            if (e.Button == MouseButtons.Right)<br/>
            {<br/>
                DataGridView.HitTestInfo info = dgvObjects.HitTest(e.X,e.Y);<br/>
                if (info.RowIndex >= 0)<br/>
                {<br/>
                    DataGridViewRow view = (DataGridViewRow)dgvObjects.Rows[info.RowIndex].DataBoundItem;<br/>
                    //DataRowView view = (DataRowView)dgvObjects.Rows[info.RowIndex].DataBoundItem;<br/>
                    if (view != null)<br/>
                        dgvObjects.DoDragDrop(view,DragDropEffects.Copy);<br/>
                }<br/>
            }<br/>
        }<br/>
<br/>
        private void lbObjects_DragEnter(object sender, DragEventArgs e)<br/>
        {<br/>
            e.Effect = DragDropEffects.Copy;<br/>
        }<br/>
<br/>
<br/>
        private void lbObjects_DragDrop(object sender, DragEventArgs e)<br/>
        {<br/>
            if (e.Data.GetDataPresent(typeof(DataRowView)))<br/>
            {<br/>
                Point p = lbObjects.PointToClient(new Point(e.X, e.Y));<br/>
                int index = lbObjects.IndexFromPoint(p);<br/>
                if (index >= 0)<br/>
                {<br/>
                    DataRowView carousel_object = (DataRowView)lbObjects.Items[index];<br/>
                    DataRowView objects = (DataRowView)e.Data.GetData(typeof(DataRowView));<br/>
                    int newID = (int)carousel_object[0];<br/>
                    int oldID = (int)objects[1];<br/>
                    if (oldID != newID)<br/>
                    {<br/>
                        objects.BeginEdit();<br/>
                        objects[1] = newID;<br/>
                        objects.EndEdit();<br/>
                    }<br/>
                }<br/>
            }<br/>
        }
It gives me the error:

InvalidCastException was unhandled
Unable to cast object of type 'modelClasses.mediaobject' to type 'system.data.data.datarowview'.

I noticed that the datarowview caused the error maybe becoz i didnt use the dataset for my tables from the database.

Apparently first time working with dragdrop in datagridview, I hope I gave enough information for your guys. please kindly give me some feedback on this.

Thank you very much.

CC
  • Moved byTaylorMichaelLMVPThursday, September 17, 2009 1:43 PMWinForms related (From:Visual C# General)
  •  
Royal Oak  Thursday, September 17, 2009 1:25 PM

Hi,

->InvalidCastException was unhandled

Unable to cast object of type 'modelClasses.mediaobject' to type 'system.data.data.datarowview'.

->DataGridViewRow view = (DataGridViewRow)dgvObjects.Rows[info.RowIndex].DataBoundItem;

Because the DataGridView is bound to an object typed of modelClasses.mediaobject. You can changed this to:

modelClasses.mediaobject cdrag = dgvObjects.Rows[info.RowIndex].DataBoundItem as modelClasses.mediaobject;

Besides, in lbObjects_DragDrop event, you need also modify the type.

I write the following example for you. Hope this helps.

private void Form2_Load(object sender, EventArgs e)

{

dgvObjects.AllowDrop = true;

lbObjects.AllowDrop = true;

List<Custom> listsource = new List<Custom>();

listsource.Add(new Custom(1, "user1"));

listsource.Add(new Custom(2, "user2"));

listsource.Add(new Custom(3, "user3"));

listsource.Add(new Custom(4, "user4"));

dgvObjects.DataSource = listsource;

dgvObjects.AllowDrop = true;

lbObjects.AllowDrop = true;

dgvObjects.MouseDown += new MouseEventHandler(dgvObjects_MouseDown);

lbObjects.DragEnter += new DragEventHandler(lbObjects_DragEnter);

lbObjects.DragDrop += new DragEventHandler(lbObjects_DragDrop);

}

private void dgvObjects_MouseDown(object sender, MouseEventArgs e)

{

if (e.Button == MouseButtons.Right)

{

DataGridView.HitTestInfo info = dgvObjects.HitTest(e.X, e.Y);

if (info.RowIndex >= 0)

{

Custom cdrag = dgvObjects.Rows[info.RowIndex].DataBoundItem as Custom;

if (cdrag != null)

dgvObjects.DoDragDrop(cdrag, DragDropEffects.Copy);

}

}

}

private void lbObjects_DragEnter(object sender, DragEventArgs e)

{

e.Effect = DragDropEffects.Copy;

}

private void lbObjects_DragDrop(object sender, DragEventArgs e)

{

if (e.Data.GetDataPresent(typeof(Custom)))

{

Point p = lbObjects.PointToClient(new Point(e.X, e.Y));

int index = lbObjects.IndexFromPoint(p);

Console.WriteLine(index.ToString());

// if drag to the remain place of the listbox, the index is -1

if (index == -1)

index = lbObjects.Items.Count - 1;

Custom objects = e.Data.GetData(typeof(Custom)) as Custom;

int newID = int.Parse(lbObjects.Items[index].ToString());

int oldID = objects.ID;

if (oldID != newID)

{

//objects.BeginEdit();

objects.ID = newID;

//objects.EndEdit();

}

}

}

}

public class Custom

{

public Custom()

{

this._ID = -1;

this._Name = "User";

}

public Custom(int id, string name)

{

this._ID = id;

this._Name = name;

}

private int _ID;

private string _Name;

public int ID

{

get { return _ID; }

set { _ID = value; }

}

public string Name

{

get { return _Name; }

set { _Name = value; }

}

}

Best regards,

Ling Wang


Please remember to click “Mark as Answer�on the post that helps you, and to click “Unmark as Answer�if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
Ling Wang  Wednesday, September 23, 2009 2:32 PM

Hi,

->InvalidCastException was unhandled

Unable to cast object of type 'modelClasses.mediaobject' to type 'system.data.data.datarowview'.

->DataGridViewRow view = (DataGridViewRow)dgvObjects.Rows[info.RowIndex].DataBoundItem;

Because the DataGridView is bound to an object typed of modelClasses.mediaobject. You can changed this to:

modelClasses.mediaobject cdrag = dgvObjects.Rows[info.RowIndex].DataBoundItem as modelClasses.mediaobject;

Besides, in lbObjects_DragDrop event, you need also modify the type.

I write the following example for you. Hope this helps.

private void Form2_Load(object sender, EventArgs e)

{

dgvObjects.AllowDrop = true;

lbObjects.AllowDrop = true;

List<Custom> listsource = new List<Custom>();

listsource.Add(new Custom(1, "user1"));

listsource.Add(new Custom(2, "user2"));

listsource.Add(new Custom(3, "user3"));

listsource.Add(new Custom(4, "user4"));

dgvObjects.DataSource = listsource;

dgvObjects.AllowDrop = true;

lbObjects.AllowDrop = true;

dgvObjects.MouseDown += new MouseEventHandler(dgvObjects_MouseDown);

lbObjects.DragEnter += new DragEventHandler(lbObjects_DragEnter);

lbObjects.DragDrop += new DragEventHandler(lbObjects_DragDrop);

}

private void dgvObjects_MouseDown(object sender, MouseEventArgs e)

{

if (e.Button == MouseButtons.Right)

{

DataGridView.HitTestInfo info = dgvObjects.HitTest(e.X, e.Y);

if (info.RowIndex >= 0)

{

Custom cdrag = dgvObjects.Rows[info.RowIndex].DataBoundItem as Custom;

if (cdrag != null)

dgvObjects.DoDragDrop(cdrag, DragDropEffects.Copy);

}

}

}

private void lbObjects_DragEnter(object sender, DragEventArgs e)

{

e.Effect = DragDropEffects.Copy;

}

private void lbObjects_DragDrop(object sender, DragEventArgs e)

{

if (e.Data.GetDataPresent(typeof(Custom)))

{

Point p = lbObjects.PointToClient(new Point(e.X, e.Y));

int index = lbObjects.IndexFromPoint(p);

Console.WriteLine(index.ToString());

// if drag to the remain place of the listbox, the index is -1

if (index == -1)

index = lbObjects.Items.Count - 1;

Custom objects = e.Data.GetData(typeof(Custom)) as Custom;

int newID = int.Parse(lbObjects.Items[index].ToString());

int oldID = objects.ID;

if (oldID != newID)

{

//objects.BeginEdit();

objects.ID = newID;

//objects.EndEdit();

}

}

}

}

public class Custom

{

public Custom()

{

this._ID = -1;

this._Name = "User";

}

public Custom(int id, string name)

{

this._ID = id;

this._Name = name;

}

private int _ID;

private string _Name;

public int ID

{

get { return _ID; }

set { _ID = value; }

}

public string Name

{

get { return _Name; }

set { _Name = value; }

}

}

Best regards,

Ling Wang


Please remember to click “Mark as Answer�on the post that helps you, and to click “Unmark as Answer�if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
Ling Wang  Wednesday, September 23, 2009 2:32 PM

You can use google to search for other answers

Custom Search

More Threads

• className from webbrowser
• Crystal Report
• webBrowser.IsBusy always false??
• Re: modal form will not close
• Windows.Form resize...when?
• delete multipl items from list box-key driven
• graph in vb.net
• SQL Server Management Studio Express
• WebBrowser needed, must be at least IE 5.5 SP1
• Updating A Context Menu At Run-Time