0
  1. I have a folder in shared drive containing xml files.
  2. The file gets clubbed into a single data for live monitoring
  3. The FilesystemWatcher listen to last modified time and load the file in to main datatatable or merges it.
  4. The datagridview rowcell should appear should display the color on the basis country.
  5. I have no problem in clubbing the file and doing other stuffs except the color part in the datagridview.
  6. The problem is that the below code while loading the file datagridveiw rows do changes the color of the cell but when it finishes loading, only last datagridview row shows the colored cell.
  7. on the top of that, if user sorts the datagridview column, all cells loses their color format including the last one
  8. Can anyone tell me what I am doing wrong or how to fix it?
private void Read_Summary_XML()
{
  String[] country_array = new[] { "US", "Russia", "United Kingdom", "Cananda" };
  Color[] color_array = new Color[] { Color.FromArgb(53, 255, 51), Color.FromArgb(51, 204, 51), Color.FromArgb(255, 153, 0), Color.FromArgb(255, 153, 0) };
  List<String> direcotries_list = new List<String>(Directory.EnumerateDirectories(log_folder));
  foreach (String directory in direcotries_list)
  {
    String xml_file = Path.Combine(directory, "summary.xml");
    using (XmlReader xml_reader = new XmlNodeReader(Read_XML_File(xml_file)))
    {
      DataTable dt = new DataTable();
      dt = summary_dt.Clone();
      dt.ReadXml(xml_reader);
      summary_dt.Merge(dt);
      String country = dt.Rows[0]["country_clm"].ToString();
      DataGridViewRow row = (from dgv_row in summary_dgv.Rows.Cast<DataGridViewRow>()
                             where dgv_row.Cells["country_clm"].Value.ToString().Equals(country)
                             select dgv_row).First();
      Int32 ri = row.Index;
      row.Cells["country_clm"].Style.BackColor = color_array[Array.IndexOf(country_array, country)];

    }
 color_array = null; country_array = null;
 }
}
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
J D
  • 63
  • 1
  • 8
  • I've seen a lot of weird issues with DGV. So normally I suggest deleting the DGV and adding again. This often solves issues. How many styles do you have defined for the DGV? Usually there is a default style and when you change BackColor with one style you change all the rows. Can't explain why only one row gets color and other default to no color. I would of expected all the rows to get the same color. – jdweng Jul 08 '20 at 15:34
  • @jdweng do you know the answer? – J D Jul 08 '20 at 15:49
  • Read following : https://learn.microsoft.com/en-us/dotnet/framework/winforms/controls/cell-styles-in-the-windows-forms-datagridview-control – jdweng Jul 08 '20 at 15:57

1 Answers1

0

I have managed to find the solution. Add cell formatting event listener in form load event.

this.summary_dgv.CellFormatting += new System.Windows.Forms.DataGridViewCellFormattingEventHandler(this.DGV_Cell_Formatting);

private void DGV_Cell_Formatting(object sender, DataGridViewCellFormattingEventArgs e)
{
  if (summary_dgv.Columns[e.ColumnIndex].Name == "country_clm")
  {
     String[] country_array = new[] { "US", "Russia", "United Kingdom", "Cananda" };
     Color[] color_array = new Color[] { Color.FromArgb(53, 255, 51), Color.FromArgb(51, 204, 51), Color.FromArgb(255, 153, 0), Color.FromArgb(255, 153, 0) };

     summary_dgv.Rows[e.RowIndex].Cells["country_clm"].Style.BackColor = color_array[Array.IndexOf(country_array, e.Value)];
     color_array = null; country_array = null;
   }
}
J D
  • 63
  • 1
  • 8