This is a migrated thread and some comments may be shown as answers.

[Solved] Checkbox names

12 Answers 116 Views
GridView
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Keith Stephens
Top achievements
Rank 1
Keith Stephens asked on 15 Sep 2010, 09:29 PM
I have a gridview and on this gridview I have 2 fields that have a cell template of a checkbox.

The checking of these boxes will have very similar functionality so, is there a way were I can tell which checkbox was checked so I can just have one event.

The functionality will just be:  If checkbox1 is checked update fields 2 and 3, if checkbox2 is checked update fields 8 and 9.
Hope this makes sense.
KS.

12 Answers, 1 is accepted

Sort by
0
Vlad
Telerik team
answered on 16 Sep 2010, 08:35 AM
Hello,

 You can give these CheckBoxes names and in the event you can cast the sender to CheckBox and and check the current CheckBox Name.

Kind regards,
Vlad
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Keith Stephens
Top achievements
Rank 1
answered on 16 Sep 2010, 02:44 PM
Great thanks,

One more question.  When my page loads the gridview is empty until the user selects a name.
But when the gridview loads the _Checked(object sender, RoutedEventArgs e) event fires 2 times for each of the checkboxes I have.
Then when I check one of them, the event fires multiple times.
0
Keith Stephens
Top achievements
Rank 1
answered on 16 Sep 2010, 04:15 PM
And one more question.

I have this code:
private void chkOverRide_Checked(object sender, RoutedEventArgs e)
        {
            CheckBox chk = (CheckBox)sender;
            string chkName = chk.Name;
  
            var row = chk.ParentOfType<GridViewRow>();
  
            AATshipAgree agrow = new AATshipAgree();
            agrow = (AATshipAgree)row.DataContext;
              
  
            if (chkName == "chkCostOverRide")
            {
                if (row != null)
                {
                    row.Cells[10].IsEnabled = true;
                    row.Cells[12].IsEnabled = true;
                    //  row.Cells[14].IsEnabled=true;
  
                }
            }
            else if (chkName == "chkHazOverRide")
            {
                if (row != null)
                {
                    row.Cells[18].IsEnabled = true;
                    row.Cells[20].IsEnabled = true;
                    //  row.Cells[22].IsEnabled = true;
  
  
                }
  
            }
  
        }

It works until it reaches the commented out row.Cells.  Cells 14, and 22.  These cells are the cells right after the checkbox cell.  So it seems that once it hits this cell it calls the Checked event.  I need this event to be called after the row is complete.  The cells that I need to enable or disable are after the check box.

Thanks,
KS
0
Keith Stephens
Top achievements
Rank 1
answered on 16 Sep 2010, 07:55 PM
I've noticed as I check and uncheck the box, I the code above does not work anymore.

I am wanting to enable and disable cells based on checking and unchecking the checkbox.  Apparently what I have is not working.

Can a sample be given.

Thanks,
KS
0
Keith Stephens
Top achievements
Rank 1
answered on 20 Sep 2010, 03:38 PM
I am still having problems here.
The code I included in the other reply does not work as I had thought.

Thanks,
KS
0
Maya
Telerik team
answered on 21 Sep 2010, 01:27 PM
Hello Keith Stephens,

I have prepared a sample project implementing the code-snippet you have provided and everything works as expected. You may use it as a reference and in case there is some misunderstanding according to your requirements, you may change the application in the way you want and send it back.

Kind regards,
Maya
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Keith Stephens
Top achievements
Rank 1
answered on 21 Sep 2010, 01:55 PM
Your code example does work fine, I even created an unchecked event and copied the code and set the isenabled = true;

And in my project the checked event works fine, but the unchecked event, for some reason the object
 AATshipAgree  agrow = new AATshipAgree();
            agrow = row.DataContext as AATshipAgree;


is blank.
private void chkOverRide_Checked(object sender, RoutedEventArgs e)
        {
            CheckBox chk = (CheckBox)sender;
            string chkName = chk.Name;
  
            var row = chk.ParentOfType<GridViewRow>();
  
            AATshipAgree  agrow = new AATshipAgree();
            agrow = row.DataContext as AATshipAgree;
  
             
            if (chkName == "checkBox1")
            {
                if (row != null)
                {
                    row.Cells[10].IsEnabled = true;
                    row.Cells[12].IsEnabled = true;
                    row.Cells[14].IsEnabled = true;
                }
            }
            else if (chkName == "checkBox2")
            {
                if (row != null)
                {
                    row.Cells[18].IsEnabled = true;
                    row.Cells[20].IsEnabled = true;
                    row.Cells[22].IsEnabled = true;
                }
  
            }
}
 private void chkCostOverRide_Unchecked(object sender, RoutedEventArgs e)
        {
  
            CheckBox chk = (CheckBox)sender;
            string chkName = chk.Name;
  
            var row = chk.ParentOfType<GridViewRow>();
  
            AATshipAgree agrow = new AATshipAgree();
            agrow = row.DataContext as AATshipAgree;
  
  
            if (chkName == "checkBox1")
            {
                if (row != null)
                {
                    row.Cells[10].IsEnabled = false;
                    row.Cells[12].IsEnabled = false;
                    row.Cells[14].IsEnabled = false;
                }
            }
            else if (chkName == "checkBox2")
            {
                if (row != null)
                {
                    row.Cells[18].IsEnabled = false;
                    row.Cells[20].IsEnabled = false;
                    row.Cells[22].IsEnabled = false;
                }
  
            }
}

Let me know if you see anything I might be doing wrong.

Thanks,
KS
0
Maya
Telerik team
answered on 21 Sep 2010, 03:11 PM
Hello Keith Stephens,

I have handled the Unchecked event of the CheckBox-es in the sample project I sent to you, but unfortunately I am not able to reproduce it. Thus in order to provide you with any further solution, I would need a bit more information about your project, the grid's ItemsSource and the way it is set. Furthermore, what is the exact implementation and definition of your AATshipAggree class?

Regards,
Maya
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Keith Stephens
Top achievements
Rank 1
answered on 21 Sep 2010, 03:58 PM
I don't think any of that is the problem.  I am trying various code testing, and before I could get the checked to work but the unchecked event would give me a null Ref error on row.datacontent (it was null).

Now I have just my checked event and the unchecked event is commented out.  And with this code I see that the checked event gets called 2 times.  The first time it goes throught and disables my cells but then the second time row.datacontent is null and throws a null error.  The times when I had it working was when I had commented out the code for cells 14, and 22 because these would return an out of range error.

private void chkOverRide_Checked(object sender, RoutedEventArgs e)
       {             
           CheckBox chk = (CheckBox)sender;
           string chkName = chk.Name;            
           var row = chk.ParentOfType<GridViewRow>();     
                      
           AATshipAgree agrow = new AATshipAgree();
           agrow = (AATshipAgree)row.DataContext;
            
           if (chkName == "chkCostOverRide")
           {
               if (row != null)
               {         
                   row.Cells[10].IsEnabled = true;
                   row.Cells[12].IsEnabled = true;
                   //row.Cells[14].IsEnabled = true;                  
               }
           }
           else if (chkName == "chkHazOverRide")
           {
               if (row != null)
               {                  
                   row.Cells[18].IsEnabled = true;
                   row.Cells[20].IsEnabled = true;
                  // row.Cells[22].IsEnabled = true;
               }
           }
       }
0
Keith Stephens
Top achievements
Rank 1
answered on 21 Sep 2010, 04:52 PM
I was able to break the example, with the error I am getting when I get mine to work, and I have to comment out the cells affected.
I just created a bool property, and set then to your GetClubs method.

If any of the players have a true for this new bool, then the app breaks.
public static ObservableCollection<Club> GetClubs()
    {
        ObservableCollection<Club> clubs = new ObservableCollection<Club>();
        Club club;
        // Liverpool
        club = new Club("Liverpool", new DateTime(1892, 1, 1), 45362);
        //club.players.Add(new Player("test",99,Position.DF,"England",true));
        club.Players.Add(new Player("Pepe Reina", 25, Position.GK, "Spain", false));
        club.Players.Add(new Player("Jamie Carragher", 23, Position.DF, "England",false));
        club.Players.Add(new Player("Steven Gerrard", 8, Position.MF, "England",true));
        club.Players.Add(new Player("Fernando Torres", 9, Position.FW, "Spain",false));
        clubs.Add(club);
        // Manchester Utd.
        club = new Club("Manchester Utd.", new DateTime(1878, 1, 1), 76212);
        club.Players.Add(new Player("Edwin van der Sar", 1, Position.GK, "Netherlands", false));
        club.Players.Add(new Player("Rio Ferdinand", 5, Position.DF, "England", true));
        club.Players.Add(new Player("Ryan Giggs", 11, Position.MF, "Wales", true));
        club.Players.Add(new Player("Wayne Rooney", 10, Position.FW, "England", true));
        clubs.Add(club);
        // Chelsea
        club = new Club("Chelsea", new DateTime(1905, 1, 1), 42055);
        club.Players.Add(new Player("Petr Cech", 1, Position.GK, "Czech Republic", true));
        club.Players.Add(new Player("John Terry", 26, Position.DF, "England", true));
        club.Players.Add(new Player("Frank Lampard", 8, Position.MF, "England", true));
        club.Players.Add(new Player("Nicolas Anelka", 39, Position.FW, "France", true));
        clubs.Add(club);
        // Arsenal
        club = new Club("Arsenal", new DateTime(1886, 1, 1), 60355);
        club.Players.Add(new Player("Manuel Almunia", 1, Position.GK, "Spain", true));
        club.Players.Add(new Player("Gaël Clichy", 22, Position.DF, "France", true));
        club.Players.Add(new Player("Cesc Fàbregas", 4, Position.MF, "Spain", true));
        club.Players.Add(new Player("Robin van Persie", 11, Position.FW, "Netherlands", true));
        clubs.Add(club);
        return clubs;
    }
using System.ComponentModel;
using System.Collections.ObjectModel;
using System.Linq;
  
namespace CheckBox_EnableCells
{
    /// <summary>
    /// A football player.
    /// </summary>
    public class Player : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
  
        private string name;
        private int number;
        private Position position;
        private string country;
        private bool active;
  
        public string Name
        {
            get { return this.name; }
            set
            {
                if (value != this.name)
                {
                    this.name = value;
                    this.OnPropertyChanged("Name");
                }
            }
        }
  
        public int Number
        {
            get { return this.number; }
            set
            {
                if (value != this.number)
                {
                    this.number = value;
                    this.OnPropertyChanged("Number");
                }
            }
        }
  
        public Position Position
        {
            get { return this.position; }
            set
            {
                if (value != this.position)
                {
                    this.position = value;
                    this.OnPropertyChanged("Position");
                }
            }
        }
  
        public string Country
        {
            get { return this.country; }
            set
            {
                if (value != this.country)
                {
                    this.country = value;
                    this.OnPropertyChanged("Country");
                }
            }
        }
  
        public bool Active
        {
            get { return this.active; }
            set
            {
                if (value != this.active)
                {
                    this.active = value;
                    this.OnPropertyChanged("Active");
                }
            }
        }
  
        public Player()
        {
  
        }
  
        public Player(string name, int number, Position position, string country, bool active)
        {
            this.name = name;
            this.number = number;
            this.position = position;
            this.country = country;
            this.active = active;
  
        }
  
        protected virtual void OnPropertyChanged(PropertyChangedEventArgs args)
        {
            PropertyChangedEventHandler handler = this.PropertyChanged;
            if (handler != null)
            {
                handler(this, args);
            }
        }
  
        private void OnPropertyChanged(string propertyName)
        {
            this.OnPropertyChanged(new PropertyChangedEventArgs(propertyName));
        }
  
        public override string ToString()
        {
            return this.Name;
        }
  
        public static ObservableCollection<Player> GetPlayers()
        {
            return new ObservableCollection<Player>(Club.GetClubs().SelectMany(c => c.Players));
        }
    }
}

And in the xaml I am setting
<DataTemplate>
                            <CheckBox IsChecked="{Binding Active}" x:Name="checkBox1" Unchecked="checkBox1_Unchecked" Checked="checkBox1_Checked" />
                        </DataTemplate>

Do I need to bind differntly, instead of using ischecked?
0
Keith Stephens
Top achievements
Rank 1
answered on 21 Sep 2010, 05:37 PM
Just as I thought, if in your grid you have a checkbox, and you are trying to disable/enable rows that come after the checkbox, based on if it's checked or not you will get an error.  If the cells are before the checkbox then it works just fine.

I am binding the checkbox to a boolean value so if it's true I want to disable cells and if it's false I leave them enabled.

I just hope  the client is fine with me reagranging the cells.

KS.
0
Accepted
Maya
Telerik team
answered on 22 Sep 2010, 03:48 PM
Hi Keith Stephens,

The problem in the case when setting a Property bound to IsChecked of the CheckBox is that not all of the cells are rendered during the initial firing of the Checked event. However, as an appropriate workaround, you may use the IsReadOnlyBinding Property of the grid. It sets a particular cell to be ReadOnly or not. Thus by means of a IValueConverter, you may connect the value of that property to the Active Property of the Player class. 
I am sending you a sample project illustrating the proposed solution.

 

Kind regards,
Maya
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
Tags
GridView
Asked by
Keith Stephens
Top achievements
Rank 1
Answers by
Vlad
Telerik team
Keith Stephens
Top achievements
Rank 1
Maya
Telerik team
Share this question
or