2
Reply

problem selecting cell after datagridviewcell Drop Down

try abc

try abc

Apr 24 2010 2:05 AM
4.5k
Hello.. 
I want to drag n drop Single cell.. such that i can copy the value form one cell to another.. i used following code .. everything works fine but  problem is that  after dropping cell the new cell where i am dropping value doesn't  highlight. and highlight remain on the first cell where i start dragging... 
Here is the code..   i try to set current cell .. but still didn't work.


  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 && info.ColumnIndex >= 0)
                                               {
                                                         string  text = Convert.ToString(dataGridView1.Rows[info.RowIndex].Cells[info.ColumnIndex].Value);
                                                         if (text != null)
                                                         {

                                                                   dataGridView1.DoDragDrop(text, DragDropEffects.Copy);
                                                         }
                                               }
                           }
                           
                 }
 private void dataGridView1_DragEnter(object sender, DragEventArgs e)
                 {
                           e.Effect = DragDropEffects.Copy;
                 }

private void dataGridView1_DragDrop(object sender, DragEventArgs e)
                 {
                           if (e.Data.GetDataPresent(typeof(System.String )))
                           {
                                     DataGridView dgv = (DataGridView)sender;
                                     Point p = dgv.PointToClient(new Point(e.X, e.Y));
                                     DataGridView.HitTestInfo info= dgv.HitTest(p.X, p.Y);
                                     if (info.RowIndex >= 0 && info.ColumnIndex >= 0)
                                     {
                                               
                                               dataGridView1[info.ColumnIndex,info.RowIndex].Value = e.Data.GetData(typeof(System.String));
                                               dgv.CurrentCell = dataGridView1[info.ColumnIndex, info.RowIndex];
                                               dgv.CurrentCell.Selected = true;
                                               
                                     }
                           }
                           
                 }

Answers (2)