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
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