0

I am trying to allow the user to change the color of the datagrid displayed in my app.

for this I use user settings and colordialog boxes.

problem is, when I try to update the color, it isnt displayed, and I either have to close/reopen the app to see the changes, or load a completly different DGV (they are in a tabcontrol).

here is the code I use at the update:

AlternatingRowsDefaultCellStyle.BackColor = Properties.Settings.Default.CellBackColor1;

am I missing something?

I tried to refresh the DGV, but it doesnt change a thing.

4

1 回答 1

0

I added a Button to a form that opens up a ColorDialog and waits for the users to select a color. If the user hits the OK button, it first saves the setting and then sets the dataGridView.CellStyle.BackColor to whatever color the user selects. This forces an immediate update of the form with the correct background color.

private void button1_Click(object sender, EventArgs e)
{
    if (colorDialog1.ShowDialog() == DialogResult.OK)
    {
        Properties.Settings.Default.CellBackColor1 = colorDialog1.Color;
        dataGridView1.AlternatingRowsDefaultCellStyle.BackColor = colorDialog1.Color;
    } 
}

I assume your Form.Load and Form.FormClosing events handle the saving of the settings.

于 2012-08-08T05:54:56.733 回答