想用拖动的方式,改变DataGridView中的行的位置,要如何实现呢?  还请高手赐教!! 先说谢谢啦!!!

解决方案 »

  1.   

    to wshuangminlg(asp.net 2.0 群:31412938(群主)):就是交换行的顺序啊,怎么不可以呢?
      

  2.   

    namespace ArrayMarge
    {
        partial class Form1
        {
            /// <summary>
            /// 必要なデザイナ変数です。
            /// </summary>
            private System.ComponentModel.IContainer components = null;        /// <summary>
            /// 使用中のリソースをすべてクリーンアップします。
            /// </summary>
            /// <param name="disposing">マネージ リソースが破棄される場合 true、破棄されない場合は false です。</param>
            protected override void Dispose(bool disposing)
            {
                if (disposing && (components != null))
                {
                    components.Dispose();
                }
                base.Dispose(disposing);
            }        #region Windows フォーム デザイナで生成されたコード        /// <summary>
            /// デザイナ サポートに必要なメソッドです。このメソッドの内容を
            /// コード エディタで変更しないでください。
            /// </summary>
            private void InitializeComponent()
            {
                this.dataGridView1 = new System.Windows.Forms.DataGridView();
                ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).BeginInit();
                this.SuspendLayout();
                // 
                // dataGridView1
                // 
                this.dataGridView1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
                this.dataGridView1.Location = new System.Drawing.Point(12, 12);
                this.dataGridView1.MultiSelect = false;
                this.dataGridView1.Name = "dataGridView1";
                this.dataGridView1.RowTemplate.Height = 21;
                this.dataGridView1.SelectionMode = System.Windows.Forms.DataGridViewSelectionMode.FullRowSelect;
                this.dataGridView1.Size = new System.Drawing.Size(514, 242);
                this.dataGridView1.TabIndex = 1;
                this.dataGridView1.MouseMove += new System.Windows.Forms.MouseEventHandler(this.dataGridView1_MouseMove);
                this.dataGridView1.CellMouseMove += new System.Windows.Forms.DataGridViewCellMouseEventHandler(this.dataGridView1_CellMouseMove_1);
                this.dataGridView1.CellMouseUp += new System.Windows.Forms.DataGridViewCellMouseEventHandler(this.dataGridView1_CellMouseUp);
                this.dataGridView1.CellMouseEnter += new System.Windows.Forms.DataGridViewCellEventHandler(this.dataGridView1_CellMouseEnter);
                this.dataGridView1.CellContentClick += new System.Windows.Forms.DataGridViewCellEventHandler(this.dataGridView1_CellContentClick);
                // 
                // Form1
                // 
                this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
                this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
                this.ClientSize = new System.Drawing.Size(557, 266);
                this.Controls.Add(this.dataGridView1);
                this.Name = "Form1";
                this.Text = "Form1";
                this.Load += new System.EventHandler(this.Form1_Load);
                ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).EndInit();
                this.ResumeLayout(false);        }        #endregion        private System.Windows.Forms.DataGridView dataGridView1;
        }
    }
      

  3.   

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using System.Data;
    namespace ArrayMarge
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }                 private void Form1_Load(object sender, EventArgs e)
            {
                DataTable dt=new DataTable();            dt.Columns.Add("c1");
                dt.Columns.Add("c2");            DataRow dr;            dr = dt.NewRow();
                dr[0]="r11";
                dr[1]="r12";
                dt.Rows.Add(dr);
                dr = dt.NewRow();
                dr[0] = "r21";
                dr[1] = "r22";
                dt.Rows.Add(dr);            dr = dt.NewRow();
                dr[0] = "r31";
                dr[1] = "r32";
                dt.Rows.Add(dr);            dr = dt.NewRow();
                dr[0] = "r41";
                dr[1] = "r42";
                dt.Rows.Add(dr);                        this.dataGridView1.DataSource = dt;
            
            }        private void ChargeRow(int indexFrom, int indexTo)
            {
                if (indexFrom == indexTo)
                {
                    return;
                }            DataTable dt;
                dt=(DataTable)this.dataGridView1.DataSource ; 
                if (indexFrom>=0 && indexFrom<dt.Rows.Count )
                {
                    if (indexTo >= 0 && indexTo < dt.Rows.Count)
                    {
                        DataRow dr = dt.NewRow();                    CopyRow(dt.Rows[indexFrom] , dr  );
                        CopyRow(dt.Rows[indexTo] , dt.Rows[indexFrom]  );
                        CopyRow(dr , dt.Rows[indexTo] );
                        dr = null;
                    }
                }             
                 
            }
            private void CopyRow(DataRow drFrom,DataRow drTo)
            {
                int i = 0;
                foreach (object var in drFrom.ItemArray )
                {
                    drTo[i++] = var;
                    
                }
            }
                  private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
            {        }        private void dataGridView1_MouseMove(object sender, MouseEventArgs e)
            {
                if (e.Button == MouseButtons.Left  )
                {
                     
     
                }
            }        private void dataGridView1_CellMouseMove(object sender, DataGridViewCellMouseEventArgs e)
            {
                if (e.Button == MouseButtons.Left)
                {
                    this.ChargeRow(this.dataGridView1.SelectedRows[0].Index, e.RowIndex);
                }
            }        private void dataGridView1_CellMouseUp(object sender, DataGridViewCellMouseEventArgs e)
            {
                this.ChargeRow(this.dataGridView1.SelectedRows[0].Index, e.RowIndex);
                this.dataGridView1.Rows[e.RowIndex].Selected = true;
            }        private void dataGridView1_CellMouseMove_1(object sender, DataGridViewCellMouseEventArgs e)
            {
                if (e.Button == MouseButtons.Left )
                {
                    
                    if (e.RowIndex > this.dataGridView1.SelectedRows[0].Index)
                    {
                        this.dataGridView1.Cursor = Cursors.PanSouth;
                    }
                    else if (e.RowIndex < this.dataGridView1.SelectedRows[0].Index)
                    {
                        this.dataGridView1.Cursor = Cursors.PanNorth;
                    }
                    else
                    {
                        this.dataGridView1.Cursor = Cursors.Default;
                    }                
                }
                else
                {
                    this.dataGridView1.Cursor = Cursors.Default;
                }
            }        private void dataGridView1_CellMouseEnter(object sender, DataGridViewCellEventArgs e)
            {
                
               
            }
        }
    }
      

  4.   

    首先在DragEnter标记你选中的行然后在DragDrop得到要拖放的位置,然后改变DataGridView的DataSource的行的位置后再绑定DataGridView
      

  5.   

    How can I move rows by dragging the row header cell?
    http://www.syncfusion.com/FAQ/WindowsForms/FAQ_c44c.aspx#q832q这个也适合datagridview。