Wednesday, June 30, 2004
DataGrid Help 2 -- Windows Form VB.NET
Create the Sample
Follow these steps to create a new Visual Basic Windows Application project:
Start Microsoft Visual Studio .NET.
On the File menu, point to New, and then click Project.
In
the New Project dialog box, click Visual Basic Project under Project
Types, and then click Windows Application under Templates. By default,
Form1 is added.
Drag a DataGrid control from the toolbox to Form1.
Add the following code to the top of the code window in the Declarations section of Form1.vb:
Imports System.Data.SqlClient
Imports System.Windows.Forms
Add the following code after the "Windows Form Designer generated code" section of the code window:
Public MyCombo As New ComboBox()
Dim con As New SqlConnection("server=myservername;uid=myid;pwd=mypassword;database=northwind")
Dim daEmp As New SqlDataAdapter("Select * From Employees", con)Public ds As New DataSet()
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
AddHandler MyCombo.TextChanged, AddressOf Ctrls_TextChanged
'Fill ComboBox list.
MyCombo.Name = "MyCombo"
MyCombo.Visible = False
MyCombo.Items.Clear()
MyCombo.Items.Add("Sales Representative")
MyCombo.Items.Add("Inside Sales Coordinator")
MyCombo.Items.Add("Vice President, Sales")
MyCombo.Items.Add("Sales Manager")
MyCombo.Items.Add("Flunky")
daEmp.Fill(ds, "Employees")'Set the RowHeight of the DataGrid to the height of the ComboBox.
DataGrid1.PreferredRowHeight = MyCombo.Height
DataGrid1.DataSource = dsDataGrid1.DataMember = "Employees"
'Add ComboBox to the Control collection of the DataGrid.
DataGrid1.Controls.Add(MyCombo)
End SubPrivate Sub DataGrid1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles DataGrid1.Paint
If DataGrid1.CurrentCell.ColumnNumber = 3 Then
MyCombo.Width = DataGrid1.GetCurrentCellBounds.Width
End If
End SubPrivate Sub Ctrls_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs)
If DataGrid1.CurrentCell.ColumnNumber = 3 Then
MyCombo.Visible = False
If DataGrid1.Item(DataGrid1.CurrentCell) & "" = "" Then
SendKeys.Send("*")
End If
DataGrid1.Item(DataGrid1.CurrentCell) = MyCombo.Text
End If
End Sub
Private Sub DataGrid1_CurrentCellChanged(ByVal sender As Object, ByVal
e As System.EventArgs) Handles DataGrid1.CurrentCellChanged
If DataGrid1.CurrentCell.ColumnNumber = 3 Then
MyCombo.Visible = False
MyCombo.Width = 0
MyCombo.Left = DataGrid1.GetCurrentCellBounds.Left
MyCombo.Top = DataGrid1.GetCurrentCellBounds.Top
MyCombo.Text = DataGrid1.Item(DataGrid1.CurrentCell) & ""
MyCombo.Visible = True
Else
MyCombo.Visible = False
MyCombo.Width = 0
End If
End SubPrivate Sub DataGrid1_Scroll(ByVal sender As Object, ByVal e As System.EventArgs) Handles DataGrid1.Scroll
MyCombo.Visible = False
MyCombo.Width = 0
End SubPrivate Sub DataGrid1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles DataGrid1.Click
MyCombo.Visible = False
MyCombo.Width = 0
End Sub
Modify the connection string as necessary for your environment.
Press
F5 to run the project. Click one of the fields in the Title column in
the DataGrid. Notice that the ComboBox control is located in the
DataGrid.
Expand the ComboBox. Notice that a list of titles is displayed.