Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

'Array of columns' in DataGridView? 1

Status
Not open for further replies.

traceyr

Technical User
Aug 16, 2002
41
DE
I'm about to create a DataGridView which, in addition to an ID column, will have 66 columns, with column headers numbered from 1 to 66, the rows to hold integer values. It would be time-consuming to define each one manually in the form definition, so (how) can I generate these columns programatically? E.g. can they be defined as an array of columns? Or can I loop through code from 1 to 66 adding the header text, defining the col. attributes in code?
Many thanks.
 
You can add a column or two manually, then look at the form's designer code to see how columns are added to the DataGridView, then mimic this behavior in your own code:
Code:
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim dgvtbc(66) As DataGridViewTextBoxColumn

        For ix As Integer = 0 To dgvtbc.GetUpperBound(0)
            dgvtbc(ix) = New DataGridViewTextBoxColumn
            dgvtbc(ix).HeaderText = ix.ToString
            dgvtbc(ix).Name = "Col" & ix.ToString
        Next

        dgvtbc(0).HeaderText = "Id"
        dgvtbc(0).Name = "Id"

        Me.DataGridView1.Columns.AddRange(dgvtbc)

    End Sub
 
Looks great! Neat approach. Many thanks.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top