Adding checkbox to datagrid rows

3

I have an application in WPF , in which there is a window with a datagrid with several lines, having only registers. In this window there is also a button and it is necessary for the user, when clicking the button, to appear on each line of datagrid a checkbox .

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

    
asked by anonymous 10.03.2015 / 19:38

2 answers

1

You can add a column with Template from a checkbox in each row instead of just the name, so the property of your object being added to the Datagrid must 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 lastly with the click of the button update the values of the checkboxes:

private void btAtualizar_Click(object sender, RoutedEventArgs e)
{
    foreach (var item in dtgUsuarios.Items)
    {
        bool novoValorCheckbox = item.UsuarioHabilitado;
    }
}
    
27.05.2015 / 12:45
-2
  

public class Person {public int ID {get;   set; } public string Name {get; set; }}

     

public class PersonModel: Person {public bool Selected {get;   set; }}

     

private voids ButaoClick () {List list = new   List (); list = Loadadded ();   productsDatagrid.ItemsSource = list; }

It makes sense

    
11.03.2015 / 04:25