单击时将backcolor设置为datagridview按钮不能正常工作

时间:2014-04-21 17:25:10

标签: vb.net datagrid

我的程序只有一半工作。我有以下事件:

Private Sub handleItemButton(ByVal sender As Object, ByVal e As EventArgs)

    'Local variables
    Dim btnItemButton As InventoryItemButton = CType(sender, InventoryItemButton)
    Dim StrItemName As String = btnItemButton.Text
    Dim strQty As String = "1"
    Dim dblQty As Double = CDbl(strQty)
    Dim table As DataTable = ItemInventoryControlPriceTableAdapter.GetData(StrItemName)
    Dim dblPrice As Double = CDbl(table.Rows(0)("Price"))
    Dim dblTotal As Double = dblPrice * dblQty
    Dim row As Integer = grdNewInvoice.Rows.Count



    grdNewInvoice.Rows.Add(StrItemName, strQty, dblPrice, dblTotal, "Remove")

    If grdNewInvoice.CurrentRow.IsNewRow Then

    Dim r As DataGridViewButtonCell = CType(grdNewInvoice.Rows(0).Cells(4), DataGridViewButtonCell)

    With r

        If Not grdNewInvoice.CurrentRow.IsNewRow Then

            .FlatStyle = FlatStyle.Flat
            .Style.ForeColor = System.Drawing.Color.White
            .Style.BackColor = System.Drawing.Color.RoyalBlue

        End If

    End With

    For index As Integer = 0 To grdNewInvoice.RowCount - 1

        dblQty = Convert.ToDouble(grdNewInvoice.Rows(index).Cells(1).Value)
        dblPrice = Convert.ToDouble(grdNewInvoice.Rows(index).Cells(2).Value)
        grdNewInvoice.Rows(index).Cells(3).Value = dblPrice * dblQty

    Next


End Sub

该过程使用某些信息向我的数据网格添加一行。 Cell 4是我的数据网格上的一个按钮单元,我想将背景颜色更改为RoyalBlue。我遇到的问题是它只适用于添加的第一行。之后添加的所有行都将恢复为标准颜色。

我在哪里错了?

0 个答案:

没有答案