How to give multiple color to cells to give number of values in datagridview in vb2008 Express edition RRS feed

  • Question

  • hai all

    im trying to 3 types of  marks will show different color in datagridview cells for given number of values at runtime.

    How to give multiple color to cells to give number of values  in datagridview in vb2008 Express edition

    Friday, November 16, 2012 12:11 PM


  • Hi

    Not exactly sure what you need, but here is some example code to show how to set a DGV background colour according to an input text value.

    ' new default BLANK form named Form1
    ' replace all code with this code
    Public Class Form1
        Private sc As New SplitContainer
        Private b1 As New Button
        Private DGV As New DataGridView
        Private tb1 As New TextBox
        Private matchColour As Color = Color.Red
        Private unmatchColour As Color = Color.White
        Private Sub Form1_Load(sender As Object, e As System.EventArgs) Handles Me.Load
            ' set up controls on form
            sc.Dock = DockStyle.Fill
            sc.Orientation = Orientation.Horizontal
            sc.SplitterDistance = 210
            sc.FixedPanel = FixedPanel.Panel2
            sc.IsSplitterFixed = True
            tb1.Location = New Point(20, 0)
            b1.Location = New Point(tb1.Right + 10, tb1.Top)
            tb1.Font = New Font("Ariel", 12)
            tb1.BackColor = Color.LightGray
            b1.Text = "Match"
            sc.Panel2.Controls.AddRange({tb1, b1})
            AddHandler b1.Click, AddressOf b1_Click
            DGV.Columns.Add("One", "One")
            DGV.Columns.Add("Two", "Two")
            DGV.Columns.Add("Three", "Three")
            DGV.Columns("Three").AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill
            DGV.Dock = DockStyle.Fill
            ' just dummy data to test code
            DGV.Rows.Add("aaa", "bbb", "ccc")
            DGV.Rows.Add("ddd", "eee", "fff")
            DGV.Rows.Add("aaa", "eee", "ccc")
            DGV.Rows.Add("ddd", "bbb", "fff")
        End Sub
        Private Sub b1_Click(sender As System.Object, e As System.EventArgs)
            ' find matches on button click
            ' match with text as per Textbox text
            For i As Integer = 0 To DGV.RowCount - 1
                Dim r As DataGridViewRow = DGV.Rows(i)
                For Each ii As DataGridViewCell In r.Cells
                    If Not r.IsNewRow Then
                        If ii.Value.ToString.ToLower = tb1.Text.ToLower Then
                            ii.Style.BackColor = matchColour
                            ii.Style.BackColor = unmatchColour
                        End If
                    End If
        End Sub
    End Class

    Regards Les, Livingston, Scotland

    Saturday, November 17, 2012 2:53 PM