<bdo id='szD6g'></bdo><ul id='szD6g'></ul>
      1. <tfoot id='szD6g'></tfoot>

        <legend id='szD6g'><style id='szD6g'><dir id='szD6g'><q id='szD6g'></q></dir></style></legend>
      2. <small id='szD6g'></small><noframes id='szD6g'>

      3. <i id='szD6g'><tr id='szD6g'><dt id='szD6g'><q id='szD6g'><span id='szD6g'><b id='szD6g'><form id='szD6g'><ins id='szD6g'></ins><ul id='szD6g'></ul><sub id='szD6g'></sub></form><legend id='szD6g'></legend><bdo id='szD6g'><pre id='szD6g'><center id='szD6g'></center></pre></bdo></b><th id='szD6g'></th></span></q></dt></tr></i><div id='szD6g'><tfoot id='szD6g'></tfoot><dl id='szD6g'><fieldset id='szD6g'></fieldset></dl></div>

        在 DataGridView 中拖放行

        时间:2023-08-25
            <i id='YXt9F'><tr id='YXt9F'><dt id='YXt9F'><q id='YXt9F'><span id='YXt9F'><b id='YXt9F'><form id='YXt9F'><ins id='YXt9F'></ins><ul id='YXt9F'></ul><sub id='YXt9F'></sub></form><legend id='YXt9F'></legend><bdo id='YXt9F'><pre id='YXt9F'><center id='YXt9F'></center></pre></bdo></b><th id='YXt9F'></th></span></q></dt></tr></i><div id='YXt9F'><tfoot id='YXt9F'></tfoot><dl id='YXt9F'><fieldset id='YXt9F'></fieldset></dl></div>
              <bdo id='YXt9F'></bdo><ul id='YXt9F'></ul>

            • <legend id='YXt9F'><style id='YXt9F'><dir id='YXt9F'><q id='YXt9F'></q></dir></style></legend>

                <small id='YXt9F'></small><noframes id='YXt9F'>

                    <tbody id='YXt9F'></tbody>
                  <tfoot id='YXt9F'></tfoot>
                • 本文介绍了在 DataGridView 中拖放行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我有带有 DataGridView 的 windows 窗体,我需要在此 DataGridView 中启用拖放行.

                  I have windows form with DataGridView and I need to enable drag and drop rows in this DataGridView.

                  它是从数据库中填充的.我的代码无法正常工作,因为在第一次拖放后我无法将行放到正确的位置.

                  It is filled from the database. My code does not work properly because after the first drag and drop I cant drop row to the right position.

                  这是 Load 表单,其中 DataGridView 从数据库中填充

                  This is the Load forms where DataGridView is filled from database

                  DataTable bsPeople;
                  Rectangle dragBoxFromMouseDown;
                      int rowIndexFromMouseDown;
                      int rowIndexOfItemUnderMouseToDrop; 
                      private void Form1_Load(object sender, EventArgs e)
                      {
                          DataGridView1.AllowDrop = true;
                          bsPeople= objPeople.ReturnPeople(); // fill data from SQL Server 
                          DataGridView1.DataSource = bsPeople;
                      }
                  

                  这是拖放事件

                  private void dataGridView1_MouseMove(object sender, MouseEventArgs e)
                      {
                          if (((e.Button ==MouseButtons.Left)))
                          {
                              if (((dragBoxFromMouseDown != Rectangle.Empty)
                                          && !dragBoxFromMouseDown.Contains(e.X, e.Y)))
                              {
                                  DragDropEffects dropEffect = DataGridView1.DoDragDrop(DataGridView1.Rows[rowIndexFromMouseDown], DragDropEffects.Move);
                              }
                          }
                      }
                  
                      private void dataGridView1_MouseDown(object sender, MouseEventArgs e)
                      {
                          rowIndexFromMouseDown = DataGridView1.HitTest(e.X, e.Y).RowIndex;
                          if ((rowIndexFromMouseDown != -1))
                          {
                              Size dragSize = SystemInformation.DragSize;
                              dragBoxFromMouseDown = new Rectangle(new Point(e.X - (dragSize.Width / 2), e.Y - (dragSize.Height / 2)), dragSize);
                          }
                          else
                          {
                              dragBoxFromMouseDown = Rectangle.Empty;
                          }
                      }
                  
                      private void dataGridView1_DragOver(object sender, DragEventArgs e)
                      {
                          e.Effect = DragDropEffects.Move;
                      }
                  
                      private void dataGridView1_DragDrop(object sender, DragEventArgs e)
                      {
                          Point clientPoint = DataGridView1.PointToClient(new Point(e.X, e.Y));
                          rowIndexOfItemUnderMouseToDrop = DataGridView1.HitTest(clientPoint.X, clientPoint.Y).RowIndex;
                          if ((e.Effect == DragDropEffects.Move))
                          {
                              DataGridViewRow rowToMove = (DataGridViewRow)e.Data.GetData(typeof(DataGridViewRow));
                              object[] celldata=new object[DataGridView1.ColumnCount];
                              for (int col = 0; (col
                                          <= (rowToMove.Cells.Count - 1)); col++)
                              {
                                  celldata[col] = rowToMove.Cells[col].Value;
                              }
                  
                              DataRow row = bsPeople.NewRow();
                              row.ItemArray = celldata;
                              bsPeople.Rows.InsertAt(row, rowIndexOfItemUnderMouseToDrop);
                              rowToMove.DataGridView.Rows.Remove(rowToMove);
                  
                          }
                      }
                  }
                  

                  推荐答案

                  Drag &掉落

                  您需要删除并插入right DataRows.试试这个:

                  You need to delete and insert the right DataRows. Try this:

                  private void DGV_DragDropData(object sender, DragEventArgs e)
                  {
                     Point clientPoint = DGV.PointToClient(new Point(e.X, e.Y));
                     rowIndexOfItemUnderMouseToDrop =
                         DGV.HitTest(clientPoint.X, clientPoint.Y).RowIndex;
                     if (e.Effect == DragDropEffects.Move)
                     {
                        DataGridViewRow rowToMove = (DataGridViewRow)e.Data.GetData(typeof(DataGridViewRow));
                        // find the row to move in the datasource:
                        DataRow oldrow = ((DataRowView)rowToMove.DataBoundItem).Row;
                        // clone it:
                        DataRow newrow = bsPeople.NewRow();
                        newrow.ItemArray = oldrow.ItemArray;
                        bsPeople.Rows.InsertAt(newrow, rowIndexOfItemUnderMouseToDrop);
                        bsPeople.Rows.Remove(oldrow);
                     }
                  }
                  

                  请注意,代码仅移动 一个 行.要移动多行,还需要一些技巧..

                  Note that the code only moves one row. For moving multiple rows there are a few more tricks needed..

                  行状态:

                  另请注意,您可能需要设置新行的 RowState,具体取决于您的需要.添加后,您可能希望通过调用 newRow.AcceptChanges() 将其设置为 Unchanged 或设置为其他状态,具体取决于原始行的状态.

                  Also note that you may need to set the RowState of the new row, depending on your needs. As it is added you may want to set it it Unchanged by calling newRow.AcceptChanges() or maybe to some other state, depending on the state of the original row.

                  要修改 RowState,需要遵循一些规则:

                  To modify the RowState a few rules apply:

                  • DataRow 必须驻留在 DataTable 中;刚创建后,状态为Unattached,无法更改.
                  • SetAddedSetModified 方法只能更改 Unchanged 行;所以你必须先调用 AcceptChanges().
                  • The DataRow must reside in a DataTable; after you have just created it, the state is Unattached and you can't change it.
                  • Only Unchanged rows can be changed by the SetAdded or SetModified methods; so you must call AcceptChanges() first.

                  这应该可以完成工作:

                     if (oldrow.RowState != DataRowState.Unchanged) newrow.AcceptChanges();
                     if (oldrow.RowState == DataRowState.Added) newrow.SetAdded();
                     if (oldrow.RowState == DataRowState.Modified) newrow.SetModified();
                  

                  这些行必须在新行添加到表之后添加..

                  These lines must be added after the new row is added to the table..

                  这篇关于在 DataGridView 中拖放行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:C# 将一个图片框拖放到另一个图片框 下一篇:[C#][Selenium] 如何拖放元素

                  相关文章

                  最新文章

                  • <bdo id='Kl6zb'></bdo><ul id='Kl6zb'></ul>

                    <tfoot id='Kl6zb'></tfoot>
                  1. <i id='Kl6zb'><tr id='Kl6zb'><dt id='Kl6zb'><q id='Kl6zb'><span id='Kl6zb'><b id='Kl6zb'><form id='Kl6zb'><ins id='Kl6zb'></ins><ul id='Kl6zb'></ul><sub id='Kl6zb'></sub></form><legend id='Kl6zb'></legend><bdo id='Kl6zb'><pre id='Kl6zb'><center id='Kl6zb'></center></pre></bdo></b><th id='Kl6zb'></th></span></q></dt></tr></i><div id='Kl6zb'><tfoot id='Kl6zb'></tfoot><dl id='Kl6zb'><fieldset id='Kl6zb'></fieldset></dl></div>

                    <small id='Kl6zb'></small><noframes id='Kl6zb'>

                    <legend id='Kl6zb'><style id='Kl6zb'><dir id='Kl6zb'><q id='Kl6zb'></q></dir></style></legend>