mercredi 25 janvier 2017

DataGrid - Get CellValue after checking a CheckBox

In my application I need to get ID column content of row where clicked a checkbox. Currently I am using CheckBox_Click and datagrid_SelectionChanged but it is not working perfectly. The problem is that this handler only triggers after selection changed and it causes problems with counting properly. I've tried different handlers, but i was able to make only this one work(as it works). What can I change in my code or which event handler I should use?

selectionchanged:

private void datagrid_SelectionChanged(object sender, SelectionChangedEventArgs e)  
    {
        //somecode
        try
        {
            DataGrid dataGrid = sender as DataGrid;
            DataGridRow row = (DataGridRow)dataGrid.ItemContainerGenerator.ContainerFromIndex(dataGrid.SelectedIndex);
            if (row != null)
            {
                DataGridCell RowColumn = dataGrid.Columns[1].GetCellContent(row).Parent as DataGridCell;
                string CellValue = ((TextBlock)RowColumn.Content).Text;
                int i = Convert.ToInt32(CellValue);
                //somecode
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
            //somecode
    } here

checkboxclick:

        private void CheckBox_Click_1(object sender, RoutedEventArgs e) //Klikniecie w checkboxa
    {
            //somecode
        if (((CheckBox)sender).IsChecked == true)
        {
            //somecode
        }
        else
        {
            //somecode
        }
            //somecode
    }

XAML:

            <DataGrid x:Name="dataGrid" ItemsSource="{Binding}" AutoGenerateColumns="False" Margin="0" Grid.Row="2" OverridesDefaultStyle="True" IsManipulationEnabled="True"  CanUserAddRows="false" SelectionChanged="datagrid_SelectionChanged">
            <DataGrid.ItemBindingGroup>
                <BindingGroup/>
            </DataGrid.ItemBindingGroup>
            <DataGrid.Columns>
                <DataGridTemplateColumn>
                    <DataGridTemplateColumn.Header>
                        <CheckBox Content="All" Click="CheckBoxAll_Click" x:Name="headerCheckBox" Checked="headerCheckBox_Checked"/>
                    </DataGridTemplateColumn.Header>
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <CheckBox Name="chk" Margin="10 0 0 0" ClickMode="Press" Click="CheckBox_Click_1" IsChecked="{Binding IsChecked, ElementName=headerCheckBox, Mode=OneWay}" PreviewMouseLeftButtonDown="chk_PreviewMouseLeftButtonDown" />
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
                <DataGridTextColumn Binding="{Binding  Path=id}" Header="ID" Width="auto" />
                //Some columns
            </DataGrid.Columns>
        </DataGrid>

Thank for you help :)




Aucun commentaire:

Enregistrer un commentaire