C# 两个dataGridView 间行拖动例子

发布时间:2009年07月29日      浏览次数:678 次
研究了整整一天,在网上找到了些类似的代码,但是都没有完整的例子。所以以下代码都是自己写的,也许很多地方不够优化,完美,不过确实实现了两个表结构相同的datagridview间的整行拖动。
也许这功能很多都人都用不上,但是要用的时候,网上是没有源码的,自己做了一个。感谢网上的朋友给了我思路。有疑问的请联系~~
注意里面所有的事件都是两个datagridview都需要挂上~才能实现拖动,转载请注明
欢迎更多热爱.net 的朋友一起探讨研究。
源码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace WindowsApplication1
{
public partial class Form3 : Form
{
public Form3()
{
InitializeComponent();
}
private int selectionIdx = 0;
private void Form3_Load(object sender, EventArgs e)
{
DataSet ds = new DataSet();
ds.Tables.Add("table1");
ds.Tables[0].Columns.Add("code");
ds.Tables[0].Columns.Add("desc");
ds.Tables[0].Rows.Add(new object[] { "1", "aaa" });
ds.Tables[0].Rows.Add(new object[] { "2", "bbb" });
DataSet ds2 = new DataSet();
ds2.Tables.Add("table1");
ds2.Tables[0].Columns.Add("code");
ds2.Tables[0].Columns.Add("desc");
ds2.Tables[0].Rows.Add(new object[] { "3", "ccc" });
ds2.Tables[0].Rows.Add(new object[] { "4", "ddd" });
dataGridView1.DataSource = ds.Tables[0];
dataGridView2.DataSource = ds2.Tables[0];
}
DataRow dr = null;
DataGridViewRow dgvr = null;
private void dataGridView1_DragDrop(object sender, DragEventArgs e)
{
try
{
if (dr != null)
{
DataGridView dgv = sender as DataGridView;
string strTemp = dgv.Name;
int idx = GetRowFromPoint(e.X, e.Y, dgv);
if (idx < 0)
idx = dgv.Rows.Count;
if (e.Data.GetDataPresent(typeof(DataGridViewRow)))
{
((DataTable)dgv.DataSource).Rows.InsertAt(dr, idx);
selectionIdx = idx;
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void dataGridView1_DragOver(object sender, DragEventArgs e)
{
try
{
DataGridView dgv = sender as DataGridView;
string strTemp = dgv.Name;
dgvr = (DataGridViewRow)e.Data.GetData(typeof(DataGridViewRow));
dr = ((DataTable)dgv.DataSource).NewRow();
for (int i = 0; i < dgvr.Cells.Count; i++)
{
dr[i] = dgvr.Cells[i].Value;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void dataGridView1_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
{
try
{
if (e.RowIndex > 0)
selectionIdx = e.RowIndex;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private int GetRowFromPoint(int x, int y,DataGridView dgv)
{
for (int i = 0; i < dgv.RowCount; i++)
{
Rectangle rec = dgv.GetRowDisplayRectangle(i, false);
if (dgv.RectangleToScreen(rec).Contains(x, y))
return i;
}
return -1;
}
private void dataGridView1_SelectionChanged(object sender, EventArgs e)
{
try
{
DataGridView dgv = sender as DataGridView;
if ((dgv.Rows.Count > 0) && (dgv.SelectedRows.Count > 0))
{
if (dgv.Rows.Count <= selectionIdx)
selectionIdx = dgv.Rows.Count - 1;
dgv.Rows[selectionIdx].Selected = true;
dgv.CurrentCell = dgv.Rows[selectionIdx].Cells[0];
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void dataGridView1_DragEnter(object sender, DragEventArgs e)
{
e.Effect = DragDropEffects.Move;
}
private void dataGridView1_CellMouseMove(object sender, DataGridViewCellMouseEventArgs e)
{
try
{
DataGridView dgv = sender as DataGridView;
string strTemp = dgv.Name;
if ((e.Clicks < 2) && (e.Button == MouseButtons.Left))
{
if ((e.ColumnIndex == -1) && (e.RowIndex > -1))
{
DragDropEffects dd = dgv.DoDragDrop(dgv.Rows[e.RowIndex], DragDropEffects.Move);
if (dd == DragDropEffects.Move && dgvr != null)
{
dgv.Rows.Remove(dgvr);
dgvr = null;
dr = null;
}
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
}
搞定了~~,呵呵,如果对你有帮助,记得踩踩,我才有信心做更多的技术性研究~~
免责声明:本站相关技术文章信息部分来自网络,目的主要是传播更多信息,如果您认为本站的某些信息侵犯了您的版权,请与我们联系,我们会即时妥善的处理,谢谢合作!