VB.NET - datagridview

Asked By Ananth Christi Vijay on 07-Dec-10 02:27 AM
hi,

in my project i want to display the records using gridview.

by in my visual studio package i've only datagridview.

how should i add gridview to the tools.
Sagar P replied to Ananth Christi Vijay on 07-Dec-10 02:34 AM
GridView comes with ASP.Net web application not with windows applciation.
In windows we have DataGridView which will act same as gridview in web application..

So you can use DataGridView to show your data... which is same as GridView in web application....
Ananth Christi Vijay replied to Sagar P on 07-Dec-10 02:37 AM
Thanx,

could you please tell me the code to display the data in the datagridview from my access database..

Sagar P replied to Ananth Christi Vijay on 07-Dec-10 02:57 AM
Its simple, you can use this code for same;

Dim con As OleDbConnection = New OleDbConnection("Provider=Microsoft.jet.oledb.4.0;data source=D:\myDB.mdb")

Dim cmd As OleDbCommand = New OleDbCommand("Select * FROM User", con)

con.Open()

Dim myDA As OleDbDataAdapter = New OleDbDataAdapter(cmd)

Dim myDataSet As DataSet = New DataSet()

myDA.Fill(myDataSet, "MyTable")

DataGridView1.DataSource = myDataSet.Tables("MyTable").DefaultView

con.Close()

http://www.switchonthecode.com/tutorials/csharp-tutorial-binding-a-datagridview-to-a-database

Ananth Christi Vijay replied to Sagar P on 07-Dec-10 04:13 AM
Thanks ..

what is the query to bind the data between two dates to a datagridview frm access database

thanks..
Sagar P replied to Ananth Christi Vijay on 07-Dec-10 05:03 AM
Try this;
declare 2 variable startDate and endDate as string and assign some dates to it. Then use this code to get all data between these 2 dates;

Dim con As OleDbConnection = New OleDbConnection("Provider=Microsoft.jet.oledb.4.0;data source=D:\myDB.mdb")

Dim cmd As OleDbCommand = New OleDbCommand("Select * FROM User where Date<='"+startDate+"' and Date>='"+endDate+"', con)

con.Open()

Dim myDA As OleDbDataAdapter = New OleDbDataAdapter(cmd)

Dim myDataSet As DataSet = New DataSet()

myDA.Fill(myDataSet, "MyTable")

DataGridView1.DataSource = myDataSet.Tables("MyTable").DefaultView

con.Close()

Ananth Christi Vijay replied to Sagar P on 07-Dec-10 05:55 AM

hello Mr.Sagar,

i've written the coding for binding the data in te gridview between two date..

bt no record is displayed in the grid.

Dat mentions the  field date in my database..

con = New OleDbConnection("provider=microsoft.jet.oledb.4.0; data source=C:\Documents and Settings\Admin\My Documents\Visual Studio 2005\Projects\kkss donation\child.mdb")

con.Open()

cmd = New OleDbCommand("select * from child1 where Dat<=? and Dat>=?", con)

cmd.Parameters.AddWithValue("Dat", TextBox1.Text)

cmd.Parameters.AddWithValue("Dat", TextBox2.Text)

Dim myDA As OleDbDataAdapter = New OleDbDataAdapter(cmd)

Dim myDataSet As DataSet = New DataSet()

myDA.Fill(myDataSet, "child1")

DataGridView1.DataSource = myDataSet.Tables("child1").DefaultView

con.Close()

Sagar P replied to Ananth Christi Vijay on 07-Dec-10 06:50 AM
Try this directly without parameters;

con = New OleDbConnection("provider=microsoft.jet.oledb.4.0; data source=C:\Documents and Settings\Admin\My Documents\Visual Studio 2005\Projects\kkss donation\child.mdb")

con.Open()

cmd = New OleDbCommand("select * from child1 where Dat<='"+TextBox1.Text+"' and Dat>='"+TextBox1.Text+"'", con)

Dim myDA As OleDbDataAdapter = New OleDbDataAdapter(cmd)

Dim myDataSet As DataSet = New DataSet()

myDA.Fill(myDataSet, "child1")

DataGridView1.DataSource = myDataSet.Tables("child1").DefaultView

con.Close()