GridView的DataGridViewLinkColumn中

GridView的DataGridViewLinkColumn中

本文介绍了SelectionForeColor无法用于DataGridView的DataGridViewLinkColumn中的链接单元格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Winform 4.5应用程序中,我有一个 DataGridView ,其中第一列为链接列.我希望所选链接单元格的链接颜色为白色.由于默认情况下,所选行(或单元格)的背景颜色为蓝色,并且所有链接的 ForeColor 也是蓝色,因此当用户选择一行(或链接单元格)时,链接不可读.我尝试编写以下代码,但它根本不会改变所选链接单元格的链接颜色.

In my Winform 4.5 app, I have a DataGridView with first column as a link column. I would like to have the link color of the selected link cell to be white. Since by default the background color of a selected row (or a cell) is blue and the ForeColor of all the links are also blue, when user selects a row (or a link cell) the text of the link is not readable. I tried writing the following code but it does not change the link color of selected link cell at all.

private void dataGridView1_SelectionChanged(object sender, EventArgs e)
{
    foreach (DataGridViewLinkCell cell in ((DataGridView)sender).SelectedCells)
    {
        if (cell.ColumnIndex == 0)
        {
            if (cell.Selected)
            {
                cell.Style = new DataGridViewCellStyle()
                {
                    SelectionForeColor = SystemColors.HighlightText
                };
            }
        }
    }
}

然后我修改了上面的代码,如下所示.但这会将所有链接的链接颜色更改为白色-由于这些链接的背景色也是白色,因此使未选择的链接单元格不可读:

I then modified the above code as follows. But it changes the link color of all the links to white - that makes non-selected link cells to be not readable since the backcolor of those links is also white:

private void dataGridView1_SelectionChanged(object sender, EventArgs e)
    {
        foreach (DataGridViewLinkCell cell in ((DataGridView)sender).SelectedCells)
        {
            if (cell.ColumnIndex == 0)
            {
                if (cell.Selected)
                {
                    cell.LinkColor = SystemColors.HighlightText;
                }
            }
        }
    }

我通过在foreach循环内设置一个断点并选择一个链接单元来测试了这两个代码.我注意到代码确实正确地经过了foreach循环的一次迭代.此外,我没有更改 DataGridViewLinkColumn

I tested both the codes by setting a breakpoint inside the foreach loop and selecting a link cell. I noticed that the code does go through exactly one iteration of the foreach loop correctly. Moreover, I have made no change to the default settings of the DataGridViewLinkColumn

修改默认情况下, DataGridView 在行选择中看起来像这样.请注意,第二列中的单元格将其 ForeColor 更改为白色,但第一列中的单元格未更改:

EditBy default the DataGridView looks like this on a row selection. Notice that the cell in the second column changes its ForeColor to white but not the cell in the first column:

我希望它在行选择中看起来像这样:

I want it to looks like this on a row selection:

推荐答案

编辑 CellLeave 事件.

    private void dataGridView1_SelectionChanged(object sender, EventArgs e)
    {
        foreach (DataGridViewLinkCell cell in
            ((DataGridView) sender).SelectedCells.OfType<DataGridViewLinkCell>())
        {
            if (cell.Selected)
            {
                cell.LinkColor = SystemColors.HighlightText;
            }
        }

    }

    private void dataGridView1_CellLeave(object sender, DataGridViewCellEventArgs e)
    {
        foreach (DataGridViewLinkCell cell in
            ((DataGridView) sender).Rows[e.RowIndex].Cells.OfType<DataGridViewLinkCell>())
        {
            cell.LinkColor = cell.LinkVisited ? Color.Purple : Color.Blue;
        }
    }

这篇关于SelectionForeColor无法用于DataGridView的DataGridViewLinkColumn中的链接单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 22:11