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?
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")
Re: Data Grid coding not working properly!!!
The datasoure of datagridview belong to datatable format.
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?
Re: Data Grid coding not working properly!!!
Code:
The datasoure of datagridview belong to datatable format
Then where should I rectify my code Manhit?
Re: Data Grid coding not working properly!!!
Quote:
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
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.....
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")
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?
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.
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?
Re: Data Grid coding not working properly!!!
Do you want use crystal report or excel to view.
creat other new thread.