Monday, September 10, 2012

Add an item to WPF Datagrid (VB.NET)

There was a question on visual basic forum on how to add an item to a wpf datagrid. Here's a simple example:
Code view:
Dim linkColumn As New DataGridTextColumn : Dim titleColumn As New DataGridTextColumn  
linkColumn.Header = "Links:" : titleColumn.Header = "Titles:"  
linkColumn.Width = dataGrid.Width / 2 : titleColumn.Width = dataGrid.Width / 2  
linkColumn.Binding = New System.Windows.Data.Binding("Link") : titleColumn.Binding =   
New System.Windows.Data.Binding("Title")  
dataGrid.Columns.Add(linkColumn) : dataGrid.Columns.Add(titleColumn)  
dataGrid.Items.Add(New DataItems() With {.Link = "http://testingme.com", .Title = "mytesting"})
XAML:
 <DataGrid AutoGenerateColumns="False" Height="289" HorizontalAlignment="Left" Margin="10,10,0,0" Name="dataGrid"   
          VerticalAlignment="Top" Width="481" Grid.ColumnSpan="2" ItemsSource="{Binding }">  
     </DataGrid>  
VB.NET Class
     Private _link As String  
         Public Property Link() As String  
           Get  
             Return _link  
           End Get  
          Set(ByVal l As String)  
             _link = l  
           End Set  
         End Property  
        Private _title As String  
        Public Property Title() As String  
          Get  
            Return _title  
          End Get  
          Set(ByVal t As String)  
            _title = t  
          End Set  
        End Property

That's it... :)

0 comments:

Post a Comment