Results 1 to 4 of 4

Thread: populate ListView with data

  1. #1
    ilyama is offline VB.NET Forum Newbie
    .NET Framework
    .NET 4.5
    Join Date
    Sep 2014
    Posts
    1
    Reputation
    0

    populate ListView with data

    Hi,

    I have a server program accepting clients. When a server event occurs the function UpdateClientList() is called.
    The function needs to populate the Listview (lsClients) with all the clients.


    Code:
    <Window x:Class="MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="500" Width="850" Loaded="Window_Loaded" Initialized="Window_Initialized">
        <Grid>
            <TabControl HorizontalAlignment="Left" Height="460" VerticalAlignment="Top" Width="832" TabStripPlacement="Left" SelectionChanged="TabControl_SelectionChanged">
                <TabItem Header="TabItem" Width="120" Margin="0,0,0,-27">
                    <ListView x:Name="lvClients" Margin="20,0,17,138">
                        <ListView.View>
                            <GridView>
                                <GridViewColumn Width="80" Header="status"/>
                                <GridViewColumn Width="80" Header="SessionID"/>
                                <GridViewColumn Width="80" Header="MachineID"/>
                            </GridView>
                        </ListView.View>
                    </ListView>
                    
                </TabItem>
    
                <TabItem Header="TabItem" Width="120" Height="50" Margin="0,28,0,-28">
                    <Button Content="Button" Height="100" Width="75" Margin="279,326,346,24" Click="Button_Click"/>
                </TabItem>
      </TabControl>
        </Grid>
    </Window>
    .vb code
    Code:
     Private Sub UpdateClientsList()
    
            InitializeComponent()
            Dim sessionList As List(Of TcpComm.Server.SessionCommunications) = server.GetSessionCollection()
            Dim lvi As ListView
    
            Me.lvClients.Items.Clear()
    
            For Each session As TcpComm.Server.SessionCommunications In sessionList
    
                lvi = New ListView
                lvi.Items.Add(session.sessionID.ToString())
    
                lvi.Items.Add(session.machineId)
    
                Me.lvClients.Items.Add(lvi)
            Next
        End Sub
    But this doesn't seem to work.
    How can I easily show the sessions in the ListView?
    Any info at all would be appreciated. I'm totally new to WPF and the data biding is making my head spin...

  2. #2
    jasin is offline VB.NET Forum Newbie
    .NET Framework
    Join Date
    Mar 2014
    Posts
    1
    Reputation
    0
    I'm out right now. But I would would just make an observable collection and attach it to the listview.itemssource.

  3. #3
    jdsanders24 is offline VB.NET Forum Newbie
    .NET Framework
    .NET 4.0
    Join Date
    Oct 2014
    Posts
    4
    Reputation
    0
    Hello, Im not sure of the rules for posting code.. so for now I will point you toward some resources for research.

    First thing I would research is DataBinding. Binding in WPF is fairly simple and will make your world so much easier. Bind your listview to a ItemsSource such as a datatable then bind the items to the columns of the table.

    -Jonathan

  4. #4
    jdsanders24 is offline VB.NET Forum Newbie
    .NET Framework
    .NET 4.0
    Join Date
    Oct 2014
    Posts
    4
    Reputation
    0
    Decided to go ahead and give you a sample. (Sorry if I broke any rules)

    Code Behind:
    Code:
    Imports System.Data
    
    
    Class MainWindow
    
    
        
    PrivateSub Button_Click(sender AsObject, e AsRoutedEventArgs)
    
            
    Dim DT AsNewDataTable
    
    
            
    'Setup DataTable Columns
    
            DT.Columns.Add("Column1")
    
            DT.Columns.Add("Column2")
    
            DT.Columns.Add("Column3")
    
    
            
    'Create New Row
    
            
    Dim DR AsDataRow = DT.NewRow
    
            DR.Item("Column1") = "Row 1 Col 1"
    
            DR.Item("Column2") = "Row 1 Col 2"
    
            DR.Item("Column3") = "Row 1 Col 3"
    
    
            
    'Add row to DataTable DT
    
            DT.Rows.Add(DR)
    
    
            
    'Create new row
    
            DR = DT.NewRow
    
            DR.Item("Column1") = "Row 2 Col 1"
    
            DR.Item("Column2") = "Row 2 Col 2"
    
            DR.Item("Column3") = "Row 2 Col 3"
    
    
            
    'Add row to DataTable DT
    
            DT.Rows.Add(DR)
    
    
            
    'Set lvClients ItemsSource to the default view of DataTable DT
    
            lvClients.ItemsSource = DT.DefaultView
    
    
            
    'Bindings in XAML take care of displaying the data automatically.
    
        
    End Sub
    
    
     
    
        
    PrivateSub TabControl_SelectionChanged(sender AsObject, e AsSelectionChangedEventArgs)
    
    
        
    End Sub
    
    
        
    PrivateSub Window_Initialized(sender AsObject, e AsEventArgs)
    
    
        
    End Sub
    
    
        
    PrivateSub Window_Loaded(sender AsObject, e AsRoutedEventArgs)
    
    
        
    End Sub
    
    End Class
    XAML:
    Code:
    <Window x:Class="MainWindow"
    
       
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    
       
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    
       
     Title="MainWindow" Height="500" Width="850" Loaded="Window_Loaded" Initialized="Window_Initialized">
    
        
    <Grid>
    
            
    <TabControl HorizontalAlignment="Left" Height="460" VerticalAlignment="Top" Width="832" TabStripPlacement="Left" SelectionChanged="TabControl_SelectionChanged">
    
                
    <TabItem Header="TabItem" Width="120" Margin="0,0,0,-27">
    
                    
    <ListView x:Name="lvClients" Margin="20,0,17,138">
    
                        
    <ListView.View>
    
                            
    <GridView>
    
                                
    <GridViewColumn Width="80" Header="status" DisplayMemberBinding="{Binding Path=Column1}" />
    
                                
    <GridViewColumn Width="80" Header="SessionID" DisplayMemberBinding="{Binding Path=Column2}"/>
    
                                
    <GridViewColumn Width="80" Header="MachineID" DisplayMemberBinding="{Binding Path=Column3}"/>
    
                            
    </GridView>
    
                        
    </ListView.View>
    
                    
    </ListView>
    
    
                
    </TabItem>
    
    
                
    <TabItem Header="TabItem" Width="120" Height="50" Margin="0,28,0,-28">
    
                    
    <Button Content="Button" Height="100" Width="75" Margin="279,326,346,24" Click="Button_Click"/>
    
    
                
    </TabItem>
    
            
    </TabControl>
    
        
    </Grid>
    
    </Window>
    All you have to do is set the DisplayMemberBinding of the GridView column to the name of the DataTable column, simple eh?
    Last edited by jdsanders24; 10-17-2014 at 9:25 AM. Reason: Code Font Coloring was difficult to read..

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •