Getting the information from Column Header in a Datagrid

Posted on 2006-03-27
Last Modified: 2010-04-23
I have a datagrid that has been populated from a dataset.

I need to be able to loop though the grid and get the data from it.... i have the following working code....

        For lngRow = 0 To dgData.VisibleRowCount - 1

            For lngCol = 0 To dgData.VisibleColumnCount - 1

                strData = strData & dgData.Item(lngRow, lngCol)
                If lngCol < dgData.VisibleColumnCount - 1 Then strData = strData & vbTab

            Next lngCol
            strData = strData & vbCrLf
        Next lngRow

This loops through each row and each column and adds the data to a string - however it only loops though the datagrid starting with the first record - it does not include the Field Names (columns headings)

can anyone tell me how to get the details of the column headings please???

Question by:CraigBroadman
    LVL 96

    Expert Comment

    by:Bob Learned
    You can get the column names from the DataTable:

    Dim columnName As String = table.Columns(lngCol).ColumnName


    Author Comment

    I am not actually used a datatable - I am using a data adapter and a dataset.... see below.....

                daReport = New SqlClient.SqlDataAdapter(SQLStatement, SQLConn)
            Catch SQLEx As Exception
            End Try

                daReport.Fill(objReportDataSet, "ReportData")
                dgData.DataSource = objReportDataSet.Tables("ReportData")
            Catch ex As Exception
            End Try
    LVL 96

    Accepted Solution

    Yes, but a DataSet is made up of multiple DataTables.

    Dim table As DataTable = DataSet1.Tables("Table name goes here")
    Dim columnName As String = table.Columns(lngCol).ColumnName


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
    Introduction When many people think of the WebBrowser ( control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
    To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…
    This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor ( If you're looking for how to monitor bandwidth using netflow or packet s…

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now