locked
Change font color of datagridview selected cells RRS feed

  • Question

  • Hi,

    I am trying to change the font color of cell(s) that are selected by the user. So, the user highlights the cells, then presses a button to change the font color. I tried using datagridview1.selectedcells, but couldn't figure it out. Thanks in advance!

    Monday, January 9, 2012 5:55 AM

Answers

  • My apologies then. I didn't think the answers to my questions were that complex. I am a beginner when it comes to vb. Fortunately I have just had a lot of success finding answers my questions in this forum. Even if I don't receive the exact code for what I am trying to accomplish (which I rarely do) Any direction I am given is usually quite helpful.

    Sonneh,

    Welcome to the forum.  Please do not be put off by the first such reply as you received that prompted you to feel the need to apologize.  Your question is not that complex.  And since you indicated you are willing to work off of a suggestion course of action, you are more ambitious than many who look for cut and paste answers.

    The example shown below creates an array of colors that you you will need to replace for your needs.  What you will need to do is create an array with length equal to the number of rows in your datagridview.  I just used a shortcut for this example that will create an array of colors.  Do not use this array for your application as it is just a subset of the available colors.

    The biggest problem I see that you face will be to determine a way to select colors that are different yet readable against the backcolor.  The Button click event handler has the logic you will need to set the ForeColor based on the cell's RowIndex property.  I showed you the events on the DataGridView that you will need to handle for updating your color array. 

    It's up to you to fill in the gaps.  Feel; free to ask questions if you need further guidance.

     

    Public Class Form1
       ' Get all the values from the KnownColor enumeration.
       Dim colorsArray As System.Array = _
         [Enum].GetValues(GetType(KnownColor))
       Dim allColors(colorsArray.Length) As KnownColor
       Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
           Array.Copy(colorsArray, allColors, colorsArray.Length)
       End Sub


       Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
          For Each cell As DataGridViewCell In DataGridView1.SelectedCells
             cell.Style.ForeColor = Color.FromName(allColors(cell.RowIndex).ToString)
          Next
       End Sub

       Private Sub UpdateColorArray(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewRowsAddedEventArgs) Handles DataGridView1.RowsAdded
          UpdateColorArray()
       End Sub

       Private Sub DataGridView1_RowsRemoved(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewRowsRemovedEventArgs) Handles DataGridView1.RowsRemoved
          UpdateColorArray()
       End Sub

       Sub UpDateColorArray()
          'Expend your color array
       End Sub
    End Class

     


    • Edited by TnTinMN Monday, January 9, 2012 7:25 AM typo
    • Marked as answer by Sonneh Sunday, February 12, 2012 7:46 PM
    Monday, January 9, 2012 7:17 AM

All replies

  • Hi, Try these forum answered links. http://social.msdn.microsoft.com/Forums/en-US/vbgeneral/thread/18f3ef8a-4977-4cf3-93b0-ff615377470e http://social.msdn.microsoft.com/Forums/en-US/vbgeneral/thread/adb50122-9b53-4b60-9ec7-9c8c030522c8 If a post answers your question, please click Mark As Answer on that post and Mark as Helpful. Happy Coding...
    • Edited by Ragu.R Monday, January 9, 2012 6:11 AM
    Monday, January 9, 2012 6:10 AM
  • Ok, so far, the if statement from the first link was pretty close to what I want, except rather than selected cell, can I do the same but with a range of cells. I want the user to be able to select a range of cells from many columns and rows and change their font color all at the same time by clicking the button. 

    Just a future question I will have: The datagridview is connected to an access database. so the font color changes will have to be unique to each record in the database. so if the user changes the font color of customer first and last name to red, when they navigate to the next record, it will remain the default color. How would i accomplish this? even if you could point me in the right direction i would appreciate it!

    • Edited by Sonneh Monday, January 9, 2012 6:32 AM
    Monday, January 9, 2012 6:29 AM
  • Sonneh,

    Yea but that you have to do program and because it is an uncommon wish you have to do that yourself, the chance that somebody has done that before you is low, and even lower is the chance that somebody who did that sees your question and want to give the code.

    Be aware this kind of wishes are mostly only a bunch of work and a lot of code and not a pleasant thing to do and almost impossible to show in sample code in a forum or on a website.

    So start if you really want what you wish with coding.

     


    Success
    Cor
    Monday, January 9, 2012 6:38 AM
  • My apologies then. I didn't think the answers to my questions were that complex. I am a beginner when it comes to vb. Fortunately I have just had a lot of success finding answers my questions in this forum. Even if I don't receive the exact code for what I am trying to accomplish (which I rarely do) Any direction I am given is usually quite helpful.
    • Edited by Sonneh Monday, January 9, 2012 6:48 AM
    Monday, January 9, 2012 6:46 AM
  • My apologies then. I didn't think the answers to my questions were that complex. I am a beginner when it comes to vb. Fortunately I have just had a lot of success finding answers my questions in this forum. Even if I don't receive the exact code for what I am trying to accomplish (which I rarely do) Any direction I am given is usually quite helpful.

    Sonneh,

    Welcome to the forum.  Please do not be put off by the first such reply as you received that prompted you to feel the need to apologize.  Your question is not that complex.  And since you indicated you are willing to work off of a suggestion course of action, you are more ambitious than many who look for cut and paste answers.

    The example shown below creates an array of colors that you you will need to replace for your needs.  What you will need to do is create an array with length equal to the number of rows in your datagridview.  I just used a shortcut for this example that will create an array of colors.  Do not use this array for your application as it is just a subset of the available colors.

    The biggest problem I see that you face will be to determine a way to select colors that are different yet readable against the backcolor.  The Button click event handler has the logic you will need to set the ForeColor based on the cell's RowIndex property.  I showed you the events on the DataGridView that you will need to handle for updating your color array. 

    It's up to you to fill in the gaps.  Feel; free to ask questions if you need further guidance.

     

    Public Class Form1
       ' Get all the values from the KnownColor enumeration.
       Dim colorsArray As System.Array = _
         [Enum].GetValues(GetType(KnownColor))
       Dim allColors(colorsArray.Length) As KnownColor
       Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
           Array.Copy(colorsArray, allColors, colorsArray.Length)
       End Sub


       Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
          For Each cell As DataGridViewCell In DataGridView1.SelectedCells
             cell.Style.ForeColor = Color.FromName(allColors(cell.RowIndex).ToString)
          Next
       End Sub

       Private Sub UpdateColorArray(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewRowsAddedEventArgs) Handles DataGridView1.RowsAdded
          UpdateColorArray()
       End Sub

       Private Sub DataGridView1_RowsRemoved(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewRowsRemovedEventArgs) Handles DataGridView1.RowsRemoved
          UpdateColorArray()
       End Sub

       Sub UpDateColorArray()
          'Expend your color array
       End Sub
    End Class

     


    • Edited by TnTinMN Monday, January 9, 2012 7:25 AM typo
    • Marked as answer by Sonneh Sunday, February 12, 2012 7:46 PM
    Monday, January 9, 2012 7:17 AM
  • TnTinMin,

    If you think you know it better than me, no problem. But if you want to criticize my replies, then give a complete sample, not something which only creates an array of colors.

    What the user asks is: give the end user control over all font colors in all the cells of a datagridview and keep those settings at the next start otherwise it is senseless. 

    Not a simple handle the array of colors, that probably every beginner can do and has than nothing which he can use.

    So if it is not complex, than show that easy piece of code which solves the problem.

    I try to help the OP in a way that he spent his time with better things so that when he has enough knowledge will fulfil or not what he wants. It seems that your purpose here is the opposite beside of course to show that you know it better.

    Therefore I'm also curious for your code which solves the problem, not this sample how to setup an array of colors. 

    By the way, the style of your code looks much VB6 if you want currently an own way a collection of used colors there are better solutions.

    Be aware that if you had placed your message without flaming me I would not even have given a reaction.

     

     

     


    Success
    Cor

    Monday, January 9, 2012 7:51 AM
  • TnTinMin,

    If you think you know it better than me, no problem. But if you want to criticize my replies, then give a complete sample, not something which only creates an array of colors.

    What the user asks is: give the end user control over all font colors in all the cells of a datagridview and keep those settings at the next start otherwise it is senseless. 

    Not a simple handle the array of colors, that probably every beginner can do and has than nothing which he can use.

    So if it is not complex, than show that easy piece of code which solves the problem.

    I try to help the OP in a way that he spent his time with better things so that when he has enough knowledge will fulfil or not what he wants. It seems that your purpose here is the opposite beside of course to show that you know it better.

    Therefore I'm also curious for your code which solves the problem, not this sample how to setup an array of colors. 

    By the way, the style of your code looks much VB6 if you want currently an own way a collection of used colors there are better solutions.

    Be aware that if you had placed your message without flaming me I would not even have given a reaction.

     

     

     


    Success
    Cor

    Cor,

    You seem to have some sever emotional problems,  among which is a persecution complex.  In Reed's thread on Trolling, you attacked him for a general comment on inappropriate behavior in which you correctly recognized  a description of your abusive nature.  In your normal behavior pattern, you then deleted the post so that any comments about your temper tantrum would look misplaced. You then continued to try an dissuade him from finding a way to dealing with trolling and other bad behavior on the forum.  Are you afraid that if he was successful, that you would easily be identified as a problem and be dealt with appropriately?

    Here you attack the original poster's question without offering anything of substance.  When I provide some of the guidance that the OP requested, you attack my response and say it is inadequate because I did not write an  example that could cut and pasted into a final product.

    I'm not going to waste anymore effort trying to influence you to behave appropriately.  If you decide to respond to this post, do not expect any response from me as I am going to "feed the troll".

     

    Monday, January 9, 2012 4:43 PM
  • Thank you!
    Sunday, February 12, 2012 7:46 PM