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
30 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 48

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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
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 48

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

A little background as to how I came to I design this code: Around 5 years ago I designed an add-in that formatted Excel files to a corporate standard, applying different cell colours and font type depending on whether the cells contained inputs,…
This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
Viewers will learn the basics of slicers and timelines for both PivotTables and standard Excel tables in Excel 2013.
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.

707 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

18 Experts available now in Live!

Get 1:1 Help Now