DragDrop - add a column

  • Thread starter Thread starter dbuchanan
  • Start date Start date
D

dbuchanan

Hello,

I have a source dgv with 4 columns and a target dgv with 5 columns.

I would like to supply column #1 in the target dgv with a value contained in
a variable. All rows resulting from the drag drop would therefore receive
the identical integer value for column #1.

How do I do this?

Thank you,

Douglas
 
Hi Douglas,
All rows resulting from the drag drop would therefore receive the
identical integer value for column #1.

I don't understand what you mean in the above sentence. Could you please
explain more what you would like to do? It would be better if you give an
example.

I look forward to your reply.

Sincerely,
Linda Liu
Microsoft Online Community Support

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
Hi Linda,

Here are the two dgv columns:

DragDrop Source dgv
Columns:
pk Matl Typ Cate Dept
-- ----- --- ---- -----
3 2x4x8 Fir Dime Struc
4 2x6x8 Fir Dime Struc
8 4x8x.2 Ply Plyw Struc
9 4x4x8 Prs Dime Struc

DragDrop Target dgv
Columns:
fk pk Matl yp Cate Dept
-- -- ----- --- ---- -----
6 3 2x4x8 Fir Dime Struc
6 4 2x6x8 Fir Dime Struc
6 8 4x8x.2 Ply Plyw Struc
6 9 4x4x8 Prs Dime Struc

You see, I want the target dgv to have one more column than the source dgv
and I want to supply the value of the new column from the value of a
variable that I populate.

Thank you,
Douglas
 
Hi Douglas,

Thank you for your reply and more information!

I will give you an example. It requires that you create a WinForm
application project and add two DataGridView controls on the form named
dataGridView1 and dataGridView2. Add two DataGridViewTextBoxColumn in the
dataGridView1 and add three DataGridViewTextBoxColumn in the dataGridView2.

public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();

}
int foreignkey;
private void Form1_Load(object sender, EventArgs e)
{
foreignkey = 6;

this.dataGridView2.AllowDrop = true;

this.dataGridView1.MouseMove += new
MouseEventHandler(dataGridView1_MouseMove);
this.dataGridView2.DragEnter += new
DragEventHandler(dataGridView2_DragEnter);
this.dataGridView2.DragDrop += new
DragEventHandler(dataGridView2_DragDrop);

this.dataGridView1.RowCount = 3;
this.dataGridView1.Rows[0].Cells[0].Value = "1";
this.dataGridView1.Rows[0].Cells[1].Value = "11";
this.dataGridView1.Rows[1].Cells[0].Value = "2";
this.dataGridView1.Rows[1].Cells[1].Value = "22";
this.dataGridView1.Rows[2].Cells[0].Value = "3";
this.dataGridView1.Rows[2].Cells[1].Value = "33";
}

void dataGridView2_DragDrop(object sender, DragEventArgs e)
{
DataGridViewRow row = e.Data.GetData(typeof(DataGridViewRow))
as DataGridViewRow;
if (row != null)
{
DataGridViewRow newrow = row.Clone() as DataGridViewRow;
newrow.Cells.Add(new DataGridViewTextBoxCell());
newrow.Cells[0].Value = foreignkey;
for (int i = 0; i < row.Cells.Count; i++)
{
newrow.Cells[i+1].Value = row.Cells.Value;
}
this.dataGridView2.Rows.Add(newrow);
}
}

void dataGridView2_DragEnter(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(typeof(DataGridViewRow)))
{
e.Effect = DragDropEffects.Copy;
}
}

void dataGridView1_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{

this.dataGridView1.DoDragDrop(this.dataGridView1.CurrentRow,
DragDropEffects.All);
}
}
}

Build and run the application. Drag a row in the dataGridView1 and drop it
onto the dataGridView2. You should see a row is added into the
dataGridView2 with the first column containing the value of 6.

Hope this helps.
If you have anything unclear, please feel free to let me know.

Sincerely,
Linda Liu
Microsoft Online Community Support

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

This posting is provided "AS IS" with no warranties, and confers no rights.
 
Linda,

(... and there was one more thing I needed to do. Add the form load event to
the form.)

It works!

Thank you very much,

Douglas
 
Back
Top