Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/300.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 更改DataGridView中一行中多个选定单元格的颜色_C#_Winforms_Visual Studio 2012_Datagridview - Fatal编程技术网

C# 更改DataGridView中一行中多个选定单元格的颜色

C# 更改DataGridView中一行中多个选定单元格的颜色,c#,winforms,visual-studio-2012,datagridview,C#,Winforms,Visual Studio 2012,Datagridview,我有一个30行12列的Gridview,具有multiselect true和选择模式:Cell Select。 我想做以下事情: a) 当用户选择单元格时,单元格的颜色应该改变。但是,我想限制用户,以便它一次只能从一行中选择多个单元格,即用户不能跨不同行选择多个单元格 b) 如果用户选择一行中的多个单元格,则该单元格的颜色将发生变化,并且该单元格将保持选中状态,直到用户重新选择该单元格以进行更改以消除颜色变化(取消选择以默认颜色)。现在,若用户选择某些单元格,它的颜色会发生变化,但当用户移动到

我有一个30行12列的Gridview,具有multiselect true和选择模式:Cell Select。 我想做以下事情:

a) 当用户选择单元格时,单元格的颜色应该改变。但是,我想限制用户,以便它一次只能从一行中选择多个单元格,即用户不能跨不同行选择多个单元格

b) 如果用户选择一行中的多个单元格,则该单元格的颜色将发生变化,并且该单元格将保持选中状态,直到用户重新选择该单元格以进行更改以消除颜色变化(取消选择以默认颜色)。现在,若用户选择某些单元格,它的颜色会发生变化,但当用户移动到选择其他单元格时,以前选择的单元格会变回默认颜色(自动取消选择)

现在我可以更改选定单元格的颜色,但我无法满足上述条件

foreach (DataGridViewRow row in dataGridView1.Rows)
            {
                foreach (DataGridViewColumn col in dataGridView1.Columns)
                {

                   dataGridView1.Rows[row.Index].DefaultCellStyle.SelectionBackColor = Color.Pink;

                }
            }
[更新]:我尝试过这个,但仍然无法解决我的问题

    private void dataGridView1_CellClick(object sender,
       DataGridViewCellEventArgs e)
    {
        List<DataGridViewRow> rowCollection = new List<DataGridViewRow>();



        foreach (DataGridViewCell cell in dataGridView1.SelectedCells)
        {
            rowCollection.Add(dataGridView1.Rows[cell.RowIndex]);

        }




        foreach (DataGridViewRow row in rowCollection)
        {

                dataGridView1.Rows[row.Index].DefaultCellStyle.SelectionBackColor = Color.Pink;


        }

    }
private void dataGridView1\u CellClick(对象发送方,
DataGridViewCellEventArgs(e)
{
List rowCollection=新列表();
foreach(dataGridView1.SelectedCells中的DataGridViewCell单元格)
{
添加(dataGridView1.Rows[cell.RowIndex]);
}
foreach(行集合中的DataGridViewRow行)
{
dataGridView1.Rows[row.Index].DefaultCellStyle.SelectionBackColor=Color.Pink;
}
}

我建议查看DataGridView的默认事件。根据条件的声音,您需要一个选定单元格的列表(或者您可以计算的单元格对象上已经有一个标志),并且您需要为单元格单击事件添加一个事件处理程序。因此,您可以对照单击的新单元格检查“selectedCells”列表中的单元格行。如果它们是一样的,你可以给电池上色。您还可以检查列表中是否存在该单元格,表明您希望删除该着色

希望这有帮助!:)

a)只需使用
DataGridView
SelectionChanged
事件即可:

private void dataGridView1_SelectionChanged(object sender, EventArgs e)
{
    int currentRowIndex = dataGridView1.CurrentCell.RowIndex;

    foreach (DataGridViewCell cell in dataGridView1.SelectedCells)
        if (cell.RowIndex != currentRowIndex)
            cell.Selected = false;
}
b) 你的问题我不清楚。它看起来和a)一样。顺便说一下,您可以按住CTRL键在
DataGridView
中选择多个单元格

最后,不要使用
DefaultCellStyle.SelectionBackColor
,它相当慢。我建议您处理
DataGridView
CellPainting
事件以对单元格着色:

private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
    if ((e.State & DataGridViewElementStates.Selected) != 0)
    {
        e.CellStyle.SelectionBackColor = Color.Pink;
    }
}