如何实现DataGridView中的行的拖动? 想用拖动的方式,改变DataGridView中的行的位置,要如何实现呢? 还请高手赐教!! 先说谢谢啦!!! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 to wshuangminlg(asp.net 2.0 群:31412938(群主)):就是交换行的顺序啊,怎么不可以呢? 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; }} 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) { } }} 首先在DragEnter标记你选中的行然后在DragDrop得到要拖放的位置,然后改变DataGridView的DataSource的行的位置后再绑定DataGridView How can I move rows by dragging the row header cell?http://www.syncfusion.com/FAQ/WindowsForms/FAQ_c44c.aspx#q832q这个也适合datagridview。 vs断点调试网站项目出问题了, 求助. 如何制作模板控件? 已知一个解密算法,如何写成加密算法, 自定义控件的数据绑定 求多元线性回归算法源代码(C#/C++) c# 调用GBK编码的WebService发送数据给服务器时,对方收到数据为乱码,但我收到的返回则没有乱码 怎么写判断用于确定或取消的消息框? 一个很菜的问题,我装的 framework sdk 怎么没有csc.exe文件? c#中如何将一个图象文件与文本合并并存成另一个图象文件 请问!FOR循环不能中断和continue了。 ooReport网上平台(网络直报)统计商业智能报表分析系统 设计模式Builder生成器学习记录
{
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;
}
}
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)
{
}
}
}
http://www.syncfusion.com/FAQ/WindowsForms/FAQ_c44c.aspx#q832q这个也适合datagridview。