How to save color changes to a Datagridview line?

Asked

Viewed 21 times

-1

I need to save the color changes the user makes on the datagrid.

Currently the colors are exchanged according to the user’s selection using the following code:

dgv1.CurrentRow.DefaultCellStyle.BackColor = Color.Red; //Troca a cor da linha para vermelho

but if any form reset happens, the datagrid returns to the default color (white).

Is there any way to save these changes and load them at the time of the datagrid load?

1 answer

1

You can use the System.IO.File to read and save a file containing the color.
Saving the color:

var cor = dgv1.CurrentRow.DefaultCellStyle.BackColor;
System.IO.File.WriteAllText("cor.txt", $"{cor.R.ToString()},{cor.G.ToString()},{cor.B.ToString()}");

Carrying the color:

string[] rgb = System.IO.File.ReadAllText("cor.txt").Split(",");
int r = int.Parse(rgb[0]);
int g = int.Parse(rgb[1]);
int b = int.Parse(rgb[2]);
dgv1.CurrentRow.DefaultCellStyle.BackColor = System.Drawing.Color.FromArgb(r, g, b);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.