[英]Datagridview updating just last row
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;
}
}
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;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.