Populating Datagrid view at runtime

ganesha

New member
Joined
May 6, 2008
Messages
1
Programming Experience
Beginner
Hi,

I am new to .net platform.
I want to display two columns in data grid with some rows in it.
These columns do not have any table/ data dictionary reference.
Is it possible to create/populate datagview at run time with columns
not refering to any tables. Values of the rows will also be provided in program itself.
You inputs to this will be appreiciable.

Rgds,
Ganesh
 
Yes, everthing you can do in Designer you can also do in code. Of course, the more you can set up in Designer the less code you need to write. Using no tables the columns is operated through the Columns property and rows through Rows property or RowCount property, setting/getting data through Item property. For example:
VB.NET:
Dim col As New DataGridViewTextBoxColumn
col.Name = "numbers"        
Me.DataGridView1.Columns.Add(col)
Me.DataGridView1.RowCount = 5
For rowindex As Integer = 0 To 4
    Me.DataGridView1.Item("numbers", rowindex).Value = rowindex.ToString
Next
You can also go through the Rows property setting different Cells value:
VB.NET:
Me.DataGridView1.Rows(2).Cells("numbers").Value = "99"
I've used the String indexer for columns here (column name) because it is more readable, but it can also be operated with Integer indexer like the rows.

The columns can of course be added in Designer too, and only data populated in code.
 
Back
Top