2017-02-17 161 views
1

我有一個datagrid視圖,我導入此數據網格中的Excel值,但我看不到單元格的顏色。這是我的代碼和圖片。VB.Net datagrid導入excel單元格顏色

我怎樣才能將顏色從Excel傳遞到數據網格?

[enter image description here

Dim cmb As New DataGridViewComboBoxColumn() 
    cmb.HeaderText = "Colum1" 
    cmb.Name = "cmb" 
    cmb.Items.Add("Select for Main Plot") 
    cmb.Items.Add("Select") 
    DataGridView1.Columns.Add(cmb) 

    Dim conn As OleDbConnection 
    Dim dta As OleDbDataAdapter 
    Dim dts As DataSet 
    Dim excel As String 
    Dim OpenFileDialog As New OpenFileDialog 

    OpenFileDialog.InitialDirectory = My.Computer.FileSystem.SpecialDirectories.MyDocuments 
    OpenFileDialog.Filter = "All Files (*.*)|*.*|Excel files (*.xlsx)|*.xlsx|CSV Files (*.csv)|*.csv|XLS Files (*.xls)|*xls" 

    If (OpenFileDialog.ShowDialog(Me) = System.Windows.Forms.DialogResult.OK) Then 
    End If 

    Dim fi As New FileInfo(OpenFileDialog.FileName) 
    Dim FileName As String = OpenFileDialog.FileName 

    excel = fi.FullName 
    conn = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + excel + ";Extended Properties=Excel 12.0;") 
    dta = New OleDbDataAdapter("Select * From [Sayfa1$]", conn) 
    dts = New DataSet 
    dta.Fill(dts, "[Sayfa1$]") 
    DataGridView1.DataSource = dts 
    DataGridView1.DataMember = "[Sayfa1$]" 
    conn.Close() 
+0

OleDbConnection有望從您的Excel文件讀取數據,而不是單元格格式並將其傳輸到DataGridView。你自己在這裏。 – Steve

+0

你在這裏提出什麼建議 – Emrah

回答

0

正如史蒂夫指出...這是你必須執行。既然你沒有真正指定着色方案的工作方式,我會假設根據單元格中的值決定單元格應該具有的背景顏色。這是相當直接的,可以通過多種方式實現。

一種選擇是製作一個簡單地循環遍歷DataGridView中所有單元格的方法,並根據單元格的值相應地設置其顏色。但每次單元格值發生更改時都可以使用,您將不得不重新運行該方法。

另一種選擇是將此邏輯放入CellFormatting事件中。這樣,當用數據初始化DataGridView時將應用顏色,並且如果用戶更改單元值,也將改變顏色。

由於您的發佈代碼沒有顯示任何內容,因此您可能需要根據自己的需要調整以下代碼。

Private Sub DataGridView1_CellFormatting(sender As Object, e As DataGridViewCellFormattingEventArgs) Handles DataGridView1.CellFormatting 
    Dim cellValue As String 
    Dim row As DataGridViewRow 
    row = DataGridView1.Rows(e.RowIndex) 
    If (Not row.IsNewRow) Then 
    cellValue = row.Cells(e.ColumnIndex).Value 
    If (cellValue IsNot Nothing) Then 
     row.Cells(e.ColumnIndex).Style.BackColor = GetColor(cellValue) 
    End If ' ignore empty cells 
    End If ' ignore new row 
End Sub 

Private Function GetColor(colorValue As String) As Color 
    Select Case colorValue 
    Case "1" 
     GetColor = Color.FromArgb(245, 129, 109) 
    Case "2" 
     GetColor = Color.FromArgb(251, 129, 113) 
    Case "3" 
     GetColor = Color.FromArgb(252, 155, 119) 
    Case "4" 
     GetColor = Color.FromArgb(250, 186, 119) 
    Case "5" 
     GetColor = Color.FromArgb(253, 208, 130) 
    Case "6" 
     GetColor = Color.FromArgb(253, 236, 129) 
    Case "7" 
     GetColor = Color.FromArgb(215, 224, 130) 
    Case "8" 
     GetColor = Color.FromArgb(178, 213, 130) 
    Case "9" 
     GetColor = Color.FromArgb(139, 201, 128) 
    Case "10" 
     GetColor = Color.FromArgb(100, 189, 122) 
    Case Else 
     GetColor = Color.White 
    End Select 
End Function