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 = "", .Title = "mytesting"})
 <DataGrid AutoGenerateColumns="False" Height="289" HorizontalAlignment="Left" Margin="10,10,0,0" Name="dataGrid"   
          VerticalAlignment="Top" Width="481" Grid.ColumnSpan="2" ItemsSource="{Binding }">  
VB.NET Class
     Private _link As String  
         Public Property Link() As String  
             Return _link  
           End Get  
          Set(ByVal l As String)  
             _link = l  
           End Set  
         End Property  
        Private _title As String  
        Public Property Title() As String  
            Return _title  
          End Get  
          Set(ByVal t As String)  
            _title = t  
          End Set  
        End Property

That's it... :)


Post a Comment