showing table from sql server in datagrid control

urvi_bhal

Member
Joined
Jun 29, 2005
Messages
14
Programming Experience
Beginner
hi,

I am using datagrid control and showing data from sql server at runtime.
Creating data adapter and dataset at runtime than assiging dataset to datagrid. datasource property. but when it is showing datagrid with data it is showing datagrid with table name and when i click on table name it is showing data in column format.
I want to see if data can be shown in column format instead of table name.
 
code for datagrid

VB.NET:
Dim dap As New SqlDataAdapter() /*Global variable
Dim ds As DataSet /*Global variable
/* cm1 is sqlcommand
dap.SelectCommand = cm1
ds = New DataSet()
dap.Fill(ds)
DataGrid1.DataSource = ds
DataGrid1.Expand(0)
DataGrid1.ReadOnly = True
 
Specify the table you wish to be the datasource in your datasource statement.

DataGrid1.DataSource = ds.Tables(0)

You should get into the habit of naming your tables on the .Fill method, that way you can refer to them by TableName.

Example:

dap.Fill(ds,"TableNameHere")
Datagrid1.Datasource = ds.Tables("TableNameHere")

Would be much easier to keep track of different tables if you had several in your dataset. Otherwise, you can refer to them by index like in the first example.

Hope that helps
Bloks
 
Back
Top