Public Class CSR045 '---------------------------------------------------------------- ' A: customer selection '---------------------------------------------------------------- Dim rowFilter As String = String.Empty Dim ds As New DataSet() '------------------------------------------------------------- ' B: get data, load selected view '------------------------------------------------------------- Private Sub CSR045_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load ds.Tables.Add() With ds.Tables(0) .Columns.Add("ACCSCD") .Columns.Add("ACDSLN") .Rows.Add("ORD", "Chicago") .Rows.Add("DEN", "Denver") .Rows.Add("FAT", "Fresno") .Rows.Add("HWD", "Hayward") .Rows.Add("LAS", "Las Vegas") .Rows.Add("LAX", "Los Angeles") .Rows.Add("MOD", "Modesto") .Rows.Add("OAK", "Oakland") .Rows.Add("PAO", "Palo Alto") .Rows.Add("PDX", "Portland") .Rows.Add("RBL", "Red Bluff") .Rows.Add("SAC", "Sacramento") .Rows.Add("PAO", "Palo Alto") .Rows.Add("SLC", "Salt Lake City") .Rows.Add("SAN", "San Diego") .Rows.Add("SFO", "San Francisco") .Rows.Add("SJC", "San Jose") .Rows.Add("SLO", "San Luis Obispo") .Rows.Add("SBA", "Santa Barbara") .Rows.Add("SMO", "Santa Monica") .Rows.Add("SEA", "Seattle") End With rowFilter = String.Empty txtSearch.Text = String.Empty '------------------------------------------------------------- 'set initial search option, load form '------------------------------------------------------------- rdoBeginsWith.Checked = True FormatColumns() FormatView() End Sub '------------------------------------------------------------- ' C: set up columns '------------------------------------------------------------- Private Sub FormatColumns() With DataGridView1.Columns .Add("CCSCD", "Code") .Add("CDSLN", "Description") End With End Sub '------------------------------------------------------------- ' D: size up columns '------------------------------------------------------------- Private Sub FormatColumnsSize() With DataGridView1 .AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells) .Columns("CDSLN").AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill End With End Sub '------------------------------------------------------------- ' E: load data into DataGridView '------------------------------------------------------------- Private Sub FormatView() DataGridView1.Rows.Clear() Dim view As New DataView(ds.Tables(0)) If (rowFilter <> String.Empty) Then view.RowFilter = rowFilter End If Dim rowCount As Integer = 0 For Each drv As DataRowView In view With DataGridView1 .Rows.Add() .Rows(rowCount).Cells("CCSCD").Value = drv("ACCSCD") .Rows(rowCount).Cells("CDSLN").Value = drv("ACDSLN") End With rowCount += 1 Next FormatColumnsSize() End Sub '------------------------------------------------------------- ' F: row was selected, get values and return '------------------------------------------------------------- Private Sub HandleRowSelection() Dim CustCode As String Dim CustName As String With DataGridView1.CurrentRow CustCode = .Cells("CCSCD").Value CustName = .Cells("CDSLN").Value End With MessageBox.Show("CustCode: " & CustCode & " Name: " & CustName) Me.Close() Me.Dispose() End Sub '------------------------------------------------------------- ' G: cancel button, return '------------------------------------------------------------- Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click Me.Close() Me.Dispose() End Sub '------------------------------------------------------------- ' H: handle click, content within a cell '------------------------------------------------------------- Private Sub DataGridView1_CellContentClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick HandleRowSelection() End Sub '------------------------------------------------------------- ' I: handle key presses in DataGridView '------------------------------------------------------------- Private Sub DataGridView1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles DataGridView1.KeyDown '------------------------------------------------------------- ' handle Enter key in DGV cell - display detail program, ' suppress Enter so that DGV cursor does not autoadvance ' down to the next row '------------------------------------------------------------- If e.KeyValue = Keys.Enter Then HandleRowSelection() e.SuppressKeyPress = True Else '------------------------------------------------------------- ' if key is not Arrow Down or Arrow Up, suppress, else pass ' through to DGV to allow row-to-row navigation '------------------------------------------------------------- If Not ((e.KeyValue = Keys.Down) Or (e.KeyValue = Keys.Up) Or (e.KeyValue = Keys.PageDown) Or (e.KeyValue = Keys.PageUp)) Then e.SuppressKeyPress = True End If End If End Sub '------------------------------------------------------------- ' J: handle change of search text '------------------------------------------------------------- Private Sub txtSearch_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtSearch.TextChanged If (txtSearch.Text.Trim().Length = 0) Then rowFilter = String.Empty Else If (rdoBeginsWith.Checked) Then rowFilter = String.Format("ACDSLN like '{0}'", txtSearch.Text.ToUpper() & "%") Else rowFilter = String.Format("ACDSLN like '{0}'", "%" & txtSearch.Text.ToUpper() & "%") End If End If FormatView() End Sub '------------------------------------------------------------- ' K: "begins with" option selected '------------------------------------------------------------- Private Sub rdoBeginsWith_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rdoBeginsWith.CheckedChanged If (txtSearch.Text.Trim().Length > 0) Then txtSearch_TextChanged(sender, e) End If End Sub '------------------------------------------------------------- ' L: "contains" option selected '------------------------------------------------------------- Private Sub rdoContains_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rdoContains.CheckedChanged If (txtSearch.Text.Trim().Length > 0) Then txtSearch_TextChanged(sender, e) End If End Sub End Class