Change the color of a line and deselect a DataGrid

2

I have a method where I can change the color of a row in my DataGrid.

I followed this link: Link

The problem is how to take the selection off when it is painted. Type, go back to the original color.

I tried the following way:

public void alterarCorLinhaSelecionada()
    {
        int selectedIndex = dataGridAtribuicaoVaga.SelectedIndex;
        //Guarda a Row selecionada
        DataGridRow row =
                  dataGridAtribuicaoVaga.ItemContainerGenerator.ContainerFromIndex(selectedIndex) as DataGridRow;

        if (row == null)//A linha selecionada não está visivel
        {
            object item2 = dataGridAtribuicaoVaga.Items[selectedIndex];
            dataGridAtribuicaoVaga.ScrollIntoView(item2);//Torna a linha selecionada visivel
            row = dataGridAtribuicaoVaga.ItemContainerGenerator.ContainerFromIndex(selectedIndex) as DataGridRow;
        }

        var bc = new BrushConverter();

        if (row.IsSelected)
            row.IsSelected = false;
        else
            row.Background = (Brush)bc.ConvertFrom("#169FDB");
    }

In this case the error occurs on the line where it takes column 2

Object item = dataGridAtribuicaoVaga.SelectedItem;

        if (dataGridAtribuicaoVaga.SelectedCells.Count > 0)
        {
            cod = Convert.ToInt32((dataGridAtribuicaoVaga.SelectedCells[1].Column.GetCellContent(item) as TextBlock).Text);
            desc = (dataGridAtribuicaoVaga.SelectedCells[2].Column.GetCellContent(item) as TextBlock).Text;
            min = Convert.ToInt32((dataGridAtribuicaoVaga.SelectedCells[3].Column.GetCellContent(item) as TextBlock).Text);
            med = Convert.ToInt32((dataGridAtribuicaoVaga.SelectedCells[4].Column.GetCellContent(item) as TextBlock).Text);
            max = Convert.ToInt32((dataGridAtribuicaoVaga.SelectedCells[5].Column.GetCellContent(item) as TextBlock).Text);
        }

        p1 = new Modelos.PerfilVagaAtribuicoes();
        atribuicao = new Atribuicao();

        p1.Codigo = cod;
        p1.Descricao = desc;
        p1.PontuacaoMin = min;
        p1.PontuacaoMed = med;
        p1.PontuacaoMax = max;

        if (!_editList.Any(items => items.Codigo == p1.Codigo))
            _editList.Add(p1);

Error:

  

An exception of type 'System.ArgumentOutOfRangeException' occurred in PresentationFramework.dll but was not handled in user code   Additional information: Specified argument was out of range of valid values.

    
asked by anonymous 16.06.2015 / 18:46

1 answer

1

Do it this way.

if(dataGridAtribuicaoVaga.SelectedCells.Count() > 0)
cod = Convert.ToInt32((dataGridAtribuicaoVaga.SelectedCells[0].Column.GetCellContent(item) as TextBlock).Text);
    
22.06.2015 / 17:44