WinForms版True DBGrid教程 > 教程13:实现拖拽下拉功能 |
在此教程中,你将在True DBGrid for WinForms中学习到如何实现drag-and-drop功能性.
为WinForms 控件安装True DBGrid
完成下面的步骤:
To write code in Visual Basic
Visual Basic |
拷贝代码
|
---|---|
Dim _ptStartDrag As Point Dim _dragRow As Long |
To write code in C#
C# |
拷贝代码
|
---|---|
Point _ptStartDrag; long _dragRow; |
To write code in Visual Basic
Visual Basic |
拷贝代码
|
---|---|
Me.CallListTableAdapter.Fill(Me.DsCallList.CallList) Me.CustomersTableAdapter.Fill(Me.DsCustomers.Customers) |
To write code in C#
C# |
拷贝代码
|
---|---|
this.CallListTableAdapter.Fill(this.DsCallList.CallList); this.CustomersTableAdapter.Fill(this.DsCustomers.Customers); |
该章节描述了代码需要从C1TrueDBGrid1到C1TrueDBGrid2拖动单元格或者行的内容。此代码设想,如果你想拖动数据的全部行到C1TrueDBGrid2并为了添加新的纪录。
To write code in Visual Basic
Visual Basic |
拷贝代码
|
---|---|
Private Sub ResetDragDrop() ' Turn off drag-and-drop by resetting the highlight and label text. Me._ptStartDrag = Point.Empty Me._dragRow = - 1 Me.C1TrueDBGrid1.MarqueeStyle = C1.Win.C1TrueDBGrid.MarqueeEnum.SolidCellBorder Me.C1TrueDBGrid2.MarqueeStyle = C1.Win.C1TrueDBGrid.MarqueeEnum.SolidCellBorder Me.Label3.Text = "Drag a row from the top grid and drop it on the bottom grid." End Sub |
To write code in C#
C# |
拷贝代码
|
---|---|
private void ResetDragDrop() { // Turn off drag-and-drop by resetting the highlight and label text. this._ptStartDrag = Point.Empty; this._dragRow = - 1; this.c1TrueDBGrid1.MarqueeStyle = C1.Win.C1TrueDBGrid.MarqueeEnum.SolidCellBorder; this.c1TrueDBGrid2.MarqueeStyle = C1.Win.C1TrueDBGrid.MarqueeEnum.SolidCellBorder; this.label3.Text = "Drag a row from the top grid and drop it on the bottom grid."; } |
To write code in Visual Basic
Visual Basic |
拷贝代码
|
---|---|
Private Sub C1TrueDBGrid1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles C1TrueDBGrid1.MouseDown Dim row, col As Integer Me._ptStartDrag = Point.Empty Me._dragRow = - 1 If Me.C1TrueDBGrid1.CellContaining(e.X, e.Y, row, col) Then ' Save the starting point of the drag operation. Me._ptStartDrag = New Point(e.X, e.Y) Me._dragRow = row End If End Sub Private Sub C1TrueDBGrid1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles C1TrueDBGrid1.MouseMove ' If we don't have an empty start drag point, then the drag has been initiated. If Not Me._ptStartDrag.IsEmpty Then ' Create a rectangle that bounds the start of the drag operation by 2 pixels. Dim r As New Rectangle(Me._ptStartDrag, Size.Empty) r.Inflate(2, 2) ' If we've moved more than 2 pixels, start the drag operation. If Not r.Contains(e.X, e.Y) Then Me.C1TrueDBGrid1.Row = Me._dragRow Me.C1TrueDBGrid1.MarqueeStyle = C1.Win.C1TrueDBGrid.MarqueeEnum.HighlightRow Me.C1TrueDBGrid1.DoDragDrop(Me._dragRow, DragDropEffects.Copy) End If End If End Sub |
To write code in C#
C# |
拷贝代码
|
---|---|
private void C1TrueDBGrid1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e) { int row, col; this._ptStartDrag = Point.Empty; this._dragRow = - 1; if (this.c1TrueDBGrid1.CellContaining(e.X, e.Y, row, col) ) { // Save the starting point of the drag operation. this._ptStartDrag = new Point(e.X, e.Y); this._dragRow = row; } } private void C1TrueDBGrid1_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e) { // If we don't have an empty start drag point, then the drag has been initiated. if (!this._ptStartDrag.IsEmpty ) { // Create a rectangle that bounds the start of the drag operation by 2 pixels. Rectangle r = new Rectangle(this._ptStartDrag, Size.Empty); r.Inflate(2, 2); // If we've moved more than 2 pixels, start the drag operation. if (!r.Contains(e.X, e.Y) ) { this.c1TrueDBGrid1.Row = this._dragRow; this.c1TrueDBGrid1.MarqueeStyle = C1.Win.C1TrueDBGrid.MarqueeEnum.HighlightRow; this.c1TrueDBGrid1.DoDragDrop(this._dragRow, DragDropEffects.Copy); } } } |
To write code in Visual Basic
Visual Basic |
拷贝代码
|
---|---|
Private Sub C1TrueDBGrid2_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles C1TrueDBGrid2.DragEnter Me.Label3.Text = "Create a new record when dropped..." e.Effect = DragDropEffects.Copy End Sub Private Sub C1TrueDBGrid2_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles C1TrueDBGrid2.DragDrop Try Dim row As Integer = CInt(e.Data.GetData(GetType(Integer))) ' Use the grid's indexer to get some data. Dim custname As String = Me.C1TrueDBGrid1(row, "FirstName").ToString() ' Use the CellText() method to get some data. custname += " " + Me.C1TrueDBGrid1.Columns("LastName").CellText(row) ' Use the CellValue() method to get some data. custname += " " + Me.C1TrueDBGrid1.Columns("Company").CellValue(row).ToString() ' Add a new row to the data set for the bottom grid. Dim drv As DataRowView = Me.DsCallList.CallList.DefaultView.AddNew() drv("CallDate") = DateTime.Now drv("Customer") = custname drv("Phone") = Me.C1TrueDBGrid1.Columns("Phone").Value.ToString() drv.EndEdit() Me.C1TrueDBGrid2.MoveLast() Me.C1TrueDBGrid2.Select() ' Commit changes to the database. Dim inserted As DataSet = Me.DsCallList.GetChanges(DataRowState.Added) If Not (inserted Is Nothing) Then Me.CallListTableAdapter.Update(inserted) End If Me.ResetDragDrop() Catch ex As System.Exception MessageBox.Show(ex.Message) End Try End Sub |
To write code in C#
C# |
拷贝代码
|
---|---|
private void C1TrueDBGrid2_DragEnter(object sender, System.Windows.Forms.DragEventArgs e) { this.label3.Text = "Create a new record when dropped..."; e.Effect = DragDropEffects.Copy; } private void C1TrueDBGrid2_DragDrop(object sender, System.Windows.Forms.DragEventArgs e) { try { int row = (int)e.Data.GetData(typeof(int)); // Use the grid's indexer to get some data. string custname = this.c1TrueDBGrid1[row, "FirstName"].ToString(); // Use the CellText() method to get some data. custname += " " + this.c1TrueDBGrid1.Columns["LastName"].CellText(row); // Use the CellValue() method to get some data. custname += " " + this.c1TrueDBGrid1.Columns["Company"].CellValue(row).ToString(); // Add a new row to the data set for the bottom grid. DataRowView drv = this.DsCallList.CallList.DefaultView.AddNew(); drv["CallDate"] = DateTime.Now; drv["Customer"] = custname; drv["Phone"] = this.c1TrueDBGrid1.Columns["Phone"].Value.ToString(); drv.EndEdit(); this.c1TrueDBGrid2.MoveLast(); this.c1TrueDBGrid2.Select(); // Commit changes to the database. DataSet inserted = this.DsCallList.GetChanges(DataRowState.Added); if (! (inserted == null) ) { this.CallListTableAdapter.Update(inserted); } this.ResetDragDrop(); } catch (System.Exception ex) { MessageBox.Show(ex.Message); } } |
你已经成功地完成了在C1TrueDBGrid中实现拖拽和下拉;总结教程13.