Udostępnij za pośrednictwem


DataGrid Control for Visual Basic 6.0 Users

The DataGrid control in Visual Basic 6.0 is replaced by the Windows Forms DataGridView control in Visual Basic 2008. The names of some properties, methods, events, and constants are different, and in some cases there are differences in behavior.

Conceptual Differences

Data Binding

The Visual Basic 2008 DataGridView control does not need data-specific methods or events because all actions are performed through the data source. Because of this separation of presentation and data functionality, the data source can be changed with or without user interface input. Also, multiple controls bound to the same data source will always stay in sync.

Properties for viewing and navigation in the DataGrid control (such as TabAction, EnterAction, AllowArrows, WrapCellPointer, and Scrollable) are no longer needed. For example, the grid functions as though the Scrollable property is set to True: If more data exists than can be displayed, a scroll bar appears automatically. Excel-style navigation through the grid is the default, allowing the user to move forward with the TAB key and backward with the SHIFT+TAB key combination. For more information, see Default Keyboard and Mouse Handling in the Windows Forms DataGridView Control.

Caption Property

In Visual Basic 6.0, the Caption property is used to display a title bar above the grid; if the Caption property is left empty, no title bar is displayed.

The Visual Basic 2008 DataGridView control does not support a title bar. However, you can achieve the same effect using a Label control.

Data Formatting

In Visual Basic 6.0, formatting data in the DataGrid control is handled using the DataFormat property and a StdDataFormat object. Formatting is applied on a column-by-column basis.

In the Visual Basic 2008 DataGridView control, formatting is accomplished using the Format property of a DataGridViewCellStyle object. Formatting can be applied to individual cells, columns, or rows. For more information, see Data Formatting in the Windows Forms DataGridView Control.

hWndEditor Property

In Visual Basic 6.0, the hWndEditor property is used to pass the window handle assigned to a DataGrid control's editing window to a Windows API call.

The Visual Basic 2008 DataGridView control does not have a separate window handle when in edit mode; instead, use the Handle property of the DataGridView control or any embedded edit controls.

MarqueeStyle Property

In Visual Basic 6.0, the MarqueeStyle property controls the appearance of a selected cell or row by changing the border style, inverting the foreground and background colors, or invoking an edit window.

There is no direct equivalent for the Visual Basic 2008 DataGridView control. However, you can achieve the same effect through a combination of the SelectionMode, CellBorderStyle, and Format properties. For more information, see Selection Modes in the Windows Forms DataGridView Control.

SelLength, SelStart, SelText Properties

In the Visual Basic 6.0 DataGrid control, when a cell enters edit mode, the SelLength, SelStart, and SelText properties can be used to set the initial position of the caret or to highlight a portion of the text in the cell.

In the Visual Basic 2008 DataGridView control, these properties no longer exist. The cells in the DataGridView control are based on the TextBox control; by adding code to the EditingControlShowing event handler you can access the SelectionLength, SelectionStart, and SelectedText properties of the underlying control.

Split Views

The Visual Basic 6.0 DataGrid control supports a split view, allowing the user to display the same data side-by-side. The Split object and the Split, Splits, and TabAcrossSplits properties control the ability to display a split view.

In the Visual Basic 2008 DataGridView control, these properties no longer exist. However, you can achieve the same effect using one or more SplitContainer controls and multiple DataGridView controls. To duplicate the functionality of the TabAcrossSplits property, you can use the StandardTab property of the DataGridView control.

Code Changes for the DataGrid Control

The following code demonstrates the differences between Visual Basic 6.0 and Visual Basic 2008 by showing how, in each version, to highlight the text in a cell when a user selects the cell in a DataGridView control.

' Visual Basic 6.0
Private Sub DataGrid1_Click()
    DataGrid1.SelStart = 1
    DataGrid1.SelLength = DataGrid1.Text
    MsgBox("The selected text is " & DataGrid1.SelText)
End Sub
' Visual BasicPrivateSub DataGridView1_EditingControlShowing( _
ByVal sender AsObject, ByVal e As System.Windows.Forms. _
DataGridViewEditingControlShowingEventArgs) _
Handles DataGridView1.EditingControlShowing
    CType(e.Control, TextBox).SelectionStart = 0
    CType(e.Control, TextBox).SelectionLength = Len(CType(e.Control, _
      TextBox).Text)
    MsgBox("The selected text is " & CType(e.Control, _
      TextBox).SelectedText)
EndSub

DataGrid Control Property, Method, and Event Equivalencies

The following tables list Visual Basic 6.0 properties, methods, and events, along with their Visual Basic 2008 equivalents. Those properties, methods, and events that have the same name and behavior are not listed. All Visual Basic 2008 enumerations map to the System.Windows.Forms namespace unless otherwise noted.

This table provides links to topics explaining behavior differences. Where there is no direct equivalent in Visual Basic 2008, links are provided to topics that present alternatives.

Properties

Visual Basic 6.0

Visual Basic 2005 Equivalent

AddNewMode

New implementation. Data operations are handled in the data source. For more information, see Displaying Data in the Windows Forms DataGridView Control.

Align

Dock and Anchor

AllowAddNew

AllowNew (BindingSource)

AllowArrows

No longer an adjustable property; arrow navigation always allowed. For more information, see Default Keyboard and Mouse Handling in the Windows Forms DataGridView Control.

AllowDelete

AllowRemove (BindingSource)

AllowRowSizing

AllowUserToResizeRows

AllowUpdate

AllowEdit (BindingSource)

Appearance

New implementation. For more information, see Appearance and BorderStyle Properties for Visual Basic 6.0 Users.

ApproxCount

New implementation. Data operations are handled in the data source. For more information, see Displaying Data in the Windows Forms DataGridView Control.

BackColor

BackgroundColor

NoteNote:
Colors are handled differently in Visual Basic 2008. For more information, see Color Behavior for Visual Basic 6.0 Users.

Bookmark

New implementation. You can now directly access any item.

Caption

New implementation. Use a Label control to simulate a title bar.

Col

SelectedColumns

ColumnHeaders

ColumnHeadersVisible

Container

Parent; inherited from Control.

CurrentCellModified

IsCurrentCellDirty, IsCurrentCellInEditMode

CurrentCellVisible

CurrentCell

NoteNote:
If the CurrentCell property is set to a cell that is not visible, the grid scrolls to show that cell (similar to setting CurrentCellVisible = True).

DataChanged

IsCurrentCellDirty, IsCurrentRowDirty

DataFormats

DataGridViewCellStyle.object.

NoteNote:
Formatting can be set for an individual cell, column, or row.

DefColWidth

WidthDataGridViewColumn object.

DragIcon

DragMode

New implementation. For more information, see Drag and Drop for Visual Basic 6.0 Users.

EditActive

IsCurrentCellInEditMode

FirstRow

FirstDisplayedScrollingRowIndex

Font

FontBold

FontItalic

FontName

FontSize

FontStrikethrough

FontUnderline

Font

NoteNote:
Fonts are handled differently in Visual Basic 2008. For more information, see Font Handling for Visual Basic 6.0 Users.

ForeColor

ForeColor

NoteNote:
Colors are handled differently in Visual Basic 2008. For more information, see Color Behavior for Visual Basic 6.0 Users.

HeadFont

DataGridViewCellStyle object.

NoteNote:
Fonts are handled differently in Visual Basic 2008. For more information, see Font Object for Visual Basic 6.0 Users.

HeadLines

No direct equivalent. Use the WrapMode property of the DataGridViewCellStyle object in combination with ColumnHeadersHeight.

Height

Height, inherited from Control class.

NoteNote:
Coordinates are handled differently in Visual Basic 2008. For more information, see Coordinate System for Visual Basic 6.0 Users.

HelpContextID

New implementation. For more information, see Help Support for Visual Basic 6.0 Users.

hWnd

Handle

hWndEditor

New implementation. Use Handle.

Index

New implementation. For more information, see Control Arrays for Visual Basic 6.0 Users.

Left

Left, inherited from Control class.

NoteNote:
Coordinates are handled differently in Visual Basic 2008. For more information, see Coordinate System for Visual Basic 6.0 Users.

LeftCol

FirstDisplayedScrollingColumnIndex

MarqueeStyle

No direct equivalent. Use the SelectionMode, CellBorderStyle, and Format properties.

RecordSelectors

RowHeadersVisible

Row

SelectedRows

RowDividerStyle

GridColor, CellBorderStyle, RowHeadersBorderStyle, ColumnHeadersBorderStyle properties.

RowHeight

Height

SelBookmarks

New implementation. You can now directly access any item.

SelEndCol

SelStartCol

SelectedCells, SelectedColumns

SelLength

No direct equivalent. Use the SelectionLength property in the EditingControlShowing event handler.

SelStart

No direct equivalent. Use the SelectionStart property in the EditingControlShowing event handler.

SelText

No direct equivalent. Use the SelectedText property in the EditingControlShowing event handler.

Split

Splits

TabAcrossSplits

New implementation. Split views are not directly supported; use a SplitContainer control.

TabAction

StandardTab

Tag

New implementation.

Text

CurrentCell.Value

NoteNote:
The Value property returns an Object; use CStr or ToString to convert it to a String.

ToolTipText

ToolTip component

For more information, see ToolTip Support for Visual Basic 6.0 Users.

Top

Top

NoteNote:
Coordinates are handled differently in Visual Basic 2008. For more information, see Coordinate System for Visual Basic 6.0 Users.

VisibleCols

DisplayedColumnCount method.

NoteNote:
In Visual Basic 6.0, partially visible columns are always included in the count; the DisplayedColumnCount method uses a Boolean includePartialColumns parameter to determine whether partially visible columns are included.

VisibleRows

DisplayedRowCount method.

NoteNote:
In Visual Basic 6.0, partially visible rows are always included in the count; the DisplayedRowCount method uses a Boolean includePartialRows parameter to determine whether partially visible rows are included.

WhatsThisHelpID

New implementation. For more information, see Help Support for Visual Basic 6.0 Users.

Width

Width, inherited from Control class.

NoteNote:
Coordinates are handled differently in Visual Basic 2008. For more information, see Coordinate System for Visual Basic 6.0 Users.

WrapCellPointer

No longer an adjustable property; default behavior is WrapCellPointer = True.

Methods

Visual Basic 6.0

Visual Basic 2005 Equivalent

CaptureImage

New implementation. Capturing the contents of a DataGridView control to a PictureBox control is not supported.

ClearFields

New implementation. Column formatting is automatic when re-binding.

ClearSelCols

ClearSelection

ColContaining

IndexOf (DataGridViewColumnCollection)

Drag

New implementation. For more information, see Drag and Drop for Visual Basic 6.0 Users..

GetBookmark

New implementation. Bookmarks are no longer supported.

HoldFields

New implementation. Column formatting is preserved when rebinding.

Move

SetBounds, inherited from Control class.

NoteNote:
Coordinates are handled differently in Visual Basic 2008. For more information, seeCoordinate System for Visual Basic 6.0 Users.

Rebind

ResetBindings, inherited from Control class.

RowBookmark

New implementation. Bookmarks are no longer supported.

RowContaining

IndexOf (DataGridViewColumnCollection)

RowTop

GetContentBounds (DataGridViewCell)

Scroll

New implementation method. Use the CurrentCell property.

SetFocus

Focus

ShowWhatsThis

New implementation. For more information, see Help Support for Visual Basic 6.0 Users.

SplitContaining

New implementation. Split views are not directly supported; use a SplitContainer control.

ZOrder

BringToFront()or SendToBack()functions

Events

Visual Basic 6.0

Visual Basic 2005 Equivalent

AfterColEdit

CellEndEdit

AfterColUpdate

AfterDelete

New implementation. Data operations are handled in the data source. For more information, see Displaying Data in the Windows Forms DataGridView Control.

AfterUpdate

RowsAdded

BeforeColEdit

CellBeginEdit

BeforeColUpdate

BeforeDelete

BeforeInsert

BeforeUpdate

New implementation. Data operations are handled in the data source. For more information, see Displaying Data in the Windows Forms DataGridView Control.

ButtonClick

Click (Button control)

Change

TextChanged

NoteNote:
The behavior of the TextChanged event is slightly different. For more information, see ComboBox Control Change Event for Visual Basic 6.0 Users.

Click

SelectedIndexChanged

ColEdit

CellBeginEdit

ColResize

ColumnWidthChanged

DblClick

CellMouseDoubleClick

DragDrop

DragOver

New implementation. For more information, see Drag and Drop for Visual Basic 6.0 Users.

Error

DataError

HeadClick

ColumnHeaderMouseClick

MouseDown

CellMouseDown

MouseMove

CellMouseMove

MouseUp

CellMouseUp

OLECompleteDrag

OLEDragDrop

OLEDragOver

OLEGiveFeedback

OLESetData

OLEStartDrag

New implementation. For more information, see Drag and Drop for Visual Basic 6.0 Users.

OnAddNew

RowsAdded

RowColChange

CurrentCellChanged

RowResize

RowHeightChanged

SelChange

SelectionChanged

SplitChange

New implementation. Split views are not directly supported; use a SplitContainer control.

Validate

Validating

Upgrade Notes

When a Visual Basic 6.0 project is upgraded to Visual Basic 2008, the Change event of the DataGrid control is mapped to the TextChanged event of the Visual Basic 2008 DataGridView control. The behavior of the TextChanged event differs from that of the Change event; this difference may cause unintended results in your code.

Any code related to data-specific methods or events is not upgraded. Warning comments are added to the code, and the code must be removed or modified before compiling the application.

See Also

Concepts

DataGridView Control Technology Summary (Windows Forms)

Additions to Windows Forms for the .NET Framework 2.0

Reference

DataGridView Control Overview (Windows Forms)

Controls and Programmable Objects Compared in Different Languages and Libraries

Other Resources

DataGridView Control (Windows Forms)

Windows Forms Controls for Visual Basic 6.0 Users

Windows Forms Data Binding