Binding a DataGridView Control with Dataset in Windows Vb.net Application!
I am writing this for the users who are new in vb.net windows application. After reading this you will be able to display your data from a Sql Server Database to datagridview control. Don't be panic it is very easy and you don't have to wirte hundreds lines of code to do this very simple job. Take a long breath now we are going to start. Learn Binding a DataGridView Control with Dataset in Windows Vb.net Application and its code
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.
End Sub
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
Da.Fill(Ds, "myTable")
'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.
Anwar
What if the query has a join. Which table should be included in "DataGridView1.DataSource = Ds.Tables("myTable").DefaultView" ?