exception:从string“<text>”转换为“整数”types无效

编辑:我有一段代码曾经几乎像它应该,突然间,它开始抛出一个dbnullexception(这是以前没有)。 现在,感谢@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 

结果如下所示:

彩色的celles

我也想用颜色导出到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 

想法,任何人?

你的问题在这里:

 Me.DataGridView2.Rows(i).Cells("As (Arsen)").Value 

单元格期望作为索引的整数(如.Cells(1))。 而作为错误状态As (Arsen)不能转换为整数。

所以要么你知道单元格索引,要么你必须在第一行find它。

这段代码解决了这个问题:

  '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索引进行硬编码才能将格式应用到正确的行。 讨厌,但这是一个工作的地方。

现在我只需要找出如何导出标题行而不删除第一个数据行…