【发布时间】:2017-03-12 09:52:43
【问题描述】:
for (int i = 0; i < countriesCodes.Length; i++)
{
dataGridView1.ColumnCount = 2;
dataGridView1.Columns[0].Name = "Status";
dataGridView1.Columns[1].Name = "Country";
var countryName = codeToFullNameMap[countriesCodes[i]];
string[] row = new string[] { "Ready", countryName };
dataGridView1.Rows.Add(row);
DataGridViewLinkColumn dgvLink = new DataGridViewLinkColumn();
dgvLink.UseColumnTextForLinkValue = true;
dgvLink.LinkBehavior = LinkBehavior.SystemDefault;
dgvLink.HeaderText = "Link Data";
dgvLink.Name = "SiteName";
dgvLink.LinkColor = Color.Blue;
dgvLink.TrackVisitedState = true;
dgvLink.Text = lines[i];
dgvLink.UseColumnTextForLinkValue = true;
dataGridView1.Columns.Add(dgvLink);
}
this.dataGridView1.Columns[2].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
dataGridView1.EnableHeadersVisualStyles = false;
dataGridView1.DefaultCellStyle.SelectionBackColor = dataGridView1.DefaultCellStyle.BackColor;
dataGridView1.DefaultCellStyle.SelectionForeColor = dataGridView1.DefaultCellStyle.ForeColor;
dataGridView1.RowHeadersVisible = false;
dataGridView1.AllowUserToAddRows = false;
dataGridView1.BackgroundColor = System.Drawing.SystemColors.Control;
dataGridView1.Columns[0].DefaultCellStyle.ForeColor = Color.Red;
最后一行应该为第一列下的单元格着色:
dataGridView1.Columns[0].DefaultCellStyle.ForeColor = Color.Red;
如果我将其更改为 Columns1,它将所有单元格着色为红色。但是当它是 Columns[0] 时,状态下的第一个单元格(在屏幕截图中)仍然是黑色的。
状态下的第一个单词Ready是黑色的。无法弄清楚为什么它也没有着色。如果我为 Columns1 下的单元格着色,它将为所有单元格着色,但我想为 Columns[0] 中的 Status 下的单元格着色
【问题讨论】:
-
可能是因为被选中了?
-
代码顺序通常很混乱。这是错误的,因为代码更改了默认单元格样式在它已经添加了行。所以第一行不可避免地使用了旧的单元格样式。很多代码都需要移到 for(;;) 循环之前,即任何配置 DGV 的代码。
标签: c# .net winforms datagridview