none
How do I change a CELL BG color? RRS feed

  • Question

  • How do I change a CELL BG color?

    1) User clicks a Cell on the table
    Name:  EVC_Kiosk_CellColor_1.jpg
Views: 13
Size:  31.3 KB
    2) Then clicks a button called "Color"
    3) A color window pops up
    Name:  EVC_Kiosk_CellColor_2.JPG
Views: 13
Size:  32.4 KB
    4) User choose Color and clicks OK
    5) Color window closes
    6) The Cell BG color changes according to user's choice

    Any advise? I can't figure out how the program knows which Cell is being Selected.

    Something like this is all I got, but I need more help.

    ProjectsDataGridView.Rows(e.RowIndex).DefaultCellStyle.BackColor = Color.FromArgb(CInt(colorArray(0)))

    Saturday, April 27, 2019 11:29 AM

All replies

  • Hi

    Here is a stand alone example project. If you want to try it, you will need to put a default DataGridView1 and a default Button1 onto Form1 in the Designer.

    This shows the use of a function toreturn the user choice of colour, and the selection of which cell to set the background colour for.

    NOTE: this is only oneofmany ways to get it done.

    This example is VERY basic and has no exception handling etc.

    All the code in the Load event is just to set up the DGV and some test data.

    Option Strict On
    Option Explicit On
    Public Class Form1
        Dim r As New Random
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            With DataGridView1
                .Columns.Add("ID", "ID")
                .Columns.Add("Date of Entry", "Date of Entry")
                .Columns.Add("PO", "PO")
                .Columns.Add("Site", "Site")
                .Columns.Add("Client", "Client")
                .Columns.Add("Description", "Description")
                .Columns.Add("Lead Time", "Lead Time")
                .Columns.Add("Job Status", "Job Status")
    
                For i As Integer = 125 To 135
                    .Rows.Add(i.ToString, r.Next(1, 28).ToString & " Apr 2019 1:50PM", Nothing, Nothing, Nothing, "Oh shit", r.Next(1, 28).ToString & " Apr 2019 1:50PM", "Confirmed")
                Next
            End With
    
        End Sub
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            DataGridView1("Description", DataGridView1.CurrentRow.Index).Style.BackColor = ChooseColour(DataGridView1("Description", DataGridView1.CurrentRow.Index).Style.BackColor)
        End Sub
        Public Function ChooseColour(ByVal col As Color) As Color
            Dim c As New ColorDialog()
            c.AllowFullOpen = True
            c.AnyColor = True
            c.FullOpen = True
            c.ShowHelp = False
            c.Color = col
            Dim result As DialogResult = c.ShowDialog()
            If result = Windows.Forms.DialogResult.OK Then
                Return c.Color
            Else
                Return col
            End If
        End Function
    End Class


    Regards Les, Livingston, Scotland

    Saturday, April 27, 2019 12:27 PM
  • DataGridView1("Description", DataGridView1.CurrentRow.Index).Style.BackColor = ChooseColour(DataGridView1("Description", DataGridView1.CurrentRow.Index).Style.BackColor)

    Doesn't this highlight the whole row?

    I just want the Cell which user selected to have color changed.

    Also, how do I GET the index of the selected Cell (Row and Column)?

    I need to store the index of the highlighted Cell in variable.

    Something like:

    rowIndex = ProjectsDataGridView.CurrentRow
    colIndex = ProjectsDataGridView.CurrentCol

    Sunday, April 28, 2019 8:47 AM
  • Hi

    So you didn't try my suggestion. If you had, you wouldn;t have needed that second question.


    Regards Les, Livingston, Scotland

    Sunday, April 28, 2019 12:19 PM
  • DataGridView1("Description", DataGridView1.CurrentRow.Index).Style.BackColor = ChooseColour(DataGridView1("Description", DataGridView1.CurrentRow.Index).Style.BackColor)

    But the user could have chosen any Columns, instead of "Description".

    I don't see any "CurrentColumn" function

    Sunday, April 28, 2019 2:39 PM
  • Hi

    OK, understandable. Try changing the Button1 event code to this.

        Private Sub Button1_Click_1(sender As Object, e As EventArgs) Handles Button1.Click
            DataGridView1.CurrentCell.Style.BackColor = ChooseColour(DataGridView1.CurrentCell.Style.BackColor)
        End Sub
    


    Regards Les, Livingston, Scotland

    Sunday, April 28, 2019 2:57 PM
  • OK, Thanks
    Tuesday, April 30, 2019 9:58 AM
  • Hi,

    If you have got your solution,we appreciated you shared us your solution and mark it as an answer.

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, May 1, 2019 1:26 AM