Question Displaying result in a datagrid

Meera

Member
Joined
Jun 26, 2010
Messages
11
Programming Experience
Beginner
Hi all,
i am trying to bring a query result as a datagrid. I am new to vb.net.
SQL 2005, Visual studio 2005
I have a sql query to display 3 cols of value. the below is my code
------------------------------------------------------------------------
Private Sub UserControl1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim ConString As String = "Data Source=<>;Initial Catalog='<>';User Id=sa;Password=<>;"
Dim MyCon As New SqlConnection
MyCon.ConnectionString = ConString
Dim DA As New SqlDataAdapter("Select [No_], [Name], [City] from [dbo].[Customer]", MyCon)
Dim DS As New DataSet
DA.Fill(DS, "[dbo].[Customer]")
Dim DataGridView1 As New DataGridView
DataGridView1.DataSource = DS.DefaultViewManager
End Sub

-------------------------------------------------------
Kindly bear with my question.
What i did is at first i dint have a datagrid, drag n dropped in the form.
When executing the code the results not getting populated to the datagrid.
please guide me where im going wrong with the codings?

Many Thanks,
Meera.
 
hi, first you have couple of choice to display your data in the datagrid,
1)Drag data grid on the form and set colums which you want to show,
2)Dynamically create grid and add to your forms and add columns to it.

Once you have grid, you just need to set its data source and it will show your data to the datagrid.
in above code better you add datagrid to your form by drag and drop then just set datasource, let me remind you one more thing, if you have not added any columns then it will show all of the columns of your datasource.
 
Hi, Thanks for your reply.
Could you please explain me how to set columns in the grid?
Also my grid doesn't have any columns , but when debugged no rows or columns are displayed. Kindly let me know how to set the datasource to the grid apart from being with query.

Thanks !!
 
Back
Top