locked
Getting problem in datagrid wpf RRS feed

  • Question

  • Hi,

       I am getting problem in data grid in wpf.I have a data grid with checkboxes of 4 rows n 4 columns.I want that when i checked first rows first check box first rows second check box will also be checked. How can i get that check box value..

    thanks

     

    Thursday, October 24, 2013 10:30 AM

Answers

  • Why do you want to get a reference to the visual CheckBox control? That is not what I suggested in my previous post.

    Anyway, if you want to do this for whatever reason, you need to iterate through the items in the DataGrid and then generate each DataGridRow by using the ContainerFromItem or ContainerFromIndex method of the ItemContainerGenerator of the DataGrid:

    foreach (object item in dGrid.Items)
                {
                    DataGridRow row = dGrid.ItemContainerGenerator.ContainerFromItem(item) as DataGridRow;
                    if (row != null)
                    {
                        DataGridColumn column = dGrid.Columns.Where(c => c.Header.Equals("xyz")).FirstOrDefault();
                        DataGridCellsPresenter presenter = FindVisualChild<DataGridCellsPresenter>(row);
                        DataGridCell cell = presenter.ItemContainerGenerator.ContainerFromIndex(dGrid.Columns.IndexOf(column)) as DataGridCell;
                        if (cell.Column is DataGridTemplateColumn)
                        {
                            DataGridTemplateColumn templateColumn = cell.Column as DataGridTemplateColumn;
                            ContentPresenter ct = templateColumn.GetCellContent(row) as ContentPresenter;
                            DataTemplate dt = ct.ContentTemplate;
                            CheckBox chk1 = dt.FindName("ChkAll", ct) as CheckBox;
                            MessageBox.Show(chk1.IsChecked.ToString());
                        }
                    }
                }
    

    • Marked as answer by Jason Dot Wang Wednesday, October 30, 2013 5:34 AM
    Saturday, October 26, 2013 8:23 PM

All replies

  • You should bind the DataGrid to a collection of items with a Boolean property for each CheckBox and then handle the logic in the setters of these properties:   

    <DataGrid x:Name="checkBoxGrid" CanUserAddRows="False" AutoGenerateColumns="False">
          <DataGrid.Columns>
          <DataGridCheckBoxColumn Header="1" Binding="{Binding FirstCheck, UpdateSourceTrigger=PropertyChanged}"/>
          <DataGridCheckBoxColumn Header="2" Binding="{Binding SecondCheck, UpdateSourceTrigger=PropertyChanged}"/>
          <DataGridCheckBoxColumn Header="3" Binding="{Binding ThirdCheck, UpdateSourceTrigger=PropertyChanged}"/>
          </DataGrid.Columns>
        </DataGrid>

          checkBoxGrid.ItemsSource = new List<CheckItem>
          {
            new CheckItem(),
            new CheckItem(),
            new CheckItem(),
            new CheckItem()
          };
    
    class CheckItem : INotifyPropertyChanged
      {
        private bool _firstChecked;
        public bool FirstCheck {
          get {
            return _firstChecked;
          }
          set{
            _firstChecked = value;
            NotifyPropertyChanged("FirstCheck");
    
            //also set the second check value here:
            this.SecondCheck = value;
          }
        }
    
        private bool _secCheck;
        public bool SecondCheck {
          get {
            return _secCheck;
          }
          set {
            _secCheck = value;
            NotifyPropertyChanged("SecondCheck");
          }
        }
    
        private bool _thCheck;
        public bool ThirdCheck {
          get {
            return _thCheck;
          }
          set {
            _thCheck = value;
            NotifyPropertyChanged("ThirdCheck");
          }
        }
    
    
        public event PropertyChangedEventHandler PropertyChanged;
        private void NotifyPropertyChanged(string propertyName) {
          if (PropertyChanged != null) {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
          }
        }
      }
    

     

    Thursday, October 24, 2013 11:31 AM
  • Hi,

          Thanks for reply but i m using database to fill datagrid.I want to know how  can i get second check box when i clicked first check box of any row.

     thanks

       

    Thursday, October 24, 2013 11:42 AM
  • Hi Swati

    You can attach an event with the checkbox like '_Checked Event'. Then whenever user checks it its event will be called. Then by using that called event properties you can identify which checkbox is checked and write you logic accordingly.


    Do not Forget to Vote as Answer/Helpful, please. It encourages us to help you.

    Thursday, October 24, 2013 12:07 PM
  • Hi 

        thanks for reply i am doing the same but i want that when taht check box is checked according to that other check boxes of that rowwill also be checked.

    Thanks

    Thursday, October 24, 2013 12:39 PM
  • If the DataGrid is data-bound (to a collection, DataView or anything else), you should not set the value of the IsChecked property of of the CheckBox control programmtically as this will break the binding to the source property or column. This means that you won't be able to easily retrieve the current values from the DataGrid.

    I would recommend you to load the values from your DataTable into a collection of objects of some type with Boolean properties (like the CheckItem class in my previous post) for each CheckBox you want to display, and then use this collection as the ItemsSource for the DataGrid instead of the DataView of the DataTable itself.

    • Proposed as answer by Mr. Javaman II Friday, October 25, 2013 3:57 AM
    Thursday, October 24, 2013 12:42 PM
  • Hi,

          Thanks for reply I will try this..

    thanks

    Friday, October 25, 2013 3:34 AM
  • Hi,

              I m using below code to get cell content from data grid column

     foreach (DataGridRow r in row)
                {
                    DataRowView rv = (DataRowView)r.Item;
                    foreach (DataGridColumn column in data.Columns)
                    {
                        DataTemplate dd = column.GetCellContent(r).DataContext as DataTemplate;

                        if (column.GetCellContent(r) is TextBlock)
                        {
                            TextBlock cellContent = column.GetCellContent(r) as TextBlock;
                            MessageBox.Show(cellContent.Text);
                        }
                        else if (column.GetCellContent(r) is Check Box)
                        {
                            CheckBox chk1 = column.GetCellContent(r) as CheckBox;
                            MessageBox.Show(chk1.IsChecked.ToString());
                        }
                    }
                }

    at xaml side i have a datagridtemplate

      <DataGridTemplateColumn Header="xyz" Width="50">
                            <DataGridTemplateColumn.CellTemplate >
                                <DataTemplate>
                                    <CheckBox Name="ChkAll" IsChecked="True"  IsEnabled="False"   IsThreeState="False" Margin="10,0,0,0"  HorizontalContentAlignment="Center" HorizontalAlignment="Left"  VerticalAlignment="Center"/>

                                </DataTemplate>
                            </DataGridTemplateColumn.CellTemplate>

                        </DataGridTemplateColumn>

    but i m not able to get check box from that code plz help me out.

    Thanks

    Saturday, October 26, 2013 6:15 AM
  • Why do you want to get a reference to the visual CheckBox control? That is not what I suggested in my previous post.

    Anyway, if you want to do this for whatever reason, you need to iterate through the items in the DataGrid and then generate each DataGridRow by using the ContainerFromItem or ContainerFromIndex method of the ItemContainerGenerator of the DataGrid:

    foreach (object item in dGrid.Items)
                {
                    DataGridRow row = dGrid.ItemContainerGenerator.ContainerFromItem(item) as DataGridRow;
                    if (row != null)
                    {
                        DataGridColumn column = dGrid.Columns.Where(c => c.Header.Equals("xyz")).FirstOrDefault();
                        DataGridCellsPresenter presenter = FindVisualChild<DataGridCellsPresenter>(row);
                        DataGridCell cell = presenter.ItemContainerGenerator.ContainerFromIndex(dGrid.Columns.IndexOf(column)) as DataGridCell;
                        if (cell.Column is DataGridTemplateColumn)
                        {
                            DataGridTemplateColumn templateColumn = cell.Column as DataGridTemplateColumn;
                            ContentPresenter ct = templateColumn.GetCellContent(row) as ContentPresenter;
                            DataTemplate dt = ct.ContentTemplate;
                            CheckBox chk1 = dt.FindName("ChkAll", ct) as CheckBox;
                            MessageBox.Show(chk1.IsChecked.ToString());
                        }
                    }
                }
    

    • Marked as answer by Jason Dot Wang Wednesday, October 30, 2013 5:34 AM
    Saturday, October 26, 2013 8:23 PM
  • Hi,

      Thanks for reply,Now I am using itemcollection for loading my data to grid view  and my problem is resolved. Thanks so much...

    Swati

    Monday, October 28, 2013 12:39 PM