Thursday, July 25, 2013

Insert Data in DataGridView

Hi,

Lets say we already have DataGridView control as 'dgView' defined on our form with our columns defined as  FirstName, LastName and Address.
All columns were added from Design view of the form.
Now in code behind, if we just to add row without binding it to some datasource, it would be:
 
dgView.Rows.Add(New String() {"John","Doe","123 Main St, City UK"})
 

If we want to bind it with dataset or datatable then it would be :
 
Dim dt As New DataTable
dt.Columns.Add("FirstName") dt.Columns.Add("LastName") dt.Rows.Add(New String() {"saqib", "mahmood"}) ' This property is set if we dont have predefined columns in DataGridView.
dgView.AutoGenerateColumns = True dgView.DataSource = dt


Happy Coding!

No comments: