如何在Datagridview中应用拖放单元格内容 [英] How to apply drag drop cell content in datagridview
本文介绍了如何在Datagridview中应用拖放单元格内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想将内容从一个单元格拖动到C#.net的同一datagridview中的另一个单元格中
有帮助吗?
提前感谢
I want to drag contents from one cell to another cell within the same datagridview in C#.net
Any Help??
Thanx in advance
推荐答案
在codeproject和google上有几篇文章解决了这个问题 ^ ]
特别是对于datagridview……并没有太大的不同……这段代码可能对您有帮助
there are a couple of articles on codeproject and google that address this issueDrag and Drop between list boxes - Beginner''s Tutorial[^]
and particularly for datagridview ... its not that much different... this code might help you
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
//allow drop
this.dataGridView1.AllowDrop = true;
//data grid view sample data
DataTable dtb = new DataTable();
dtb.Columns.AddRange(new DataColumn[] { new DataColumn(), new DataColumn(), new DataColumn() });
for (int i = 0; i < 30;)
{
dtb.Rows.Add((++i).ToString(), (++i).ToString(), (++i).ToString());
}
dataGridView1.DataSource = dtb;
}
private void dataGridView1_CellMouseMove(object sender, DataGridViewCellMouseEventArgs e)
{
if (e.Button == System.Windows.Forms.MouseButtons.Left)
{
dataGridView1.DoDragDrop(dataGridView1[e.ColumnIndex,e.RowIndex].FormattedValue, DragDropEffects.Copy);
}
}
private void dataGridView1_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= dataGridView1.HitTest(cursorLocation.X,cursorLocation.Y);
if (hittest.ColumnIndex != -1
&& hittest.RowIndex != -1)
dataGridView1[hittest.ColumnIndex, hittest.RowIndex].Value = cellvalue;
}
private void dataGridView1_DragOver(object sender, DragEventArgs e)
{
e.Effect = DragDropEffects.Copy;
}
}
祝你好运
good luck
这篇关于如何在Datagridview中应用拖放单元格内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文