Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6129835
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T16:47:19+00:00 2026-05-23T16:47:19+00:00

I have a datagrid in my vb.net 4.0 wpf project. I have seen many

  • 0

I have a datagrid in my vb.net 4.0 wpf project. I have seen many examples in XAML on how to bind a DataGridComboBoxColumn however I need to do this in code as I have auto-generating columns. I switch the datagridbinding source to multiple data sets.

Inside some of these custom classes are a couple lists. I can get text and checkboxes to auto-generate correctly. When it comes across my array (I have tried many different kinds) I just see a textboxcolumn with the words (Collection) in it.

For instance – One screen I am grabbing system information on WMI calls. One of the calls returns all IP addresses on the server (We can have up to 8 IP addresses) I do not want a column per IP address. I would like to include a list of those into the datagrid so you can drop down and see them.

Any suggestions on if this is possible or if I am doing something wrong?

Thank you

Sample Code

Imports System.Collections.ObjectModel

Class MainWindow

Dim ServerInfoArray As ObservableCollection(Of ServerInfo) = New ObservableCollection(Of ServerInfo)

Private ReadOnly _ipAddresses As ObservableCollection(Of String) = New ObservableCollection(Of String)


Private Sub GetInfo(ByVal list As List(Of String))
    For Each server As String In list

        Dim tempip As List(Of String) = New List(Of String)
        Dim sinfo As ServerInfo = New ServerInfo

        tempip.Add("192.129.123.23")
        tempip.Add("23.213.223.21")

        sinfo.IPArray = tempip
        sinfo.Servername = server

        ServerInfoArray.Add(sinfo)

    Next
End Sub

Private Sub Button1_Click(sender As System.Object, e As System.Windows.RoutedEventArgs) Handles Button1.Click

    Dim serverlist As List(Of String) = New List(Of String)
    serverlist.Add("Test")
    serverlist.Add("Random")
    serverlist.Add("Local")
    GetInfo(serverlist)

End Sub

Private Sub Window_Loaded(sender As System.Object, e As System.Windows.RoutedEventArgs) Handles MyBase.Loaded
    Dim Col_Serial As DataGridTextColumn = New DataGridTextColumn()
    Col_Serial.Binding = New Binding("Servername")
    Col_Serial.Header = "Servername"
    Col_Serial.Width = 40

    Dim Col_IPArray = New DataGridComboBoxColumn()
    Col_IPArray.Header = "IP Addresses"
    Col_IPArray.IsReadOnly = True
    Col_IPArray.ItemsSource = Me._ipAddresses
    Col_IPArray.SelectedItemBinding = New Binding("IPArray")

    DataGrid1.Columns.Add(Col_Serial)
    DataGrid1.Columns.Add(Col_IPArray)
    DataGrid1.ItemsSource = ServerInfoArray
End Sub
End Class

Class ServerInfo

Dim _Servername As String
Dim _IPArray As List(Of String)

Public Property Servername() As String
    Get
        Return _Servername
    End Get
    Set(ByVal value As String)
        _Servername = value
    End Set
End Property

Public Property IPArray As List(Of String)
    Get
        Return _IPArray
    End Get
    Set(ByVal value As List(Of String))
        _IPArray = value
    End Set
End Property

Public Sub New()
    _Servername = Nothing
    _IPArray = New List(Of String)
End Sub

End Class

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T16:47:20+00:00Added an answer on May 23, 2026 at 4:47 pm

    Not sure if I am understanding perfectly but if you can live with AutoGenerateColumns = False then you can manipulate the columns, their properties and bindings from code like below. So you could use whatever logic you need to in order setup the columns from code. I tried to show how you can source the combobox column items from a different object than the items in the DataGrid as well.

    This is meant to be a simple example so I just did everything in the code-behind but from an MVVM standpoint, it depends on your preferred approach but possibility is that you could accomplish the same logic from a controller class that spins up your view models, etc…

    Hope it helps!

    XAML…

    <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="350" Width="525">
        <StackPanel>
            <Button x:Name="btnRefreshIPList">Refresh list of IPs</Button>
            <DataGrid x:Name="dataGrid1"></DataGrid>
        </StackPanel>
    </Window>
    

    Code behind…

    Imports System.Collections.ObjectModel
    
    Class MainWindow
    
        'The list of IPs for column's ItemSource property
        Private ReadOnly _ipAddresses As ObservableCollection(Of String)
    
        'The items for binding to the DataGrid's ItemsSource
        Private _items As List(Of MyObjectWithIPAddress)
    
        Public Sub New()
    
            ' This call is required by the designer.
            InitializeComponent()
    
            ' Add any initialization after the InitializeComponent() call.
            _ipAddresses = New ObservableCollection(Of String)
            _items = New List(Of MyObjectWithIPAddress)
    
        End Sub
    
        Private Sub Window_Loaded(sender As System.Object, e As System.Windows.RoutedEventArgs) Handles MyBase.Loaded
    
            Me.dataGrid1.AutoGenerateColumns = False
    
            dataGrid1.Columns.Clear()
    
            'Example of text column (Text bound to Name property)
            Dim dgTxtCol = New DataGridTextColumn()
            dgTxtCol.Header = "Name"
            dgTxtCol.Binding = New Binding("Name")
            dataGrid1.Columns.Add(dgTxtCol)
    
            'Example of combobox column (SelectedItem bound to IPAddress)
            Dim dgCmbCol = New DataGridComboBoxColumn()
            dgCmbCol.Header = "IP Address"
            dgCmbCol.ItemsSource = Me._ipAddresses
            dgCmbCol.SelectedItemBinding = New Binding("IPAddress")
            dataGrid1.Columns.Add(dgCmbCol)
    
            'Add items to DataGrid
            _items.Add(New MyObjectWithIPAddress("foo1"))
            _items.Add(New MyObjectWithIPAddress("foo2"))
    
            Me.dataGrid1.ItemsSource = Me._items
    
        End Sub
    
        ''' <summary>
        ''' To emulate fetching the object that has the IP list
        ''' </summary>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Private Function GetIpList() As MyObjectWithListOfIPs
            Dim inst = New MyObjectWithListOfIPs
            inst.IPList = New List(Of String)(New String() {"10.0.0.1", "10.0.0.2", "10.0.0.3"})
            Return inst
        End Function
    
        ''' <summary>
        ''' Updates the ObservableCollection instance based on business object
        ''' </summary>
        ''' <remarks></remarks>
        Private Sub RefreshIpAddresses()
            _ipAddresses.Clear()
            For Each ip As String In GetIpList().IPList
                _ipAddresses.Add(ip)
            Next
        End Sub
    
        Private Sub btnRefreshIPList_Click(sender As System.Object, e As System.Windows.RoutedEventArgs) Handles btnRefreshIPList.Click
            RefreshIpAddresses()
        End Sub
    End Class
    
    ''' <summary>
    ''' Object with response data (e.g., list of IPs)
    ''' </summary>
    ''' <remarks></remarks>
    Class MyObjectWithListOfIPs
    
        Private _ipList As List(Of String)
        Public Property IPList() As List(Of String)
            Get
                Return _ipList
            End Get
            Set(ByVal value As List(Of String))
                _ipList = value
            End Set
        End Property
    
    End Class
    
    ''' <summary>
    ''' Disperate object that "has an" address
    ''' </summary>
    ''' <remarks></remarks>
    Class MyObjectWithIPAddress
    
        Public Sub New(name As String)
            Me._name = name
        End Sub
    
        Private _name As String
        Public Property Name() As String
            Get
                Return _name
            End Get
            Set(ByVal value As String)
                _name = value
            End Set
        End Property
    
        Private _ipAddress As String
        Public Property IPAddress() As String
            Get
                Return _ipAddress
            End Get
            Set(ByVal value As String)
                _ipAddress = value
            End Set
        End Property
    
    End Class
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have quastion about DataGrid in WPF .NET 4. Here is XAML code with
I have a WPF DataGrid (.NET 4) with custom template columns and header styles
I have a performance issue with the WPF DataGrid (.net 4.0) first, some details:
I have a wpf datagrid (.NET 4.0) that contains raw data from a database,
Details VS-2008 Professional SP1 Version .net 3.5 Language:C# I have a WPF Datagrid which
I have one question regarding standard WPF DataGrid in .NET 4.0. When I try
I have a dataset which i bind to datagrid from wpf toolkit(forced to use
I am using the DataGrid from the WPF toolkit in .NET 3.5. I have
I have an ASP.NET Datagrid with several text boxes and drop down boxes inside
I have a WinForms .NET datagrid whose data source is a List<cLineItem> called lines.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.