Quantcast
Channel: Windows Presentation Foundation (WPF) forum
Viewing all articles
Browse latest Browse all 18858

Add and manage Checkboxcolumn to the start of a bound datagrid

$
0
0

Hi,

    I have a DataGrid which is bound to a List<X>. I am looking for is add a column at the start of the DataGrid & if that checkbox is selecetd then only retrieve the values of the row, else ignore it. Similarly, when I set the ItemSource of the DataGrid, for each row added the 1st cols checkbox should be checked.

   I have not written any code for retrieving data from datagrid by checking the 1st col, as all rows got to be added to the List. When I set the ItemSource I am not able to check mark the 1st checkbox col of that row. The code is :

XML -

<DataGrid AutoGenerateColumns="False" Name="dgvSiSelection" BorderBrush="#FFB7B39D" Background="LightYellow" RowBackground="LightGray" AlternatingRowBackground="#FFFFFFF5" BorderThickness="10" FontSize="13" FontFamily="Segoe UI" CanUserAddRows="False"><DataGrid.Columns><DataGridCheckBoxColumn Binding="{Binding BoolProperty, Mode=TwoWay}"/><DataGridTextColumn Header="" Binding="{Binding SiHeader}" MinWidth="108" IsReadOnly="True"/><DataGridTextColumn Header="Number of Chemicals" Binding="{Binding S_NumberOfCases}" /><DataGridTextColumn Binding="{Binding S_Value1}" ><DataGridTextColumn.Header><Grid><Grid.ColumnDefinitions><ColumnDefinition/><ColumnDefinition/></Grid.ColumnDefinitions><TextBlock Grid.Column="1" Text="Value1"/><CheckBox Name="chkNValue1"  Grid.Column="0" Width="16" IsChecked="{Binding RelativeSource={RelativeSource AncestorType={x:Type DataGridColumn}}, Path=DataContext.AllItemsAreChecked}" /></Grid></DataGridTextColumn.Header></DataGridTextColumn><DataGridTextColumn Binding="{Binding S_Value2}" ><DataGridTextColumn.Header><Grid><Grid.ColumnDefinitions><ColumnDefinition/><ColumnDefinition/></Grid.ColumnDefinitions><TextBlock Grid.Column="1" Text="Value2"/><CheckBox Name="chkNValue2"  Grid.Column="0" Width="16" IsChecked="{Binding RelativeSource={RelativeSource AncestorType={x:Type DataGridColumn}}, Path=DataContext.AllItemsAreChecked}" /></Grid></DataGridTextColumn.Header></DataGridTextColumn></DataGrid.Columns></DataGrid></GroupBox>
    public class SIData
    {
        public string SiHeader { get; set; }
        public string S_NumberOfCases { get; set; }
        public int S_Value1 { get; set; }
        public int S_Value2 { get; set; }
    }


        private void LoadGUI()
        {
            List<SIData> sd = mw.xlsImpExp.GetStep5SiList();

            dgvSiSelection.ItemsSource = sd;

            // SI Grid - Check the ValueX checkbox on header - if any of the object contains Value1 or Value2 then check mark that respective cheader checkbox
            for (int i = 0; i < sd.Count; i++)
            {
                SIData s = sd[i];

                if (s.S_Value1 > 0)
                    chkNValue1.IsChecked = true;
                if (s.S_Value1 > 0)
                    chkNValue2.IsChecked = true;
            }

           // What code do I write to check mark the checkbox 1st col of rows that are entered ?

            return;
        }

Lets say the List has 2 objects, so in dataGrid 2 rows will be added. Now, I want to cehckmark the 1st col i.e. the checkbox of both rows. How do I achieve that ?

Secondly, I was also wondering, is their any better or easy code to set the checkmark of Value1 & Value2 if any of the rows have values of respective field greater than 0. I mean, if 1st row have "4" in S_Value1 & 2nd row has "0", then Value1 checkbox chkNValue1 should be clicked. If all the rows have value as "0" in S_Value2 field, then the chkNValue2 checkbox should not be selected.

Any help is appreciated. Please help me with this.

Thanks


Thanks
If you find any answer helpful, then click "Vote As Helpful" and if it also solves your question then also click"Mark As Answer".


Viewing all articles
Browse latest Browse all 18858

Trending Articles