Solved

How to highlight the first row in a different color without knowing how many columns beforehand in vb.net

Posted on 2016-11-25
6
47 Views
Last Modified: 2016-11-29
Hi all.

I have a Visual Studio project that displays a datagridview control, I created a button that when clicked, it will export the datagridview control results to Excel. I would like to highlight the first row which is the column headers a different color, but the problem is that the number of columns can vary each time the datagridview control is populated with data. Sometimes there will be 7 columns other times there may be 10 etc.

Below is the code I have so far for exporting to Excel. Thank you in advance for any help!

Private Sub btnExcelExport_Click(sender As Object, e As EventArgs) Handles btnExcelExport.Click

        Dim oXLApp As Excel.Application       'Declare the object variables
        Dim oXLBook As Excel.Workbook
        Dim oXLSheet As Excel.Worksheet

        oXLApp = New Excel.Application  'Create a new instance of Excel
        oXLBook = oXLApp.Workbooks.Add  'Add a new workbook
        oXLSheet = oXLBook.Worksheets(1)  'Work with the first worksheet

        oXLApp.Visible = True               'Show it to the user

        With oXLApp
            Dim Col As DataGridViewColumn
            Dim i As Integer = 1
            For Each Col In DataGridView1.Columns
                .Cells(1, i).Value = Col.HeaderText
                i += 1
            Next
            i = 2
            Dim RowItem As DataGridViewRow
            Dim Cell As DataGridViewCell
            For Each RowItem In DataGridView1.Rows
                Dim j As Integer = 1
                For Each Cell In RowItem.Cells
                    .Cells(i, j).Value = Cell.Value
                    j += 1
                Next
                i += 1
            Next

        End With

        'oXLSheet.Range("P:P").EntireColumn.Delete()

        oXLSheet.UsedRange.Borders.LineStyle = 1
        oXLApp.Columns.AutoFit()

        oXLSheet.Range("A:A").Font.Bold = True

        oXLBook = Nothing       'Disconnect from Excel (let the user take over)
        oXLApp = Nothing
        oXLSheet = Nothing

    End Sub

Open in new window

0
Comment
Question by:printmedia
  • 3
  • 2
6 Comments
 
LVL 49

Expert Comment

by:Rgonzo1971
ID: 41901803
HI,

pls try

For Each Col In DataGridView1.Columns
    .Cells(1, i).Value = Col.HeaderText
    .Cells(1, i).Interior.ColorIndex = 6
    i+= 1
Next

Open in new window

Regards
0
 

Author Comment

by:printmedia
ID: 41901812
I get an error message stating that "Leading . can only appear inside a With statement"
0
 

Author Comment

by:printmedia
ID: 41901815
When I put your code within the "With" statement it created new columns after the ones with data.
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 33

Expert Comment

by:Norie
ID: 41901941
Do you have working code for formatting the headers?
0
 

Author Comment

by:printmedia
ID: 41902308
The code I have is the one I provided in my initial question
0
 
LVL 49

Accepted Solution

by:
Rgonzo1971 earned 500 total points
ID: 41902569
was the new code
Private Sub btnExcelExport_Click(sender As Object, e As EventArgs) Handles btnExcelExport.Click

        Dim oXLApp As Excel.Application       'Declare the object variables
        Dim oXLBook As Excel.Workbook
        Dim oXLSheet As Excel.Worksheet

        oXLApp = New Excel.Application  'Create a new instance of Excel
        oXLBook = oXLApp.Workbooks.Add  'Add a new workbook
        oXLSheet = oXLBook.Worksheets(1)  'Work with the first worksheet

        oXLApp.Visible = True               'Show it to the user

        With oXLApp
            Dim Col As DataGridViewColumn
            Dim i As Integer = 1
            For Each Col In DataGridView1.Columns
                .Cells(1, i).Value = Col.HeaderText
                .Cells(1, i).Interior.ColorIndex = 6
                i += 1
            Next
            i = 2
            Dim RowItem As DataGridViewRow
            Dim Cell As DataGridViewCell
            For Each RowItem In DataGridView1.Rows
                Dim j As Integer = 1
                For Each Cell In RowItem.Cells
                    .Cells(i, j).Value = Cell.Value
                    j += 1
                Next
                i += 1
            Next

        End With

        'oXLSheet.Range("P:P").EntireColumn.Delete()

        oXLSheet.UsedRange.Borders.LineStyle = 1
        oXLApp.Columns.AutoFit()

        oXLSheet.Range("A:A").Font.Bold = True

        oXLBook = Nothing       'Disconnect from Excel (let the user take over)
        oXLApp = Nothing
        oXLSheet = Nothing

    End Sub

Open in new window

0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This Article briefly covers methods of calculating the NPV and IRR variants in Excel as well as the limitations in calculating and interpreting IRR results. Paraphrasing Richard Shockley, author of my favourite finance reference tex…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

825 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