在datagridview中拖动选项 [英] drag option in datagridview

查看:47
本文介绍了在datagridview中拖动选项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

开始时间

结束时间

加载时间

备注

10.00

10.15

0.06

production

production

11.00

11.15

0.06

production

production

12.15

12.30

0.06

production

production

我想拖动加载时间并记下下一行并连续

i want to drag the loading time and remarks next rows and continuously

推荐答案

< style type =" text / css"> @page {margin:2cm} p {margin-bottom:0.25cm; line-height:120%} a:链接{so-language:zxx}< / style>

您可以尝试以下步骤获取结果。 

<style type="text/css">@page { margin: 2cm } p { margin-bottom: 0.25cm; line-height: 120% } a:link { so-language: zxx } </style>

You can try this steps to get results. 

步骤1
创建一个新的Windows窗体应用程序并在窗体上添加一个DataGridView和一个ListBox控件。



步骤2

写下在表单load事件中跟随以填充DataGridView中的数据.AllowUserToAddRows设置为false以禁用添加行的选项,因为它不是必需的。在DataGridView的最后一行的行标题中显示星号(*)if
默认情况下设置为true:

Step 1
Create a new Windows Forms Application and add a DataGridView and a ListBox control on the form.

Step 2
Write the following in the form load event to populate data in the DataGridView. AllowUserToAddRows is set to false to disable the option to add rows as it is not required. An asterisk (*) is displayed in the row header of the last row of the DataGridView if it is by default set to true:

private void Form3_Load(object sender,EventArgs e)

private void Form3_Load(object sender, EventArgs e)

{

string ConString = @" Data Source = DEEPAK\SQLSERVER2005; Initial Catalog = Employees; User ID = sa; Password = ******" ;;

string ConString = @"Data Source=DEEPAK\SQLSERVER2005;Initial Catalog=Employees;User ID=sa;Password=******";

SqlConnection con = new SqlConnection(ConString);

SqlConnection con = new SqlConnection(ConString);

using(SqlDataAdapter sda = new SqlDataAdapter(" SELECT DepartmentID,Name FROM Departments" con))

using (SqlDataAdapter sda = new SqlDataAdapter("SELECT DepartmentID, Name FROM Departments", con))

{

DataSet ds = new DataSet();

DataSet ds = new DataSet();

sda.Fill(ds);

sda.Fill(ds);

if(ds.Tables.Count> 0)

if (ds.Tables.Count > 0)

{

dataGridView1.DataSource = ds.Tables [0] .DefaultView;

dataGridView1.DataSource = ds.Tables[0].DefaultView;

}

}

dataGridView1.AllowUserToAddRows = false;

dataGridView1.AllowUserToAddRows = false;

}

步骤3
$


将ListBox的AllowDrop属性设置为true,如下所示:
$


listBox1 .AllowDrop = true;
$


AllowDrop属性用于启用或禁用用户将数据拖放到此控件上。



步骤4

Step 3

Set the AllowDrop property of the ListBox to true, as in:

listBox1.AllowDrop = true;

The AllowDrop property is used to enable or disable users to drop data onto this control.

Step 4



  • 在DataGridView的MouseDown事件中写下以下内容:

  • Write the following in the MouseDown event of the DataGridView:

private void dataGridView1_MouseDown(object sender,MouseEventArgs e)

{

dataGridView1.DoDragDrop(dataGridView1.SelectedRows, DragDropEffects.Move);
$
}



控件的DoDragDrop方法用于启动拖放操作。我们从DataGridView的MouseDown事件中调用它。第一个参数是我们想要在拖放操作中发送的数据。这里我们发送DataGridView的选定行。
第二个参数是DragDropEffects枚举,它提供拖放操作效果。在执行拖放操作时,光标样式会相应更改。可能的值包括DragDropEffects.All,DragDropEffects.Copy,DragDropEffects.Link,
DragDropEffects.Move,DragDropEffects.None和DragDropEffects.Scroll。

private void dataGridView1_MouseDown(object sender, MouseEventArgs e)
{
dataGridView1.DoDragDrop(dataGridView1.SelectedRows, DragDropEffects.Move);
}

The DoDragDrop method of a control is used to start a drag and drop operation. We call it from MouseDown event of the DataGridView. The first parameter is the data that we want to send in drag and drop operation. Here we are sending selected rows of the DataGridView. The second parameter is a DragDropEffects enumeration that provides the drag and drop operation effect. The cursor style changes accordingly while the drag and drop is being performed. Possible values are DragDropEffects.All, DragDropEffects.Copy, DragDropEffects.Link, DragDropEffects.Move, DragDropEffects.None and DragDropEffects.Scroll.



  • 在ListBox的DragEnter事件中写下以下内容:

  • Write the following in the DragEnter event of the ListBox:

private void listBox1_DragEnter(object sender,DragEventArgs e)

{

if(e.Data.GetDataPresent(typeof(DataGridViewSelectedRowCollection)))

{

e。 Effect = DragDropEffects.Move;

}

}



拖动对象时触发DragEnter事件进入控制区。 DragEventArgs.Data.GetDataPresent()方法用于检查拖动的数据是否是DataGridViewSelectedRowCollection的类型。然后使用DragEventArgs.Effect
属性设置拖动效果。它被设置为DragDropEffects.Move以将拖动的操作显示为move。

private void listBox1_DragEnter(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(typeof(DataGridViewSelectedRowCollection)))
{
e.Effect = DragDropEffects.Move;
}
}

The DragEnter event is fired when an object is dragged into the control's area. The DragEventArgs.Data.GetDataPresent() method is used to check if the dragged data is a type of DataGridViewSelectedRowCollection. Then the drag effect is set using the DragEventArgs.Effect property. It is set to DragDropEffects.Move to show the dragged operation as move.



  • 在ListBox的DragDrop事件中写下以下内容:

  • Write the following in the DragDrop event of the ListBox:

private void listBox1_DragDrop(object sender,DragEventArgs e)

{

DataGridViewSelectedRowCollection rows =(DataGridViewSelectedRowCollection)e.Data.GetData(typeof(DataGridViewSelectedRowCollection));
$


foreach(行中的DataGridViewRow行)

{

listBox1.Items.Add(row.Cells [1] .Value);

dataGridView1.Rows.Remove(row);

}
}
$


拖放操作完成后会触发DragDrop事件。这里,使用DragEventArgs.Data.GetData()方法将DataGridView的选定行返回到DataGridViewSelectedRowCollection变量中。然后使用foreach循环使用ListBox.Items.Add()方法将此集合的行
添加到ListBox的项目。最后,使用DataGridView.Rows.Remove()方法从DataGridView中删除该行。

private void listBox1_DragDrop(object sender, DragEventArgs e)
{
DataGridViewSelectedRowCollection rows = (DataGridViewSelectedRowCollection)e.Data.GetData(typeof(DataGridViewSelectedRowCollection));

foreach (DataGridViewRow row in rows)
{
listBox1.Items.Add(row.Cells[1].Value);
dataGridView1.Rows.Remove(row);
}
}

The DragDrop event is fired when a drag and drop operation is finished. Here, selected rows of the DataGridView is returned into a DataGridViewSelectedRowCollection variable using the DragEventArgs.Data.GetData() method. Then a foreach loop is used to add rows of this collection to items of the ListBox using the ListBox.Items.Add() method. Finally that row is removed from the DataGridView using the DataGridView.Rows.Remove() method.


这篇关于在datagridview中拖动选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆