I have 2 datagridviews and i want to copy cells from the datagridview1 to datagridview2 (a cell at a time).I′m able to select the cell I want and drag it to the datagridview2 but the value is not showing...
I spent most of the night looking for a solution...Probably is a simple answer or I just need to sleep , but please help....
I have the following code
private void dataGridView1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
DataGridView.HitTestInfo info = dataGridView1.HitTest(e.X, e.Y);
if (info.RowIndex >= 0)
{
if (info.RowIndex >= 0 && info.ColumnIndex >= 0)
{
string text = (String)
dataGridView1.Rows[info.RowIndex].Cells[info.ColumnIndex].Value;
if (text != null)
dataGridView1.DoDragDrop(text, DragDropEffects.Copy);
}
}
}
}
private void dataGridView2_DragDrop(object sender, DragEventArgs e)
{
string cellvalue=e.Data.GetData(typeof(string)) as string;
Point cursorLocation=this.PointToClient(new Point(e.X,e.Y));
System.Windows.Forms.DataGridView.HitTestInfo hittest= dataGridView2.HitTest(cursorLocation.X,cursorLocation.Y);
if (hittest.ColumnIndex != -1
&& hittest.RowIndex != -1)
dataGridView2[hittest.ColumnIndex, hittest.RowIndex].Value = cellvalue;
}
private void dataGridView2_DragEnter(object sender, DragEventArgs e)
{
e.Effect = DragDropEffects.Copy;
}
And the designer.cs i have
// dataGridView1
//
this.dataGridView1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
this.dataGridView1.Location = new System.Drawing.Point(12, 12);
this.dataGridView1.Name = "dataGridView1";
this.dataGridView1.Size = new System.Drawing.Size(299, 150);
this.dataGridView1.TabIndex = 0;
this.dataGridView1.MouseDown += new System.Windows.Forms.MouseEventHandler(this.dataGridView1_MouseDown);
//
// dataGridView2
//
this.dataGridView2.AllowDrop = true;
this.dataGridView2.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
this.dataGridView2.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
this.Column1,
this.Column2});
this.dataGridView2.Location = new System.Drawing.Point(353, 141);
this.dataGridView2.Name = "dataGridView2";
this.dataGridView2.Size = new System.Drawing.Size(240, 150);
this.dataGridView2.TabIndex = 5;
this.dataGridView2.DragDrop += new System.Windows.Forms.DragEventHandler(this.dataGridView2_DragDrop);
this.dataGridView2.DragEnter += new System.Windows.Forms.DragEventHandler(this.dataGridView2_DragEnter);
//
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…