Results 1 to 5 of 5

Thread: take specific column from dataset

  1. #1

    Thread Starter
    Member
    Join Date
    Nov 2002
    Posts
    61

    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.

  2. #2
    Frenzied Member
    Join Date
    Oct 2002
    Location
    Gammapolis
    Posts
    1,474
    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.

  3. #3
    Member
    Join Date
    May 2002
    Location
    Malaysia
    Posts
    45
    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)

  4. #4

    Thread Starter
    Member
    Join Date
    Nov 2002
    Posts
    61

    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.

  5. #5
    Member
    Join Date
    May 2002
    Location
    Malaysia
    Posts
    45
    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
  •  



Click Here to Expand Forum to Full Width