C#vs的windows窗体的datagridview之间数据的拖拽
效果
代码
1.新建对象:
private DataGridViewSelectedRowCollection sourceRowCollection = null;
2.在dataGridView1(上表:药品库存单)的按下鼠标事件(dataGridView1_MouseDown)中编辑:
private void dataGridView1_MouseDown(object sender, MouseEventArgs e)
{
//捕获鼠标点击区域的信息
DataGridView.HitTestInfo hitTestInfo = this.dataGridView1.HitTest(e.X, e.Y);
if (e.X < 30 && hitTestInfo.RowIndex > -1)
{
if (this.dataGridView1.SelectedRows.Count > 0)
{
sourceRowCollection = this.dataGridView1.SelectedRows;
}
}
else
sourceRowCollection = null;
}
3.在dataGridView1(上表:药品库存单)的鼠标移动事件(dataGridView1_MouseMove)中编辑:
private void dataGridView1_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
if (sourceRowCollection != null)
{
DragDropEffects effect = this.dataGridView1.DoDragDrop(sourceRowCollection, DragDropEffects.Move);
if (effect == DragDropEffects.Move)
{
//将sourceRowCollection重新置空
sourceRowCollection = null;
}
}
}
}
4.在dataGridView2(下表:处方单)的将对象拖过控件事件(dataGridView2_DragOver)中编辑:
private void dataGridView2_DragOver(object sender, DragEventArgs e)
{
if (!e.Data.GetDataPresent(typeof(DataGridViewSelectedRowCollection)))
{
e.Effect = DragDropEffects.None;
return;
}
else
{
e.Effect = DragDropEffects.Move; //这个值会返回给DoDragDrop方法
}
}
5.在dataGridView2(下表:处方单)的拖放操作完成事件(dataGridView2_DragDrop)中编辑:
private void dataGridView2_DragDrop(object sender, DragEventArgs e)
{
try
{
if (e.Data.GetDataPresent(typeof(DataGridViewSelectedRowCollection)))
{
DataGridViewSelectedRowCollection rowCollection = e.Data.GetData(typeof(DataGridViewSelectedRowCollection)) as DataGridViewSelectedRowCollection;
if (rowCollection == null)
{
return;
}
//新增行
//注意要将鼠标的Point转换到当前工作区域,否则无法得到正确的HitTestInfo
System.Drawing.Point p = this.dataGridView2.PointToClient(new System.Drawing.Point(e.X, e.Y));
DataGridView.HitTestInfo hitTestInfo = this.dataGridView2.HitTest(p.X, p.Y);
//如果鼠标所在的位置的RowIndex>-1,则在当前位置接入列,否则就在最末尾新增列
if (hitTestInfo.RowIndex > -1)
{
this.dataGridView2.Rows.Insert(hitTestInfo.RowIndex + 1, rowCollection.Count);
for (int i = 0; i < rowCollection.Count; i++)
{
this.dataGridView2.Rows[hitTestInfo.RowIndex + i + 1].Cells[0].Value = rowCollection[i].Cells[2].Value;//药品
this.dataGridView2.Rows[hitTestInfo.RowIndex + i + 1].Cells[1].Value = rowCollection[i].Cells[3].Value;//剂型
this.dataGridView2.Rows[hitTestInfo.RowIndex + i + 1].Cells[2].Value = rowCollection[i].Cells[4].Value;//规格
this.dataGridView2.Rows[hitTestInfo.RowIndex + i + 1].Cells[3].Value = rowCollection[i].Cells[5].Value;//单位
this.dataGridView2.Rows[hitTestInfo.RowIndex + i + 1].Cells[4].Value = rowCollection[i].Cells[6].Value;//产地
this.dataGridView2.Rows[hitTestInfo.RowIndex + i + 1].Cells[5].Value = rowCollection[i].Cells[7].Value;//单价
this.dataGridView2.Rows[hitTestInfo.RowIndex + i + 1].Cells[6].Value = rowCollection[i].Cells[9].Value;//有效日期
this.dataGridView2.Rows[hitTestInfo.RowIndex + i + 1].Cells[7].Value = 0;//数量
this.dataGridView2.Rows[hitTestInfo.RowIndex + i + 1].Cells[8].Value = 0;//金额
double text7 = 0;
textBox7.Text = text7.ToString();//这个textBox7是“总计”,拖入药品输入数量,总计会相应变化
}
}
else
{
foreach (DataGridViewRow row in rowCollection)
{
int i = this.dataGridView2.Rows.Add();
this.dataGridView2.Rows[i].Cells[0].Value = row.Cells[2].Value;
this.dataGridView2.Rows[i].Cells[1].Value = row.Cells[3].Value;
this.dataGridView2.Rows[i].Cells[2].Value = row.Cells[4].Value;
this.dataGridView2.Rows[i].Cells[3].Value = row.Cells[5].Value;
this.dataGridView2.Rows[i].Cells[4].Value = row.Cells[6].Value;
this.dataGridView2.Rows[i].Cells[5].Value = row.Cells[7].Value;
this.dataGridView2.Rows[i].Cells[6].Value = row.Cells[9].Value;
this.dataGridView2.Rows[hitTestInfo.RowIndex + i + 1].Cells[7].Value = 0;
this.dataGridView2.Rows[hitTestInfo.RowIndex + i + 1].Cells[8].Value = 0;
double text7 = 0;
textBox7.Text = text7.ToString();//这个textBox7是“总计”,拖入药品输入数量,总计会相应变化
}
}
}
}
catch (Exception ex)
{
throw (ex);
}
}
6.完成。