Reputation: 725
I can add column in ListView
, but it is so difficult to add an item in ListView
. Here is my code:
Dim myGridView As New GridView
myGridView.AllowsColumnReorder = True
myGridView.ColumnHeaderToolTip = "Employee Information"
Dim gvc1 As New GridViewColumn
gvc1.DisplayMemberBinding = New Binding("FirstName")
gvc1.Header = "FirstName"
gvc1.Width = 100
myGridView.Columns.Add(gvc1)
Dim gvc2 As New GridViewColumn
gvc2.DisplayMemberBinding = New Binding("LastName")
gvc2.Header = "Last Name"
gvc2.Width = 100
myGridView.Columns.Add(gvc2)
Dim gvc3 As New GridViewColumn()
gvc3.DisplayMemberBinding = New Binding("EmployeeNumber")
gvc3.Header = "Employee No."
gvc3.Width = 100
listview.View = myGridView
I just created ListViewItem.add
and then used the subitem
to add more items in a row. But now it's different. How can I add item in listview
in WPF WITHOUT creating a new class, because the number of the columns of data is dynamic.
EDIT:
I had been surfing the internet for hours but found nothing of great help. Everywhere, it is done using class with predefined numbers of column, while I want to add column item based on database which should have different Ipnumber of columns.
Upvotes: 0
Views: 5315
Reputation: 1
XAML part of your program must be like be this:
<ListView x:Name="lvPencere" HorizontalAlignment="Left" Height="156" Grid.Row="1" VerticalAlignment="Top" Width="309">
<ListView.View>
<GridView>
<GridView.Columns>
<GridViewColumn Header="PencereSN" Width="0" DisplayMemberBinding="{Binding PencereSN}"/>
<GridViewColumn Header="Pencere Adı" Width="300" DisplayMemberBinding="{Binding PencereAD}"/>
</GridView.Columns>
</GridView>
</ListView.View>
</ListView>
And your VB part must be like this:
Listview.Items.Add(New With {Key .PencereSN = "some string", Key .PencereAD = "some string"})
Upvotes: 0
Reputation: 1767
You should initialize the object collection in your code, for example Employees
.
Imports System.ComponentModel
Imports System.Collections.ObjectModel
Public Class EmployeeInformation
Public Property FirstName As String
Public Property LastName As String
Public Property EmployeeNumber As Integer
End Class
Class MainWindow
Public Property Employees As ObservableCollection(Of EmployeeInformation)
Public Sub New()
' This call is required by the designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
InitEmploeesCollection(10)
DataContext = Me
End Sub
Private Sub InitEmploeesCollection(count As Integer)
Employees = New ObservableCollection(Of EmployeeInformation)()
For index = 1 To count
Employees.Add(New EmployeeInformation() With {
.FirstName = "FirstName" & index,
.LastName = "LastName" & index,
.EmployeeNumber = index})
Next
End Sub
End Class
And then you can simply bind it to the ListView
:
<ListView ItemsSource="{Binding Path=Employees}">
<ListView.View>
<GridView AllowsColumnReorder="True" ColumnHeaderToolTip="Employee Information">
<GridViewColumn Header="First Name" Width="100" DisplayMemberBinding="{Binding Path=FirstName}"/>
<GridViewColumn Header="Last Name" Width="100" DisplayMemberBinding="{Binding Path=LastName}"/>
<GridViewColumn Header="Employee No." Width="100" DisplayMemberBinding="{Binding Path=EmployeeNumber}"/>
</GridView>
</ListView.View>
</ListView>
To initialize columns dynamically, you can add the code from your question. It doesn't make difference if you are taking data from database. Just fill in Employees
collection and bind it to the ListView
.
Imports System.ComponentModel
Imports System.Collections.ObjectModel
Class MainWindow
Public Property Employees As ObservableCollection(Of EmployeeInformation)
Public Sub New()
' This call is required by the designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
InitEmploeesCollection(10)
SetGridViewDynamically()
DataContext = Me
End Sub
Private Sub InitEmploeesCollection(count As Integer)
Employees = New ObservableCollection(Of EmployeeInformation)()
For index = 1 To count
Employees.Add(New EmployeeInformation() With {
.FirstName = "FirstName" & index,
.LastName = "LastName" & index,
.EmployeeNumber = index})
Next
End Sub
Private Sub SetGridViewDynamically()
Dim myGridView As New GridView
myGridView.AllowsColumnReorder = True
myGridView.ColumnHeaderToolTip = "Employee Information"
Dim gvc1 As New GridViewColumn
gvc1.DisplayMemberBinding = New Binding("FirstName")
gvc1.Header = "FirstName"
gvc1.Width = 100
myGridView.Columns.Add(gvc1)
Dim gvc2 As New GridViewColumn
gvc2.DisplayMemberBinding = New Binding("LastName")
gvc2.Header = "Last Name"
gvc2.Width = 100
myGridView.Columns.Add(gvc2)
Dim gvc3 As New GridViewColumn()
gvc3.DisplayMemberBinding = New Binding("EmployeeNumber")
gvc3.Header = "Employee No."
gvc3.Width = 100
myGridView.Columns.Add(gvc3)
ListView1.View = myGridView
End Sub
End Class
Public Class EmployeeInformation
Public Property FirstName As String
Public Property LastName As String
Public Property EmployeeNumber As Integer
End Class
This way XAML will look like this.
<ListView Name="ListView1" ItemsSource="{Binding Path=Employees}"/>
Upvotes: 3
Reputation: 387
Dim row As String() = New String() {"John", "Doe", "1"}
myGridView.Rows.Add(row)
Is this what you're looking for?
Upvotes: 1