【问题标题】:Why when coloring the first columns[0] cells in dataGridView1 it's not coloring the first cell?为什么在为 dataGridView1 中的第一列 [0] 单元格着色时,它没有为第一个单元格着色?
【发布时间】: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


【解决方案1】:

如果您将选定的单元格移动到另一个cell,您将看到该项目也有红色forecolor。这两行将覆盖选择颜色。评论它们或给出一些合乎逻辑的东西以避免混淆。

dataGridView1.DefaultCellStyle.SelectionBackColor = dataGridView1.DefaultCellStyle.BackColor;
dataGridView1.DefaultCellStyle.SelectionForeColor = dataGridView1.DefaultCellStyle.ForeColor;

编辑 1:

对于您评论的问题,将它们设置为透明。

dataGridView1.DefaultCellStyle.SelectionBackColor = System.Drawing.Color.Transparent;
dataGridView1.DefaultCellStyle.SelectionForeColor = System.Drawing.Color.Transparent;

希望有帮助,

【讨论】:

  • 评论这两行解决了我的问题,但在选择第一个单元格之前提出了我的问题,我的意思是像我点击它一样突出显示。这就是我添加这两行的原因,因为这行是删除高亮的唯一有效解决方案。现在高亮又回来了,但如果我单击/选择另一个单元格,它就会变成红色。
  • 差不多。现在第一个单元格是空白的只有当我点击它选择它我会看到里面的文字但它不是红色的。
  • 好的,把这个dataGridView1.CurrentCell.Selected = false;@DanielHalfoni
  • 唯一有效的是:DataGridViewCellStyle mystyle = new DataGridViewCellStyle(); mystyle.SelectionBackColor = 颜色.白色; mystyle.SelectionForeColor = 颜色.红色; dataGridView1.DefaultCellStyle = mystyle;我尝试了所有其他解决方案,包括 CurrentCell.Selected false ,但到目前为止只有这个解决方案。
  • 我给出的所有解决方案都出了什么问题?可以提供输出吗?或者设置 defaultcellstyle 完全解决了你的问题? @DanielHalfoni
【解决方案2】:

我也遇到了同样的问题。我通过在我的“For”循环中添加一个条件来解决它。在您的情况下,您需要输入:

if(i == 0)
   {
    countriesCodes.Colums[0].Selected = false;
   }

我认为缺少着色是由于单元格选择

的着色不正确

【讨论】:

    猜你喜欢
    • 2013-03-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-18
    相关资源
    最近更新 更多