Set the C1FlexGrid cell backcolor

Posted by: j0nnic0 on 10 September 2017, 3:37 am EST

  • Posted 10 September 2017, 3:37 am EST

    Hi!

    This is only an example, but how do I generate a display where cell in row:0 column:0 displays blue and cell in row:1 coloumn:0 displays red?

    I used VB.Net 2005 for the code:

    With C1FlexGrid1
    Dim NewStyle As C1.Win.C1FlexGrid.CellStyle
    NewStyle = .Styles.Add("NewStyle")
    NewStyle.BackColor = System.Drawing.Color.Blue
    .SetCellStyle(0, 0, NewStyle)
    NewStyle.BackColor = System.Drawing.Color.Red
    .SetCellStyle(1, 0, NewStyle)
    End With

    It only displays both cells as red color. What should I do to be able to display the appropriate result?

    Thanks!! :D

    -> Nico
  • Replied 10 September 2017, 3:37 am EST

    Hello Nico,

    After setting the NewStyle's BackColor to Blue, you reassigned the Color Red as its BackColor, that is why both the cells appear Red in Color.
    You need to create two different styles for both the cells as follows :

    With C1FlexGrid1
    Dim NewStyle1 As C1.Win.C1FlexGrid.CellStyle
    NewStyle1 = .Styles.Add("NewStyle1")
    NewStyle1.BackColor = System.Drawing.Color.Blue
    .SetCellStyle(0, 0, NewStyle1)

    Dim NewStyle2 As C1.Win.C1FlexGrid.CellStyle
    NewStyle2 = .Styles.Add("NewStyle2")
    NewStyle2.BackColor = System.Drawing.Color.Red
    .SetCellStyle(1, 0, NewStyle2)
    End With


    You can also achieve the same effect using the OwnerDrawCell event of the Grid.
    Set the DrawMode property of the Grid to 'OwnerDraw'.

    C1FlexGrid1.DrawMode = C1.Win.C1FlexGrid.DrawModeEnum.OwnerDraw

    'Use the following code to assign different colors to the cells
    Private Sub C1FlexGrid1_OwnerDrawCell(sender As System.Object, e As C1.Win.C1FlexGrid.OwnerDrawCellEventArgs) Handles C1FlexGrid1.OwnerDrawCell
    C1FlexGrid1.Cols(e.Col).StyleNew.BackColor = Color.White
    If e.Col = 0 And e.Row = 0 Then
    C1FlexGrid1.Cols(e.Col).StyleNew.BackColor = Color.Blue
    End If
    If e.Col = 0 And e.Row = 1 Then
    C1FlexGrid1.Cols(e.Col).StyleNew.BackColor = Color.Red
    End If
    End Sub


    Refer to the attached sample implementing the above.

    Hope it helps.

    Regards,
    Reema
    2013/06/Sample_FlexBackCOlor.zip
  • Replied 10 September 2017, 3:37 am EST

    Hi Reema!

    Thanks for the quick reply.

    I sort of understand a little bit how my C1FlexGrid works, thanks to your code. But if you don't mind, I have another question.

    My program basically lets the user choose a color for each C1FlexGrid cell. It saves the name of each color on a database. Then, every startup, the program will display all recorded colors for cells. Because of this, how will I be able to use 1 variable per CellStyle? Should I create another variable for the other cell? If so, how do I do that?

    Sorry for not understanding this very quickly. And sorry if my English is not that clear. I really couldn't explain this very clearly.

    Thanks again!!

    Nico
  • Replied 10 September 2017, 3:37 am EST

    Hi again!

    Don't answer my last question anymore. :) I found out how.. Thank you!

    You don't have to actually change the variable to be able to add colors to different cells. Just make sure that the string literal in the

    NewStyle = .Styles.Add("THIS STRING")

    is different than the rest. Has to be unique.

    Thanks again!

    You were a real help. :D

    Nico
  • Replied 10 September 2017, 3:37 am EST

    Hello Nico,

    Yes, it has to be unique & could be done this way too.
    So that a new Style is being added to the Styles collection of the Grid for the second cell.
    With the following statements :

    NewStyle = .Styles.Add("NewStyle")
    NewStyle = .Styles.Add("THIS STRING")

    You are actually adding two different styles to the Grid's Styles Collection. :-)

    Regards,
    Reema
  • Replied 10 September 2017, 3:37 am EST

    How do you change the backcolor of a specific cell being edited as the user enters data into it.
    If one enters a valid value then color changes to green. If however the value being
    entered or the key being pressed makes the value invalid the cell backcolor should change to red.

    This does not work with the keypressedit mode since the colors change only after exiting the
    cell or on pressing the return key.
    This has to happen while the cursor is in the cell being edited.

    Any ideas.
  • Replied 10 September 2017, 3:37 am EST

    Hi,

    We would like to inform you that you can change the BackColor of the Editor while the user is entering the data into it by using the <u>KeyUpEdit event</u> of C1FlexGrid. In the event, put the required validation condition and use the <u>BackColor</u> property of the <u>Editor</u> of the C1FlexGrid to change the back color. Here is the suggested code snippet:
    void c1FlexGrid1_KeyUpEdit(object sender, C1.Win.C1FlexGrid.KeyEditEventArgs e)
    {
    if (c1FlexGrid1.Editor.Text != "")
    {
    if (Convert.ToInt32(c1FlexGrid1.Editor.Text) <= 100)
    {
    c1FlexGrid1.Editor.BackColor = Color.Red;
    }

    else
    c1FlexGrid1.Editor.BackColor = Color.Green;
    }
    }


    Regards,
    Arpit Jain
  • Replied 10 September 2017, 3:37 am EST

    Yes this will work in all events as long as the editor backcolor is being changed.
    The other challenge was getting the cursor at the right position during a clickevent of the
    cell.This has been resolved as well by using the select method of the textbox.

    However the bigger problem is to retain a zero value if the backspace key has been pressed and
    effectively reducing the input to a null.
    Also if the value of the textbox has changed how can this be updated during this event.
    More specifically the keypressedit event.

    Any attempt to do this appends the earlier value with the current value.

    Really appreciate your earlier response. Will be delighted if this issue can be addressed as well.
  • Replied 10 September 2017, 3:37 am EST

    Hello,

    As per our understanding, you want to clear the text in the cell of the C1FlexGrid when the Backspace key is pressed while typing. In order to accomplish your requirements, you can use the same “KeyUpEdit” event and in the event, check if the Backspace key is pressed, clear the Text of the Editor of the C1FlexGrid. Here is the suggested code:
    void c1FlexGrid1_KeyUpEdit(object sender, C1.Win.C1FlexGrid.KeyEditEventArgs e)
    {
    if (e.KeyCode == Keys.Back)
    {
    c1FlexGrid1.Editor.Text = "";
    c1FlexGrid1.Editor.BackColor = Color.White;
    }
    }


    Please refer to the attached sample for complete implementation.

    Regarding the issue:
    <blockquote>
    Also if the value of the textbox has changed how can this be updated during this event.
    More specifically the keypressedit event.

    Any attempt to do this appends the earlier value with the current value.
    </blockquote>

    We are sorry to mention but we could not clearly understand this requirement, so could you please elaborate it a bit further along with the use case scenario so it would be helpful for us in understanding it clearly and providing you a solution at the earliest.

    Regards,
    Arpit Jain
    2016/01/C1FlexGrid_CellColor_Modified.zip
Need extra support?

Upgrade your support plan and get personal unlimited phone support with our customer engagement team

Learn More

Forum Channels