A nebylo by lepší položky i filtrovat?
Public Class Form1
Dim lv As DataListView, dt As DataTable
Dim tb As TextBox, cb As ComboBox
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
lv = New DataListView
tb = New TextBox
cb = New ComboBox
cb.DropDownStyle = ComboBoxStyle.DropDown
Dim pnl As Panel = New Panel
With pnl
.Controls.Add(cb)
.Controls.Add(tb)
tb.Left = cb.Width
.Height = tb.Height
End With
Me.Controls.Add(lv)
lv.Location = New System.Drawing.Point(0, pnl.Height)
lv.Dock = DockStyle.Fill
Me.Controls.Add(pnl)
pnl.Dock = DockStyle.Top
AddHandler cb.SelectedIndexChanged, New EventHandler(AddressOf cb_SelectedIndexChanged)
AddHandler tb.TextChanged, New EventHandler(AddressOf tb_TextChanged)
dt = New DataTable
dt.Columns.Add("Jméno")
dt.Columns.Add("Příjmení")
dt.Rows.Add("Honza", "Dufek")
dt.Rows.Add("Pavel", "Dufek")
dt.Rows.Add("Jan", "Hlávka")
dt.Rows.Add("Milan", "Dufek")
dt.Rows.Add("Jan", "Duffon")
dt.Rows.Add("Pavel", "Hlávka")
dt.Rows.Add("Ondřej", "Linhart")
dt.Rows.Add("Mirek", "Linhart")
lv.SetDataSource(dt)
For Each c As DataColumn In dt.Columns
lv.Columns.Add(c.Caption)
cb.Items.Add(New String(c.Caption))
Next
cb.SelectedIndex = 0
End Sub
Private Sub cb_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
tb.Clear()
End Sub
Private Sub tb_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
If tb.Text = vbNullString Then
lv.SetDataSource(dt)
Else
Dim tbFilter As New DataTable
tbFilter = dt.Clone
Dim flt As String
flt = cb.Text & " Like '" & tb.Text & "%'"
Dim dr() As DataRow = dt.Select(flt)
For i As Integer = 0 To dr.Length - 1
tbFilter.ImportRow(dr(i))
Next
lv.SetDataSource(tbFilter)
End If
End Sub
End Class
Public Class DataListView
Inherits ListView
Public Sub SetDataSource(ByVal fromDataTable As DataTable)
Me.Items.Clear()
For i As Integer = 0 To fromDataTable.Rows.Count - 1
Dim lvItem As ListViewItem
lvItem = Me.Items.Add(fromDataTable.Rows(i).Item(0).ToString)
For j As Integer = 1 To fromDataTable.Columns.Count - 1
lvItem.SubItems.Add(fromDataTable.Rows(i).Item(j).ToString)
Next j
Next i
End Sub
Public Sub New()
Me.View = Windows.Forms.View.Details
Me.HideSelection = False
Me.GridLines = True
End Sub
End Class
|