I have a DataGridView with several columns, such as year, age, deposits, withdrawals, etc.

There are 13 columns that will not change. The number of rows will vary based on user input from a TextBox and will be based on years, such as 1 to 10 or 1 to x, probably not to exceed 100.

All the data in each of the columns will be generated programatically. For example Age might be calculated as follows:

ClientAge(1) = val(TextBox1.text)

For i = 2 to Years
ClientAge(i) = ClientAge(1) + i
Next i

So if ClientAge(1) = 40 and Years = 10, then I end up with a column of numbers from 40 to 49, or whatever.

How can easily write this one dimensional array to the Age Column in my DataGridView where ClientAge one is in Row 1, ClientAge two is in Row 2, etc.

I know this can be done in a simple loop, but after much reading I can't figure out how to do this.

Any help would be much appreciated. Thanks in advance.


Re: Visual Basic Express Edition Programmatically (Manually) Entering Data in DataGridView - Unbound - Help/Ideas Please

Squire James

First, I would add a Dataset to the project, and create a DataTable (no Adapter, since no physical database is involved) with the fixed column names. Bind the DataTable to the DataGridView (by setting the DataSource property to the name of the Dataset and the DataMember property to the name of the DataTable). Then each time you want to add a row to the grid: create a row object, populate the columns you want, and add the row to the Dataset.

Your loop would look something like this:

Dim ClientAge as Integer = Integer.Parse(Textbox1.Text)

For i = 0 to Years

Dim row as DataTableRow = oDataSetInstance.TableNewRow

row.Age = ClientAge + i

' add whatever else you want to other columns as desired


Next i

Re: Visual Basic Express Edition Programmatically (Manually) Entering Data in DataGridView - Unbound - Help/Ideas Please


If you would prefer to write to the DataGridView directly, try:

grdMyGrid.RowCount = 0 'Clears existing data from grid control

grdMyGrid.RowCount = Years

For i = 1 To Years

grdMyGrid.Rows(i-1).Cells(0).Value = ClientAge(i)

'Note that the Rows index is zero-based, hence i-1; also assumes that the age should go in the left-hand column of the grid - 'change .Cells(0) to .Cells(some other number) as appropriate

Next i

Hope this helps