none
change color of selected row in datagridview RRS feed

  • Question

  • Hi,

    I have a datagridview.
    Now, the user should select a row and change the color of that row to any color he wishes.
    When he selects the row and rightclicks he should be able to change the color of the row.
    Color changing is not based on any condition. He should be able to do for any row.

    Thanks in adance
    Monday, September 28, 2009 7:35 PM

Answers

  • Do you mean ColorDialog? And try google before posting, first result from searching "colordialog":

    http://msdn.microsoft.com/en-us/library/system.windows.forms.colordialog.aspx
    Tuesday, September 29, 2009 4:34 PM
  • Hi rowter,

    The code snippet below shows how to change the row back color with ColorDialog:
        //Handle the cell mouse click event to change the back color of the current row.
        void dataGridView1_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            ColorDialog colorDlg = new ColorDialog();
            if (colorDlg.ShowDialog() == DialogResult.OK)
            {
                this.dataGridView1.Rows[e.RowIndex].DefaultCellStyle.BackColor = colorDlg.Color;
            }
        }
    Regards,
    Aland Li


    Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread.
    Wednesday, September 30, 2009 9:34 AM
    Moderator

All replies

  • Ok, what have you tried, what problems did you run into, what is your question?
    Monday, September 28, 2009 7:53 PM
  • Ravi,

    I am sorry. didnt put the question properly.
    Right now, i have code in place where the color of the row changes to red upon doubleclick.

    Now, the user should be able to select whichever color he wants to change it to.

    Either upon right click he gets options to select color or a color palette comes up and he selects a color from the palette.
    which of these 2 would be a better or easier one to implement.

    Thanks.
     
    Monday, September 28, 2009 8:00 PM
  • It depends what you want to do. Do you want to right click a row and set the row to that colour? Or do you want to right click and set the double click colour so that all of the double clicked rows change?

    I think the easiest way is to create a contextmenustrip with a few colours, or even just put a menustrip on your form and put it there. The contextmenustrip will add a right click menu when you set the DataGridView's contextmenustrip property to it, the menustrip is what's at the top of most windows (file, edit, tools, etc...).

    Monday, September 28, 2009 8:25 PM
  • Hi Scotty,

    I had to use the colorpalaette instead of the context menu.
    Colorpalette came up and i used it to set the background color of the row.

    Now my question is how can you get the name of the color selected by the client?


    Thanks in Advance
    Tuesday, September 29, 2009 3:27 PM
  • Do you mean ColorDialog? And try google before posting, first result from searching "colordialog":

    http://msdn.microsoft.com/en-us/library/system.windows.forms.colordialog.aspx
    Tuesday, September 29, 2009 4:34 PM
  • Hi rowter,

    The code snippet below shows how to change the row back color with ColorDialog:
        //Handle the cell mouse click event to change the back color of the current row.
        void dataGridView1_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            ColorDialog colorDlg = new ColorDialog();
            if (colorDlg.ShowDialog() == DialogResult.OK)
            {
                this.dataGridView1.Rows[e.RowIndex].DefaultCellStyle.BackColor = colorDlg.Color;
            }
        }
    Regards,
    Aland Li


    Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread.
    Wednesday, September 30, 2009 9:34 AM
    Moderator