locked
ArrayList to Datatable

    Question

  •  

    Hi,

     I am designing a object like this:

     

    public class ChannelParameters

          public index as integer

          public channelname as string

          public minvalue as double

          public maxvalue as double

    end class

     

    public  channelsArrayList as ArrayList

     

    public dt as datatable

     

     

    Ok

     

    I hope to transfer the class members to the datatable.

     

    Actually, I know how to build the datatable, including column and row,  so I am using follow code to fill the table

     

     

    For Each Channelitem As ChannelParameters In channelsArrayList

    Dim drow As DataRow

    drow = dt.NewRow

    drow.Item(0) = Channelitem.index

    drow.Item(1) = Channelitem.EnableChannel

    drow.Item(2) = Channelitem.channelName

    drow.Item(3) = Channelitem.physicalchannelLabel

    drow.Item(4) = Channelitem.MinValue

    drow.Item(5) = Channelitem.MaxValue

    drow.Item(6) = Channelitem.ScalingFact

    drow.Item(7) = Channelitem.Offset

    drow.Item(8) = Channelitem.YaxisLeftPosition

    dt.Rows.Add(drow)

    Next

     

    anybody know it is possible for me to build a map between the column and object memeber? then I can be easy to operate this arraylist ( read, write, and delete ....)

     

    Thank you very much!

     

     

    Thursday, October 18, 2007 8:09 PM

Answers

  • Hi yhong,

     

    Based on your post, my understanding of your question is that you need to directly manipulate the ArrayList. If I misunderstood your question, please tell me.

     

    I recommend you first edit the ArrayList object and finally create the datatable based on the ArrayList object. You can make the Arraylist object as the datasource and use BindNavigator, BindingSource and Datagridview to directly manipulate the arraylist object. Please try the following code snippet. I put a datagridview and a bindingnavigator in the form. Hope this helps.

    Code Block

    Imports System.ComponentModel

    Public Class Form1

        Public Class ChannelParameters

            Implements INotifyPropertyChanged

            Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged

            Public Sub New(ByVal ind As Integer, ByVal ch As String, ByVal mv As Double, ByVal mxv As Double)

                _index = ind

                _channelname = ch

                _minvalue = mv

                _maxvalue = mxv

            End Sub

            Public Sub New()

                _index = 0

                _channelname = String.Empty

                _minvalue = 1.0

                _maxvalue = 10.0

            End Sub      

            Public _index As Integer

            Public _channelname As String

            Public _minvalue As Double

            Public _maxvalue As Double

            Public Property index() As Integer

                Get

                    Return _index

                End Get

                Set(ByVal value As Integer)

                    If value <> _index Then

                        _index = value

                        NotifyPropertyChanged("index")

                    End If

                End Set

            End Property

            Public Property channelname() As String

                Get

                    Return _channelname

                End Get

                Set(ByVal value As String)

                    If value <> channelname Then

                        _channelname = value

                        NotifyPropertyChanged("channelname")

                    End If

                End Set

            End Property

            Public Property minvalue() As Double

                Get

                    Return _minvalue

                End Get

                Set(ByVal value As Double)

                    If value <> _minvalue Then

                        _minvalue = value

                        NotifyPropertyChanged("minvalue")

                    End If

                End Set

            End Property

            Public Property maxvalue() As Double

                Get

                    Return _maxvalue

                End Get

                Set(ByVal value As Double)

                    If value <> _maxvalue Then

                        _maxvalue = value

                        NotifyPropertyChanged("maxvalue")

                    End If

                End Set

            End Property

          

            Private Sub NotifyPropertyChanged(ByVal info As String)

                RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(info))

            End Sub

            Public Overrides Function ToString() As String

                Return _index.ToString + "   " + _channelname + "   " + _minvalue.ToString + "   " + _maxvalue.ToString)

            End Function 

        End Class

     

        Public channelsArrayList As New ArrayList

        Public dt As New DataTable

        Dim bd As New BindingSource

        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

            dt.Columns.Add("index", GetType(Integer))

            dt.Columns.Add("channelname", GetType(String))

            dt.Columns.Add("minvalue", GetType(Double))

            dt.Columns.Add("maxvalue", GetType(Double))

            channelsArrayList.Add(New ChannelParameters(1, "tcp", 1.0, 10.0))

            channelsArrayList.Add(New ChannelParameters(1, "http", 1.0, 10.0))

            bd.DataSource = channelsArrayList

            Me.BindingNavigator1.BindingSource = bd

            Me.BindingNavigator1.AddNewItem.Enabled = True

            DataGridView1.DataSource = bd

        End Sub

    End Class

     

     

     

    Best regards,

    Riquel

    Monday, October 22, 2007 6:33 AM
    Moderator

All replies

  • I think I understand what you are asking.  I don't think this is directly built into the Datatable class so the only way that I could see you doing that is via reflection.  So what you could do is have each column in your datatable have a name, and then use the column names (which must match the property names of your 'ChannelParameters' class) as the to inspect each 'ChannelParamaters' object in your arraylist and populate through that way.  Here is a very good article on reflection for VB:

    http://www.ondotnet.com/pub/a/dotnet/2003/10/06/reflectionpt1.html


    Friday, October 19, 2007 12:56 AM
  • Hi yhong,

     

    Based on your post, my understanding of your question is that you need to directly manipulate the ArrayList. If I misunderstood your question, please tell me.

     

    I recommend you first edit the ArrayList object and finally create the datatable based on the ArrayList object. You can make the Arraylist object as the datasource and use BindNavigator, BindingSource and Datagridview to directly manipulate the arraylist object. Please try the following code snippet. I put a datagridview and a bindingnavigator in the form. Hope this helps.

    Code Block

    Imports System.ComponentModel

    Public Class Form1

        Public Class ChannelParameters

            Implements INotifyPropertyChanged

            Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged

            Public Sub New(ByVal ind As Integer, ByVal ch As String, ByVal mv As Double, ByVal mxv As Double)

                _index = ind

                _channelname = ch

                _minvalue = mv

                _maxvalue = mxv

            End Sub

            Public Sub New()

                _index = 0

                _channelname = String.Empty

                _minvalue = 1.0

                _maxvalue = 10.0

            End Sub      

            Public _index As Integer

            Public _channelname As String

            Public _minvalue As Double

            Public _maxvalue As Double

            Public Property index() As Integer

                Get

                    Return _index

                End Get

                Set(ByVal value As Integer)

                    If value <> _index Then

                        _index = value

                        NotifyPropertyChanged("index")

                    End If

                End Set

            End Property

            Public Property channelname() As String

                Get

                    Return _channelname

                End Get

                Set(ByVal value As String)

                    If value <> channelname Then

                        _channelname = value

                        NotifyPropertyChanged("channelname")

                    End If

                End Set

            End Property

            Public Property minvalue() As Double

                Get

                    Return _minvalue

                End Get

                Set(ByVal value As Double)

                    If value <> _minvalue Then

                        _minvalue = value

                        NotifyPropertyChanged("minvalue")

                    End If

                End Set

            End Property

            Public Property maxvalue() As Double

                Get

                    Return _maxvalue

                End Get

                Set(ByVal value As Double)

                    If value <> _maxvalue Then

                        _maxvalue = value

                        NotifyPropertyChanged("maxvalue")

                    End If

                End Set

            End Property

          

            Private Sub NotifyPropertyChanged(ByVal info As String)

                RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(info))

            End Sub

            Public Overrides Function ToString() As String

                Return _index.ToString + "   " + _channelname + "   " + _minvalue.ToString + "   " + _maxvalue.ToString)

            End Function 

        End Class

     

        Public channelsArrayList As New ArrayList

        Public dt As New DataTable

        Dim bd As New BindingSource

        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

            dt.Columns.Add("index", GetType(Integer))

            dt.Columns.Add("channelname", GetType(String))

            dt.Columns.Add("minvalue", GetType(Double))

            dt.Columns.Add("maxvalue", GetType(Double))

            channelsArrayList.Add(New ChannelParameters(1, "tcp", 1.0, 10.0))

            channelsArrayList.Add(New ChannelParameters(1, "http", 1.0, 10.0))

            bd.DataSource = channelsArrayList

            Me.BindingNavigator1.BindingSource = bd

            Me.BindingNavigator1.AddNewItem.Enabled = True

            DataGridView1.DataSource = bd

        End Sub

    End Class

     

     

     

    Best regards,

    Riquel

    Monday, October 22, 2007 6:33 AM
    Moderator
  •  

    Yes, you answer my question! thank you so much!

     

    Thursday, November 01, 2007 3:20 PM