Datagridview avoir le Backcolor d'une cellule

Résolu
bdl20042000 Messages postés 63 Date d'inscription mercredi 18 août 2004 Statut Membre Dernière intervention 19 mai 2009 - 18 mai 2009 à 09:24
bdl20042000 Messages postés 63 Date d'inscription mercredi 18 août 2004 Statut Membre Dernière intervention 19 mai 2009 - 19 mai 2009 à 13:38
Bonjour,
je suis sous VS 2008 SP1 et Windows XP SP2.
J'ai apporté une mise en forme sur des cellules de la datagridview - jusqu'à là tout va bien.
C'est une gestion de planning.
Si une personne est en congé ce jour là, je mets la couleur Rouge dans cette cellule.

Je veux ensuite exporter rapidement le dgv vers Excel en gardant le mise en forme.
Cependant :
For Each cell As DataGridViewCell In DataGridView1.Rows(i).Cell
  cell.Style.BackColor.ToString ' -> m'affiche toujours "Color [Empty]"
Next

Dans le hearder, c'est DataGridView1.Columns(0).defaultcellstyle.backcolor et j'ai la couleur.
Mais peut être que c'est autre chose pour Cell ?

merci pour tout aide que quelqu'un pourrait m'apporter.

1 réponse

bdl20042000 Messages postés 63 Date d'inscription mercredi 18 août 2004 Statut Membre Dernière intervention 19 mai 2009
19 mai 2009 à 13:38
bonjour,
désolé, mais à priori cela fonctionne maintenant chez moi.
Voici le code qui permet d'exporter un datagridview vers excel en conservant les valeurs et la couleur de la cellule :
Imports Microsoft.Office.Interop
Imports System.Text

Public Sub ExportDgv2ExcelAvecMiseEnPage(ByVal DataGridView1 As DataGridView)
    ' Merci à http://www.vbfrance.com/codes/DATADRID-VERS-HTML_45079.aspx donc je me suis inspirer
                      
        Dim html As New StringBuilder("")

        'En tête html
        html.AppendLine("<html><center>\")
        html.AppendLine(\"<table align=center border=1 >----
\") ' gris clair

        Dim nbrLigne As Int32 = DataGridView1.Rows.Count - 1
        Dim nbrColonne As Int32 = DataGridView1.Columns.Count - 1
        Dim couleur As String
        Dim j As Int32

        If nbrColonne >= 255 Or nbrLigne >= 65535 Then
            If MessageBox.Show(\"Attention vous dépassez peut être les limites d'Excel !\" & vbCrLf & \"Voulez vous continuer ?\", \"Attention\", MessageBoxButtons.YesNo) = vbNo Then Exit Sub
        End If

        ' Nom des champs
        For i As Int32 = 0 To nbrColonne
            j = InStr(DataGridView1.Columns(i).DefaultCellStyle.BackColor.ToString, \"[\")
            couleur = Mid(DataGridView1.Columns(i).DefaultCellStyle.BackColor.ToString, j + 1, Len(DataGridView1.Columns(i).DefaultCellStyle.BackColor.ToString) - j - 1)
            html.AppendLine(\"" _
                            + DataGridView1.Columns(i).HeaderText + ", \")
        Next

        If nbrLigne > -1 Then
            For i As Int32 = 0 To nbrLigne
                ' Remplissage des lignes
                html.AppendLine(\"----
\")
                For Each cell As DataGridViewCell In DataGridView1.Rows(i).Cells
                    j = InStr(cell.Style.BackColor.ToString, \"[\")
                    couleur = Mid(cell.Style.BackColor.ToString, j + 1, Len(cell.Style.BackColor.ToString) - j - 1)
                    If couleur <> \"Empty\" Then
                            html.AppendLine(\"" + ", \")
                    Else
                            html.AppendLine(\", \")
                    End If
                Next
            Next
        End If
        html.AppendLine("
</center></html>")
        Clipboard.SetText(html.ToString)
        'Variables for Excel.
        Dim xlApp As New Excel.Application
        Dim xlWBook As Excel.Workbook = xlApp.Workbooks.Add( _
                                        Excel.XlWBATemplate.xlWBATWorksheet)
        Dim xlWSheet As Excel.Worksheet = CType(xlWBook.Worksheets(1), Excel.Worksheet)
        Dim xlCalc As Excel.XlCalculation

        'Save the present setting for Excel's calculation mode and turn it off.
        With xlApp
            xlCalc = .Calculation
            .Calculation = Excel.XlCalculation.xlCalculationManual
        End With
        With xlWSheet
            .Paste()
            .UsedRange.Columns.AutoFit()
        End With

        With xlApp
            .Visible = True
            .UserControl = True
            'Restore the calculation mode.
            .Calculation = xlCalc
        End With

        'Relase objects from memory.                        
        xlWSheet = Nothing
        xlWBook = Nothing
        xlApp = Nothing
        GC.Collect()
    End Sub
3
Rejoignez-nous