编辑:我有一段代码曾经几乎应该工作,突然间它开始抛出一个dbnull异常(之前没有)。现在,多亏了@david sdot,我得到了程序中的着色代码。
这是工作着色代码
Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
'If Not IsNothing(DataGridView2.Rows("As (Arsen)")) Then
'Kategorier i kolonner (vanlig)
'As
Dim UL1As As Double = 8
Dim UL2As As Double = 20
Dim UL3As As Double = 50
Dim Ul4As As Double = 600
Dim Ul5As As Double = 1000
If Me.DataGridView2.CurrentRow.Cells(0).Value Is DBNull.Value Then
MessageBox.Show("Cellen er tom.")
Else
For i As Integer = 0 To Me.DataGridView2.Rows.Count - 1
If Me.DataGridView2.Rows(i).Cells("As (Arsen)").Value < UL1As Then
Me.DataGridView2.Rows(i).Cells("As (Arsen)").Style.BackColor = Color.DodgerBlue
ElseIf Me.DataGridView2.Rows(i).Cells("As (Arsen)").Value >= UL1As And Me.DataGridView2.Rows(i).Cells("As (Arsen)").Value < UL2As Then
Me.DataGridView2.Rows(i).Cells("As (Arsen)").Style.BackColor = Color.LawnGreen
ElseIf Me.DataGridView2.Rows(i).Cells("As (Arsen)").Value >= UL2As And Me.DataGridView2.Rows(i).Cells("As (Arsen)").Value < UL3As Then
Me.DataGridView2.Rows(i).Cells("As (Arsen)").Style.BackColor = Color.Yellow
ElseIf Me.DataGridView2.Rows(i).Cells("As (Arsen)").Value >= UL3As And Me.DataGridView2.Rows(i).Cells("As (Arsen)").Value < Ul4As Then
Me.DataGridView2.Rows(i).Cells("As (Arsen)").Style.BackColor = Color.Orange
ElseIf Me.DataGridView2.Rows(i).Cells("As (Arsen)").Value >= Ul4As And Me.DataGridView2.Rows(i).Cells("As (Arsen)").Value < Ul5As Then
Me.DataGridView2.Rows(i).Cells("As (Arsen)").Style.BackColor = Color.Red
ElseIf Me.DataGridView2.Rows(i).Cells("As (Arsen)").Value >= Ul5As Then
Me.DataGridView2.Rows(i).Cells("As (Arsen)").Style.BackColor = Color.BlueViolet
End If
Next
End If
End Sub
结果如下:
我也想用彩色导出到Excel,但我无法使用相同的逻辑来处理导出代码。
Private Sub ExportToExcel()
' Creating a Excel object.
Dim excel As Microsoft.Office.Interop.Excel._Application = New Microsoft.Office.Interop.Excel.Application()
Dim workbook As Microsoft.Office.Interop.Excel._Workbook = excel.Workbooks.Add(Type.Missing)
Dim worksheet As Microsoft.Office.Interop.Excel._Worksheet = Nothing
Try
worksheet = workbook.ActiveSheet
worksheet.Name = "ExportedFromDatGrid"
Dim cellRowIndex As Integer = 1
Dim cellColumnIndex As Integer = 1
'Loop through each row and read value from each column.
For i As Integer = 0 To DataGridView2.Rows.Count - 2
For j As Integer = 0 To DataGridView2.Columns.Count - 1
' Excel index starts from 1,1. As first Row would have the Column headers, adding a condition check.
If cellRowIndex = 1 Then
worksheet.Cells(cellRowIndex, cellColumnIndex) = DataGridView2.Columns(j).HeaderText
Else
worksheet.Cells(cellRowIndex, cellColumnIndex) = DataGridView2.Rows(i).Cells(j).Value
End If
cellColumnIndex += 1
Next
cellColumnIndex = 1
cellRowIndex += 1
Next
If Not IsNothing(DataGridView2.Rows("As (Arsen)")) Then
'As
Dim UL1As As Double = 8
Dim UL2As As Double = 20
Dim UL3As As Double = 50
Dim Ul4As As Double = 600
Dim Ul5As As Double = 1000
'Gir nå feilmeldingen 'Conversion from string "As (Arsen)" to type 'Integer' is not valid'
For i As Integer = 0 To DataGridView2.Rows.Count - 2
'For j As Integer = 0 To DataGridView2.Columns.Count - 1
If DataGridView2.Rows(i).Cells("As (Arsen)").Value < UL1As Then
worksheet.Cells(i).Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.DodgerBlue)
ElseIf DataGridView2.Rows(i).Cells("As (Arsen)").Value >= UL1As And Me.DataGridView2.Rows(i).Cells("As (Arsen)").Value < UL2As Then
worksheet.Cells(i).Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.LawnGreen)
ElseIf DataGridView2.Rows(i).Cells("As (Arsen)").Value >= UL2As And Me.DataGridView2.Rows(i).Cells("As (Arsen)").Value < UL3As Then
worksheet.Cells(i).Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Yellow)
ElseIf DataGridView2.Rows(i).Cells("As (Arsen)").Value >= UL3As And Me.DataGridView2.Rows(i).Cells("As (Arsen)").Value < Ul4As Then
worksheet.Cells(i).Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Orange)
ElseIf DataGridView2.Rows(i).Cells("As (Arsen)").Value >= Ul4As And Me.DataGridView2.Rows(i).Cells("As (Arsen)").Value < Ul5As Then
worksheet.Cells(i).Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Red)
ElseIf DataGridView2.Rows(i).Cells("As (Arsen)").Value >= Ul5As Then
worksheet.Cells(i).Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.BlueViolet)
End If
'cellColumnIndex += 1
'Next
cellColumnIndex = 1
cellRowIndex += 1
Next
Else
MessageBox.Show("Cellen er tom")
Exit Sub
End If
'Getting the location And file name of the excel to save from user.
Dim saveDialog As New SaveFileDialog()
saveDialog.Filter = "Excel files (*.xlsx)|*.xlsx|All files (*.*)|*.*"
saveDialog.FilterIndex = 2
If saveDialog.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
workbook.SaveAs(saveDialog.FileName)
MessageBox.Show("Export Successful")
End If
Catch ex As System.Exception
MessageBox.Show(ex.Message)
Finally
excel.Quit()
workbook = Nothing
excel = Nothing
End Try
End Sub
想法,有人吗?
答案 0 :(得分:1)
你的问题在这里:
Me.DataGridView2.Rows(i).Cells("As (Arsen)").Value
单元格期望整数作为索引(如.Cells(1))。并且由于错误状态As (Arsen)
无法转换为整数。
因此,要么您知道Cells索引,要么您必须在第一行中查找它才能找到它。
答案 1 :(得分:0)
此代码解决了问题:
'As
Dim UL1As As Double = 8
Dim UL2As As Double = 20
Dim UL3As As Double = 50
Dim Ul4As As Double = 600
Dim Ul5As As Double = 1000
For i As Integer = 0 To DataGridView2.Rows.Count - 2
For j As Integer = 2 To 2
If DataGridView2.Rows(i).Cells("As (Arsen)").Value < UL1As Then
worksheet.Cells(i + 1, j + 1).Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.DodgerBlue)
ElseIf DataGridView2.Rows(i).Cells("As (Arsen)").Value >= UL1As And Me.DataGridView2.Rows(i).Cells("As (Arsen)").Value < UL2As Then
worksheet.Cells(i + 1, j + 1).Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.LawnGreen)
ElseIf DataGridView2.Rows(i).Cells("As (Arsen)").Value >= UL2As And Me.DataGridView2.Rows(i).Cells("As (Arsen)").Value < UL3As Then
worksheet.Cells(i + 1, j + 1).Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Yellow)
ElseIf DataGridView2.Rows(i).Cells("As (Arsen)").Value >= UL3As And Me.DataGridView2.Rows(i).Cells("As (Arsen)").Value < Ul4As Then
worksheet.Cells(i + 1, j + 1).Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Orange)
ElseIf DataGridView2.Rows(i).Cells("As (Arsen)").Value >= Ul4As And Me.DataGridView2.Rows(i).Cells("As (Arsen)").Value < Ul5As Then
worksheet.Cells(i + 1, j + 1).Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Red)
ElseIf DataGridView2.Rows(i).Cells("As (Arsen)").Value >= Ul5As Then
worksheet.Cells(i + 1, j + 1).Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.BlueViolet)
End If
cellColumnIndex += 1
Next
cellColumnIndex = 1
cellRowIndex += 1
Next
我不得不对j索引进行硬编码以将格式应用于正确的行。讨厌,但它是一个工作的地方。
现在我只需要找出如何在不删除第一个数据行的情况下导出标题行...