0
votes

My form has 6 dataGridViews in different tabs of a tab Control, the user is able to enter values and change the cell backcolor, with a save button, the values for each dataGridView are saved into a text file, same with the backcolor of the cells in each dataGridView. When the user re-opens the form, all the last settings(style and values) are loaded again to the 6 dataGridViews; The problem is that when the user re-opens the form, the dataGridViews freeze and I don't find a way to fix that. Can someone help me?

My loading data and style code:

 foreach (string s in File.ReadAllLines(stylePath))
        {
            string[] items = s.Split(';');
            if (items.Length == 3)
            {
                dataGridView1.Rows[Convert.ToInt32(items[0])]
                   .Cells[Convert.ToInt32(items[1])]
                   .Style.BackColor = Color.FromName(Convert.ToString(items[2]));
            }
        }
 StreamReader sr = new StreamReader(valuePath);
        foreach (DataGridViewRow row in dataGridView1.Rows)
        {
            foreach (DataGridViewCell cell in row.Cells)
            {
                cell.Value = sr.ReadLine();
            }
        }
        sr.Close();

Here is how it looks when re-opening the form: enter image description here

1
chris: we need to see some code to understand where it does freezechouaib
please edit/add the part where you re-open, read from file, update the gridViewschouaib
@chouaib I noticed something more, if I don't run the LoadStyle void, then the dataGridView did not freeze.ChrisCreateBoss

1 Answers

1
votes

try:

using(StreamReader sr = new StreamReader(stylePath))
{
   string line;
   string[] items;
   int row, cell;
   Color color;

   while(!sr.EndOfStream)
   {
      line = sr.ReadLine();
      items = line.Split(';');
      if(items.Length<3) continue; //something wrong in the file

      row = Convert.ToInt32(items[0]);
      cell = Convert.ToInt32(items[1]);

      if(String.IsNullOrEmpty(item[2])) continue; // No change is needed
      color = Color.FromName(items[2]);

      dataGridView1.Rows[row].Cells[cell].Style.BackColor = color;
   }
}