Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

set specific columns in a datagridview 1

Status
Not open for further replies.

Pampers

Technical User
Apr 7, 2004
1,300
AN
Hi everyone,
I executed a stored procedure to fill a datatable (function 1). Then I bind the table to a datagrid that shows on a form (function 2). All is ok...

But how can I limit the amount of columns in the datagridview. Not is showing all the fields the sp retrieves. Say I just want to see two columns (say: ContractNo & Balance), how can I achieve this (without adjusting the sp)?


Code:
    Private Function GetBalance(ByVal ContractNo As String) As DataTable
        'Function (1)
        'Execute Stored Procedure, Fill DataTable
        '
        Try

            NewConnection()
            OpenConnection()

            strSQL = ("uspBalanceGet")
            InitializeCommand()
            AddParameter("@ContractNo", OleDbType.VarChar, 50, ContractNo)

            objDataTable = New DataTable("tblHkContractPay")
            objDataAdapter = New OleDbDataAdapter(objCommand)
            objDataAdapter.Fill(objDataTable)

            GetBalance = objDataTable

        Catch ex As Exception
            MsgBox(ex.ToString)
            Return Nothing

        End Try


    End Function

Code:
    Private Sub BindBalanceData()
        'Function (2)
        'Bind DataTable to DataGrid
        '
        Try
            GetBalance(cboContract.Text)
            Me.dtgBalance.DataSource = objDataTable

        Catch ex As Exception
            MsgBox(ex.ToString)

        Finally
            'Clean Up
            objCommand = Nothing
            objDataTable = Nothing
            objDataAdapter = Nothing

        End Try
    End Sub






Pampers [afro]
Keeping it simple can be complicated
 
Because you're binding the datasource of the object to the stored procedure, you can't (unless they changed the code for the grid control). When I ran into this problem a long time ago, I switched to a 3rd party control that gave me more control.

With that said, there are a couple of "work arounds"

1. Switch the SP to a function so you can do "select column1, column2 from function".

2. Perform manual manipulation of the table before binding it (delete the columns from the table that you don't want). If you do this, i would recommend using a second table in case you need to get all columns later in the code...that way you don't have to query the DB again.
 
Tnx for the reply macleod1021. I get the picture. Any recommandations on a third part control in this area...

Pampers [afro]
Keeping it simple can be complicated
 
Just a little find.

The datagridview may not be so flexible, the datagrid instead is... You can set it up like you want, fe:

Code:
        'Step 1: Create a DataGridTableStyle 

        'Set mappingname to table.
        Dim tblStyle As New DataGridTableStyle()
        tblStyle.MappingName = "tblContract"

        'Step 2: Create DataGridColumnStyle for each col 
        'ContractNo
        Dim colContractNo As New DataGridTextBoxColumn()
        colContractNo.MappingName = "ContractNo"
        colContractNo.HeaderText = "ContractNo"
        colContractNo.Width = 50
        tblStyle.GridColumnStyles.Add(colContractNo)

        'Step 3: Add style to datagrid
        Me.DataGrid1.TableStyles.Add(tblStyle)

and so on.
Thanx to Sridhar Manoharan for sharing his code on this subject.


Pampers [afro]
Keeping it simple can be complicated
 

or...

Code:
        'Step 2: Create DataGridColumnStyle for each col 
        'ContractNo
        Dim colContractNo As New DataGridTextBoxColumn()
        With colContractNo
            .MappingName = "ContractNo"
            .HeaderText = "ContractNo"
            .Width = 50
        End With

Pampers [afro]
Keeping it simple can be complicated
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top