0

基本上我想要的是当更改状态并更新它会改变更新单元格的颜色这里是我的代码:

Private Sub Btnupdate_Click(sender As Object, e As EventArgs) Handles btnupdate.Click
    Try
        Dim datePublish As String = Format(dtpDatePublish.Value, "yyyy-MM-dd")

        If txtAccessionNo.Text = "" Or txtAuthor.Text = "" Or txtTitle.Text = "" Or TxtBoxISBN.Text = "" Or txtPublisher.Text = "" Or CBSection.Text = "" Or TxtBoxSubject.Text = "" Or TxtBoxYearPub.Text = "" Or TxtBoxShelf.Text = "" Or TxtBoxCallNumber.Text = "" Then
            MsgBox("All fields are required to be filled up.", MsgBoxStyle.Exclamation)
        Else
            sql = "SELECT * FROM `books` WHERE `AccessionNo` = '" & txtAccessionNo.Text & "'"
            reloadtxt(sql)

            If dt.Rows.Count > 0 Then
                panelstatus.Visible = True
                sqledit = "UPDATE `books` SET `Isbn`='" & TxtBoxISBN.Text & "',
                `BookSection` = '" & CBSection.SelectedItem & "', `Subject` = '" & TxtBoxSubject.Text & "', `Title` = '" & txtTitle.Text & "',
                     `Author` = '" & txtAuthor.Text & "', `JointAuthor` = '" & txtjointauthor.Text & "', `Publisher` = '" & txtPublisher.Text & "', 
                    `YearPublish` = '" & TxtBoxYearPub.Text & "', `Edition` = '" & TxtBoxEdition.Text & "', `Volume` = '" & TxtBoxVolume.Text & "', 
                   `Aquistion` = '" & CBAquisition.SelectedItem & "', `DateAquired` = '" & datePublish & "', `SponsorPrice` = '" & TxtBoxPrice.Text & "',
                   `Shelf` = '" & TxtBoxShelf.Text & "', `CallNumber` = '" & TxtBoxCallNumber.Text & "', `Remarks` = '" & TxtBoxRemarks.Text & "', `Status` = '" & CBstatus.SelectedItem & "' WHERE `AccessionNo` = '" & txtAccessionNo.Text & "'"

                save_or_update(sql, sqladd, sqledit, "Books has been updated in the database.", "New books has been saved in the database.")

                Call InventoryAdd_Load(sender, e)
            End If
        End If

    Catch ex As Exception
        MsgBox(ex.Message)
    End Try

这是我的单元格格式代码

Private Sub DtgList1_CellFormatting(sender As Object, e As DataGridViewCellFormattingEventArgs) Handles dtgList1.CellFormatting
    For Each row As DataGridViewRow In dtgList1.Rows
        If row.Cells(17).Value = "Available" Then
            row.DefaultCellStyle.ForeColor = Color.White
            row.DefaultCellStyle.ForeColor = Color.Red
        End If
    Next
End Sub
4

1 回答 1

0

向 DataGridViews 添加行颜色时,我通常处理 Paint 事件。错误“从字符串“Available”转换为类型“Double”无效表明 row.Cells(17).Value 是 Double,而不是字符串。你确定那是正确的列吗?

从您的代码中,我猜您的列被称为“状态”。如果不是,请根据需要在下面重命名,或继续使用列号,但确保它是正确的列。当您使用 Select * 填充您的表时,我建议使用 Column Name 更可靠,因为列顺序可能会更改或将来添加到数据库中的新列。

Private Sub dtgList1_Paint(sender As Object, e As PaintEventArgs) Handles dtgList1.Paint
    For Each rw As DataGridViewRow In dtgList1.Rows
        If rw.Cells("Status").Value = "Available" Then
            rw.DefaultCellStyle.ForeColor = Color.White
        Else
            rw.DefaultCellStyle.ForeColor = Color.Red
        End If
    Next
End Sub
于 2020-02-13T09:31:23.800 回答