|
-
Dec 25th, 2002, 09:05 PM
#1
Thread Starter
Member
take specific column from dataset
Hi All,
I am selecting all records to dataset from my table.After that i need to select specific column from dataset and to display in datagrid.
How will i do that?
My table has Id,Firstname,LastName,Address,Phone,City,State,Fax and Zip
I want to selelct FirstName,lastname and address from my dataset and display in datagrid.
My code is :
Private Sub ButReport_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButReport.Click
Dim SQLConn As SqlConnection
SQLConn = New SqlConnection()
Dim sConnectionString As String
sConnectionString = "Data Source=Lidya;user id=sa;PWD=;Initial Catalog=pubs;"
SQLConn = New SqlConnection(sConnectionString)
Dim SQLCommand As New SqlCommand("SELECT * FROM addressdb", SQLConn)
Dim SQlAdop As New SqlDataAdapter(SQLCommand)
Dim mydataset As New DataSet("selectAddress")
Try
SQLConn.Open()
SQlAdop.Fill(mydataset, "addressdb")
' Dim rowaddress As System.Data.DataRow
DataGrid1.DataSource = mydataset
Catch Mycxception As Exception
MessageBox.Show(Mycxception.ToString())
Finally
If SQLConn.State = ConnectionState.Open Then
SQLConn.Close()
End If
If Not mydataset Is Nothing Then
mydataset = Nothing
End If
End Try
End Sub
Please help me out.
Thanks.
-
Dec 26th, 2002, 12:46 AM
#2
Frenzied Member
you can do it easy at design time. Add a datagrid to your form and then set its datasource to your dataset. Then point to TableStyle collection and in the dialog box set the 'Mapping Name' to the dataset table you want to use. Close it and then reopen it and then point to GridColumnStyle and open the collection dialog. There you can add DatagridTextBoxColumn or DatagridBoolColumn and set mapping name for each. Here you can set the column header, their style and font and size.
All thses can be done via code at run time too.You may look at the automatically generated code and learn how to do it via code.
-
Dec 26th, 2002, 01:07 AM
#3
Member
what you need to do is just change your query command as follow
Dim SQLCommand As New SqlCommand("SELECT Firstname, Lastname, Address FROM addressdb", SQLConn)
-
Dec 26th, 2002, 08:15 AM
#4
Thread Starter
Member
i want make it at run time
Hi all,
Thanks for helping me But....
I want to create at run time.Please help me out.
Thanks.
-
Dec 26th, 2002, 08:14 PM
#5
Member
yup! you can change it at runtime....
Private Sub ButReport_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButReport.Click
Dim SQLConn As SqlConnection
SQLConn = New SqlConnection()
Dim sConnectionString As String
sConnectionString = "Data Source=Lidya;user id=sa;PWD=;Initial Catalog=pubs;"
SQLConn = New SqlConnection(sConnectionString)
Dim SQLCommand As New SqlCommand("SELECT Firstname, Lastname, Address FROM addressdb", SQLConn)
Dim SQlAdop As New SqlDataAdapter(SQLCommand)
Dim mydataset As New DataSet("selectAddress")
Try
SQLConn.Open()
SQlAdop.Fill(mydataset, "addressdb")
' Dim rowaddress As System.Data.DataRow
DataGrid1.DataSource = mydataset
Catch Mycxception As Exception
MessageBox.Show(Mycxception.ToString())
Finally
If SQLConn.State = ConnectionState.Open Then
SQLConn.Close()
End If
If Not mydataset Is Nothing Then
mydataset = Nothing
End If
End Try
End Sub
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|