how to fill a "Programmatically added DataGridView"

aydinozdemir

Member
Joined
May 15, 2011
Messages
15
Programming Experience
Beginner
hello i am beginner in Visual Basic(2008) programming and need your help.searched the forum and couldnt find a solution to my problem.
i want to populate a datagridview which was added programmatically.i get error that says "DataGridView1" is not declared.

this code works well if a DataGridView1 was added manually(drag-drop).
if the form doesnt have a DataGridView1 then gives the error i wrote.
where and how i can declare or what do i must do to fill? thanks for any help.

Imports System.Data.OleDb

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Dim con As New OleDbConnection
Dim sql As String
Dim ds As New DataSet
'Dim dr As New DataColumnCollection
Dim da As OleDbDataAdapter

Try

Me.Controls.Add(New DataGridView)

'The Connection string.
con.ConnectionString = "PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source = C:\Ders\Lessons.mdb"

'Pass the Connection string to OleDbConnection object.
con.Open()

'Build the SQL strings.
sql = "SELECT * FROM Teacher_1;"

'Initialize the OleDbDataAdapter with SQL and Connection string,
'and then use the OleDbAdapter to fill the DataSet with data.

da = New OleDbDataAdapter(sql, con)
da.Fill(ds, "Temp1")

'Bind the DataSet to DataGrid.
DataGridView1.DataSource = ds
DataGridView1.DataMember = "Temp1"
con.Close()


Catch Excep As System.Exception
'MessageBox.Show(Excep.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
 
First up, why are you adding the grid in code? There doesn't seem to be a reason for you to do so, based on your code. Why not just add the control in the designer?

That said, the solution is simple: declare a variable and assign that grid to it. That's all that happens when you add the grid in the designer. You're doing this:
Me.Controls.Add(New DataGridView)
so you have no reference to the DataGridView you just created. Declare a variable, create the grid, assign the grid to the variable, then add the grid to the form using that variable. You then have the variable via which to access the grid for other purposes too.
 
thanks for respond. i will add the code inside a loop after i make it work. so that i can create needed number of DataGridViews inside a TabControl's pages..needed number of DataGridViews will be taken form the count of Tables inside the database file.

like this you mean?
Me.Controls.Add(New DataGridView)
Dim DataGridView1 As DataGridView
no errors occur while debugging, new DataGridView1 creates on button click, but it doesnt fill..it is just empty.
 
when a first grid is created programmatically during runtime, what name does it have?
DataGridView1 or something else?
DataSource and DataMember doesnt seem to fill DataGridView1..
 
jmc said:
Declare a variable, create the grid, assign the grid to the variable, then add the grid to the form using that variable. You then have the variable via which to access the grid for other purposes too.
VB.NET:
dim dgv as new datagridview
dgv.datasource=...
controls.add(dgv)

when a first grid is created programmatically during runtime, what name does it have?
It has no name until you set the Name property. This can be used later to access the control from the Controls collection by name; eg Controls("controlname"). In above example the Name would be set like this:
VB.NET:
dgv.Name = "controlname"
 
thank you very much guys ..this worked very well.:)

Dim DataGridView1 As New DataGridView
DataGridView1.DataSource = ds
DataGridView1.DataMember = "Temp1"
Controls.Add(DataGridView1)
 
hello, i ve been trying to update the database table which was shown in Datagridview1, with the changes those i made on the grid. but couldnt find a way to do..i ve been reading the forum and still stucked.i will be thankful for any help.

i am trying to use this command under a new button click.and gives me error DataGridView1 is not declared. how can i declare this to refer my datagrid?
DataGridView1.update()
 
The DataGridView has nothing to do with saving data. It's all about displaying and editing data. It has no comprehension of where that data came from or is going to. You used a DataAdapter to retrieve the data into a DataTable and you use the same DataAdapter (or a different one if you prefer) to save the changes from the same DataTable. Fill gets the data and Update saves it.
 
thanks for spending time,answering my questions and your help. i got the basic principals of dataadapter and dataset but i need more practise.

i found that a variable which was "unnecessarily" declared as dataset inside the loop is causing the problem and i was getting a "null error for dataset" during dataadapter update.
Edit: Problem Solved

a partial code of my project that works well if someone needs.

Imports System.Data.OleDb

Public Class Form1

Dim con As New OleDbConnection("PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source = C:\Ders\ders.mdb")
Dim cmd As OleDbCommand
Dim cb As OleDbCommandBuilder

Dim da As OleDbDataAdapter
Dim ds As New DataSet
Dim changes As DataSet
Dim dt As DataTable()
Dim typeRow As DataRow
Dim sql As String

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click


sql = "Select * from Aydin_Ozdemir"
Try
con.Open()
da = New OleDbDataAdapter(sql, con)
da.Fill(ds)


Dim DataGridView1 As New DataGridView

Controls.Add(DataGridView1)
DataGridView1.DataSource = (ds.Tables(0))
con.Close()
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

Try
cb = New OleDbCommandBuilder(da)
changes = ds.GetChanges()
If changes IsNot Nothing Then
da.Update(ds.Tables(0))
End If
ds.AcceptChanges()
MsgBox("Save changes")
Catch ex As Exception
MsgBox(ex.ToString)
End Try

End Sub
End Class
 
Back
Top