About Binding a DataGridView Control with Dataset in Windows Vb.net Application!
1. Create a new windows Form Application
2. Drag and drop a datagirdview control on the form
3. Double click on the form to open a code window. and write the below code in the load event of the form:
Private Sub frmJVMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
' here will be the code.
5. Create a new object of SQL connection. Here is the code
dim cn as new SqlConnection
Dim strConnection as String
strConnection = "Data Source=SQLServer;Initial Catalog=DatabaseName;UID=sa;PWD="
cn.ConnectionString = strConnecton
6. In the next step we are going to define three variables
Dim cmd As New SqlCommand
Dim Da As New SqlDataAdapter
Dim Ds As New DataSet
cmd.commandtext = "Select * from myTable"
cmd.commandType = CommandType.Text
cmd.Connection = cn
Da.SelectCommand = cmd
'Fill a dataset
'When attach with datasource it will atuomatically fill datagridview.
DataGridView1.DataSource = Ds.Tables("myTable").DefaultView
7. Compile and run the application.
You are done. Please write me if you have any qeustions.
What if the query has a join. Which table should be included in "DataGridView1.DataSource = Ds.Tables("myTable").DefaultView" ?