locked
Checkbox column in datagrid RRS feed

  • Question

  • Hi,

     I have the following code:

        public partial class Search : INotifyPropertyChanged
        {
            public bool _select;
            public bool Select
            {
                get { return _select; }
                set
                {
                    _select = value;

                    // Call NotifyPropertyChanged when the property is updated
                    onPropertyChanged(this, "Select");
                   
                }
            }

            public string Description { get; set; }

            // Declare the PropertyChanged event
            public event PropertyChangedEventHandler PropertyChanged;

            public void onPropertyChanged(object sender, string propertyName)
            {
                if (PropertyChanged != null)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
                }
            } 
        }


    <DataTemplate>
         <CheckBox IsChecked="{Binding Select, Mode=TwoWay}" Checked="CheckBox_Checked" />
    </DataTemplate>

    private void CheckBox_Checked(object sender, RoutedEventArgs e)
    {

         CheckBox cb = (CheckBox)sender;
         Pearch p = cb.DataContext as PSearch;
         if (p.Select == true)
                    MessageBox.Show("check");
                else
                    MessageBox.Show("uncheck");
    }

    When I click on the check box (true), p.Select always return false but cb.IsChecked returns true.  But when I uncheck it, p.Select return true. What do I  do wrong.  Can anyone help?  Thanks!

     

    Tuesday, March 9, 2010 3:12 PM

Answers

  • Where did you do this check? You need to do this check on SeachData.PropertyChanged event, not on CheckBox.Checked event. Because as we already know, data is updated after CheckBox.Checked event.

    Or if you do not rely on the updated data, you could hookup both Checked and UnChecked event. In Checked event, show the textbox,. on UnChecked event, hide the textbox.

     

     

    Tuesday, March 9, 2010 5:16 PM

All replies

  •  You didn't do anything wrong. But your data is updated after the Checked event. That's why.

    Tuesday, March 9, 2010 3:23 PM
  • Thanks for your reply.  So, how do I capture/detect when the check box becomes true?

    Tuesday, March 9, 2010 4:47 PM
  • The answer is in the event handler you're using... if you attach a handler to "checkbox_checked", then you _already_ know the checkbox is checked because of the event handler you used...

    Tuesday, March 9, 2010 4:50 PM
  • Or you can check your Data that bind to the CheckBox. It you set two-way binding, that data should reflect your action. If you need to act upon that change, subscribe to data.PropertyChanged event.

     

    Tuesday, March 9, 2010 4:57 PM
  • Here is my scenario.  If the user checks on one of the records in the grid, then show a textblock (for example).  if all of the records in the grid are not check, then hide the textblock.  To know if one the records that were checked, I use the following LINQ statement:

    List<Search> testCount = Search.Where(a => a.Select == true).ToList<Search>();

    But testCount always return 0 eventhough there records that were checked.  Please help!!

     

    Tuesday, March 9, 2010 5:06 PM
  • Where did you do this check? You need to do this check on SeachData.PropertyChanged event, not on CheckBox.Checked event. Because as we already know, data is updated after CheckBox.Checked event.

    Or if you do not rely on the updated data, you could hookup both Checked and UnChecked event. In Checked event, show the textbox,. on UnChecked event, hide the textbox.

     

     

    Tuesday, March 9, 2010 5:16 PM
  • When we add a customized checkbox column to a datagrid in .net (windows application) , the default property allows to check or uncheck the column using a double click. On the first click it selects the column and on the second click the column is either checked or unchecked. To change this default property, we need to handle the click event on grid and modify the selected cell value. For more details please check the link below: http://www.mindfiresolutions.com/Checkbox-Column-In-Datagrid-857.php
    Wednesday, March 17, 2010 3:00 AM