|
-
Jul 17th, 2009, 10:23 PM
#1
Thread Starter
Frenzied Member
Data Grid coding not working properly!!!
I did this code:
Code:
Imports System.Data
Imports System.Data.OleDb
Public Class Form1
Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim connString As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Users\Gautam\Documents\Visual Studio 2005\Projects\waste\waste\db1.mdb"
Dim myConnection As OleDbConnection = New OleDbConnection
myConnection.ConnectionString = connString
Dim da As OleDbDataAdapter = New OleDbDataAdapter("Select * from Table1", myConnection)
Dim ds As DataSet = New DataSet
da.Fill(ds, "Table1")
DataGridView1.DataSource = ds.DefaultViewManager
End Sub
End Class
I get this output on button click:
Attachment 72031
Where does the error lies in my code?
Last edited by gautamshaw; Feb 21st, 2010 at 01:22 PM.
-
Jul 17th, 2009, 10:26 PM
#2
Re: Data Grid coding not working properly!!!
This line of code
Code:
DataGridView1.DataSource = ds.DefaultViewManager
Should be changed to this:
Code:
DataGridView1.DataSource = ds.Tables("Table1")
Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
- Abraham Lincoln -
-
Jul 17th, 2009, 10:27 PM
#3
Fanatic Member
Re: Data Grid coding not working properly!!!
The datasoure of datagridview belong to datatable format.
-
Jul 17th, 2009, 10:32 PM
#4
Thread Starter
Frenzied Member
Re: Data Grid coding not working properly!!!
Sorry Stanav......
a new set of are add on doing this:
Code:
DataGridView1.DataSource = ds.Tables("Table1")
the output is:
Attachment 72032
How to rectify this?
Last edited by gautamshaw; Feb 21st, 2010 at 01:22 PM.
-
Jul 17th, 2009, 10:34 PM
#5
Thread Starter
Frenzied Member
Re: Data Grid coding not working properly!!!
Code:
The datasoure of datagridview belong to datatable format
Then where should I rectify my code Manhit?
-
Jul 17th, 2009, 10:41 PM
#6
Fanatic Member
Re: Data Grid coding not working properly!!!
 Originally Posted by gautamshaw
Code:
The datasoure of datagridview belong to datatable format
Then where should I rectify my code Manhit?
Code:
Imports System.Data
Imports System.Data.OleDb
Public Class Form1
Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim connString As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Users\Gautam\Documents\Visual Studio 2005\Projects\waste\waste\db1.mdb"
Dim myConnection As OleDbConnection = New OleDbConnection
myConnection.ConnectionString = connString
Dim da As OleDbDataAdapter = New OleDbDataAdapter("Select * from Table1", myConnection)
Dim ds As DataSet = New DataSet
da.Fill(ds, "Table1")
dim dt as new datatable
da.fill(dt)
DataGridView1.DataSource = dt
End Sub
End Class
-
Jul 17th, 2009, 10:47 PM
#7
Thread Starter
Frenzied Member
Re: Data Grid coding not working properly!!!
Sorry manhit.....
again the same problem...
a new set of columns are added and the output looks like the above image that i posted!!!
Code still not working.....
-
Jul 17th, 2009, 10:56 PM
#8
Re: Data Grid coding not working properly!!!
OK. You need to set the datapropertyname property to your datagridview columns.
Add these lines:
Code:
DataGridView1.Columns(0).DataPropertyName = "Name"
DataGridView1.Columns(1).DataPropertyName = "Address"
DataGridView1.Columns(2).DataPropertyName = "Roll"
Right before this line
Code:
DataGridView1.DataSource = ds.Tables("Table1")
Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
- Abraham Lincoln -
-
Jul 17th, 2009, 11:02 PM
#9
Thread Starter
Frenzied Member
Re: Data Grid coding not working properly!!!
You did not understand my question......Or may be i did not explained it to you properly.......I am sorry for that.....
If my form at design time looks like this then:
Attachment 72033
Your code is perfect
But my design time form is like this:
Attachment 72034
I just want to place the values from the database to the grid columns inspite of adding the entire database columns to the grid.
How to do this?
Last edited by gautamshaw; Feb 21st, 2010 at 01:22 PM.
-
Jul 17th, 2009, 11:17 PM
#10
Re: Data Grid coding not working properly!!!
I knew this from looking at the screenshot in your last post. That's why I told you to set the columns's datapropertyname property.
Did you try what I said in the last post? You can add those 3 lines before or after the line you set the datagridview.datasource property, but adding them before gives you a slight performance advantage. And that's why I told you to add the lines before you bind the datagidview.
Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
- Abraham Lincoln -
-
Jul 17th, 2009, 11:36 PM
#11
Thread Starter
Frenzied Member
Re: Data Grid coding not working properly!!!
yes i added the three lines that you gave and it worked excellently.......
Thanks stanav
Now i want to add a print button so that i can print the datagridview contents.......
How to do this?
-
Jul 18th, 2009, 12:14 AM
#12
Fanatic Member
Re: Data Grid coding not working properly!!!
Do you want use crystal report or excel to view.
creat other new thread.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|