2017-01-11 38 views
0

我已將數據從數據庫加載到datagridview,並具有兩列目標值和卷,其中Item單元> Item最小值表示卷單元應呈綠色,項目最小值<項目單元那麼音量應該是紅色。我嘗試過但我無法做到。在條件vb.net上更改datagridview行顏色

進口System.Data.SqlClient的

公共類view_all

Dim con1 As SqlConnection 

Dim con As String 

Dim da As SqlDataAdapter 

Dim ds As DataSet 

Private Sub view_all_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 

    Dim obj As New config 

    con1 = New SqlConnection 

    con1 = obj.getcontion() 

    con1.Open() 



    da = New SqlDataAdapter("select * from item", con1) 

    ds = New DataSet 

    da.Fill(ds, "item") 

    DataGridView1.DataSource = ds 

    DataGridView1.DataMember = "item" 



    DataGridView1.Columns(0).HeaderText = "Item Id" 
    DataGridView1.Columns(1).HeaderText = "Item Name" 
    DataGridView1.Columns(2).HeaderText = "Item Number" 
    DataGridView1.Columns(3).HeaderText = "Item Propose" 
    DataGridView1.Columns(4).HeaderText = "Item Unit " 
    DataGridView1.Columns(5).HeaderText = "Item Max" 
    DataGridView1.Columns(6).HeaderText = "Item min" 

End Sub 

回答

0

所以你需要做的是抓住列索引,然後通過所有這意味着,通過在所有的細胞去行移動指定的列。

Dim intR As Integer = Maintt.DataGridView1.RowCount - 1 
Dim jr as integer ' for the rows 
    For jR = 1 To intR 
'Put condition here 
    Maintt.DataGridView1.Item(4, jR).Style.BackColor = Color.LightGreen 
    Next 

這將是你的第4列 現在你都在此列在您的條件成立,着色爲綠色的細胞。 再次通過此列,找到背景爲綠色的單元格, 獲取行索引並將此行中的所有單元格都變爲綠色。