Change the color of a line and take the selection of a Datagrid

Asked

Viewed 755 times

2

I have a method where I can change the color of a line in my Datagrid.

I followed through this link: Link

The problem is being like taking out the selection from when it is painted. Type, go back to the original color.

I tried it 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 in the row where it picks up 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 the range of Valid values.

  • Probably the dataGridAtribuicaVaga.Selectedcells must be with Length = 0

  • What do you mean Paul? but there is some solution?

1 answer

1

Do so.

if(dataGridAtribuicaoVaga.SelectedCells.Count() > 0)
cod = Convert.ToInt32((dataGridAtribuicaoVaga.SelectedCells[0].Column.GetCellContent(item) as TextBlock).Text);
  • I had to put "Count" instead of "Length" because I couldn’t find...

  • Yes, because Voce did not select any cells.

  • I select with the mouse click on the line Paul, then when I select it occurs this error

  • Try to put Selectedcells[0], I updated the answer

  • I can’t Paulo, because I am recovering the value that is in column 2. that is the code. =/

  • Voce selects ALL lines by clicking? if yes should work. could post the code at the time uqe is on this line? places a breakpoint and a WHATCH on this Selectedcells object

  • I amended Paulo..

  • Funciounou? how are you? altered what?

  • I changed the post up there

Show 4 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.