How to create DataColumn object at runtime

Hello,

I am trying to display an array in datagrid. The problem is I do not know the dimension of the array at design time, so I need to create DataColumn objects at runtime. Can someone show me the way, please Any help is appreciated. Thanks.

Michael



Answer this question

How to create DataColumn object at runtime

  • lemmi

    Heres how I did it:

    Private Sub btnTest_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTestck
    Dim aryVals() As String = {"Test 1", "Test 2", "Test 3"}
    Dim dt As DataTable = DTFromStringArray(aryVals, 50)
    aryVals = New String() {"Testing", "1 2 3", "Sample"}
    DTAddArray(dt, aryVals)
    DataGridView1.DataSource = dt
    End Sub

    #Region " Get a Data Table from a String Array "
    #Region " Comments "
    '''
    '''<summary>Returns an Empty DataTable</summary>
    '''<example>
    '''
    '''Dim dtTest As System.Data.DataTable
    '''Dim aryData() As String = {"Testing", "1 2 3", "Sample"}
    '''dtTest = yourclass.DTFromStringArray(aryNames)
    '''DataGrid1.DataSource = dtTest
    '''
    '''</example>
    '''<param name="aryValues">An Array of Strings</param>
    '''<param name="intDefaultColumnWidth">The Default Width of all the Columns</param>
    '''<returns>A DataTable Object</returns>
    '''<remarks></remarks>
    #End Region

    Public Shared Function DTFromStringArray(ByVal aryValues() As String, Optional ByVal intDefaultColumnWidth As Integer = 255) As DataTable
    Dim dt As DataTable = New DataTable("tblArray")
    Dim colNew(aryValues.GetUpperBound(0)) As DataColumn
    For i As Integer = 0 To aryValues.GetUpperBound(0)
    colNew(i) = New DataColumn
    With colNew(i)
    .
    ColumnName = "Value " & i
    .DataType = GetType(String)
    .
    AllowDBNull = False
    .DefaultValue = ""
    .MaxLength = intDefaultColumnWidth
    .Unique = False
    End With
    Next

    dt.Columns.AddRange(colNew)

    Dim pRow As DataRow = dt.NewRow
    For i As Integer = aryValues.GetLowerBound(0) To aryValues.GetUpperBound(0)
    pRow.Item(i) = aryValues(i)
    Next

    dt
    .Rows.Add(pRow)
    Return dt
    End Function

    #Region " Comments "
    '''<summary>
    '''Adds a Row to the DataTable Using the ColumnNames and RowValues linked by Index.
    '''</summary>
    '''<example>
    '''
    '''Dim aryVals() As String = {"Val1", "Val2", "Val2"}
    '''Dim dt As DataTable = DTFromStringArray(aryVals, 50)
    '''DTAddArray(dt, aryVals)
    '''
    '''</example>
    '''<param name="dt">DataTable Built using DTFromArray</param>
    '''<param name="aryRowValues">The Values to Set each Row</param>
    '''<remarks>
    '''</remarks>
    #End Region
    Public Shared Sub DTAddArray(ByRef dt As DataTable, ByVal aryRowValues() As String)
    Dim pRow As DataRow
    pRow = dt.NewRow
    For i As Integer = aryRowValues.GetLowerBound(0) To aryRowValues.GetUpperBound(0)
    pRow.Item(i) = aryRowValues(i)
    Next

    dt.Rows.Add(pRow)
    End Sub
    #End Region



  • Teo Lachev

    Datagridview add column:

    Me.DataGridView1.Columns.Add("ColumnName", "HeaderText")



  • Rajesh batchu

    Hello, thank you very much for your reply. that's really helpful.

    Michael


  • How to create DataColumn object at runtime