Adding checkbox to datagrid lines

Asked

Viewed 479 times

3

I have an application in WPF, where there is a window with a datagrid with multiple lines, having only records. In this window there is also a button and I need the user, when clicking the button, to appear in each line of the datagrid one checkbox.

After the user has finished the checklist, he will click again on the button that will call a function, traversing the lines of the datagrid and checking which ones are checked to take action only on the checked records.

  • Friend focus the question on the problem you want the solution, it seems you are describing a feature you want someone to implement. If possible put the code thingy as well.

  • Hstackoverflow I’m a little new in WPF but know c#, I really needed a function to do this.

  • I tried this but it didn’t work:private void btnExcluir_Click(Object Sender, Routedeventargs and) { for (var i = 0; i < dgPedidos.Items.Count; i++) { Checkbox ck = new Checkbox(); dgPedidos.Items.Add(ck); } }

  • Gives this error: Operation is not Valid while Itemssource is in use. Access and Modify Elements with Itemscontrol.Itemssource Instead.

  • @jp_almeida Vê this. Then you have to adapt to your case: You need to add Datagridcheckboxcolumn to Columns and not Checkbox to Items as in one of your comments.

2 answers

1

You can add a Template column of a checkbox in each row instead of just the name, for this the property of your object being added to the Datagrid should be a boolean.

<DataGrid AutoGenerateColumns="False" Name="dtgUsuarios">
    <DataGrid.Columns>

        <DataGridTemplateColumn Header="Habilitado">
            <DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <CheckBox IsChecked="{Binding UsuarioHabilitado}"/>
                </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
        </DataGridTemplateColumn>

         //Outras Colunas...
         //<DataGridTextColumn...            
         //<DataGridTextColumn...

    </DataGrid.Columns>
</DataGrid>

Add users to Datagrid:

Usuario user = new Usuario();

dtgUsuarios.Items.Add(user);

And last with the click of the button update the values of the checkbox:

private void btAtualizar_Click(object sender, RoutedEventArgs e)
{
    foreach (var item in dtgUsuarios.Items)
    {
        bool novoValorCheckbox = item.UsuarioHabilitado;
    }
}

-2

public class Pessoa ː public int ID { get; set; } public string Name{ get; set; } }

public class Personal model : Person ː public bool Selected { get; set; } }

private voids Butaoclick() { List list list = new List(); list = Loaded based(); Itemssource = list; }

Makes sense

  • I used: <Datagridtemplatecolumn.Header> <Checkbox></Checkbox> </Datagridtemplatecolumn.Header> dgPedidos.Columns[1]. Visibility = Visibility.Visible; btnCancelar.Visibility = Visibility.Visible; and it worked.

  • In the case when I did the first mode I must activate the property isreadonly="false" to check, but this way the user can edit the values of the grid.

  • jp_almeida, I fully agree, the mode you used is the one that best fits what you initially intended. In my example I did not take into account the question of the button to show the checkboxes, hence my reply does not actually answer your question. However I would like you to notice the difference, in your version you are adding checkboxes to the datagrid that will then have to analyze which ones are selected, in my example is the class model associated with the datagrid that automatically contains this information. Both solutions fit to include checkboxes in the datagrid.

  • Rui Guerra, I agree with you, but I also created a class with all the data of the grid lines and also a bool that controls whether it is checked or not. For line control I have the code. Thank you for the answer.

  • Rui Guerra, I have another problem, I have some buttons in this window that do some things. When I click on any of them appears the checks on the datagrid and a cancel button. When I click the cancel button, it disappears along with the checks, but when I click again on a button it gives an error: style specified element is already the Logical Child of Another element. Disconnect it first. Could you help me? Thank you.

  • private void btnCancelar_Click(Object Sender, Routedeventargs e) { dgPedidos.Columns[1]. Visibility = Visibility.Hidden; btnCancelar.Visibility = Visibility.Hidden; }

Show 1 more comment

Browser other questions tagged

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