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

Event ValueChanged when I set checkbox programmatically

8 Answers 659 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Fernan
Top achievements
Rank 1
Fernan asked on 24 May 2010, 04:14 PM
Hi,

I've put a custom checkbox in the header in order to select/unselect all row's checkbox. I use this code:

public class CheckBoxHeaderCell : GridHeaderCellElement 
    { 
        RadCheckBoxElement checkbox; 
 
        public CheckBoxHeaderCell(GridViewColumn column, GridRowElement row) 
            : base(column, row) 
        { 
        } 
 
        protected override void DisposeManagedResources() 
        { 
            checkbox.ToggleStateChanged -= new StateChangedEventHandler(checkbox_ToggleStateChanged); 
            base.DisposeManagedResources(); 
        } 
 
 
        protected override void CreateChildElements() 
        { 
            base.CreateChildElements(); 
            checkbox = new RadCheckBoxElement(); 
            checkbox.ToggleStateChanged += new StateChangedEventHandler(checkbox_ToggleStateChanged); 
            this.Children.Add(checkbox); 
            ApplyThemeToElement(checkbox, "ControlDefault"); 
        } 
 
        protected override SizeF ArrangeOverride(SizeF finalSize) 
        { 
            base.ArrangeOverride(finalSize); 
            if (checkbox != null
            { 
                RectangleF rect = GetClientRectangle(finalSize); 
                checkbox.Arrange(new RectangleF(rect.Right - 20, (rect.Height - 20) / 2, 20, 20)); 
            } 
            return finalSize; 
        } 
 
        private void checkbox_ToggleStateChanged(object sender, StateChangedEventArgs args) 
        { 
            this.GridControl.BeginEdit();                   
            for (int i = 0; i < this.GridControl.Rows.Count; i++) 
            { 
                this.GridControl.Rows[i].IsSelected = true
                this.GridControl.Rows[i].IsCurrent = true;                 
                this.GridControl.Rows[i].Cells["Mostrar"].Value = this.checkbox.IsChecked; 
            } 
            this.GridControl.EndEdit(); 
        } 
 
        private void ApplyThemeToElement(RadItem item, string themeName) 
        { 
            DefaultStyleBuilder builder = ThemeResolutionService.GetStyleSheetBuilder((RadControl)item.ElementTree.Control, 
                item.GetThemeEffectiveType().FullName, string.Empty, themeName) as DefaultStyleBuilder; 
 
            if (builder != null
            { 
                item.Style = new XmlStyleSheet(builder.Style).GetStyleSheet(); 
            } 
        } 
    }  

and:

 private void radGridView1_CreateCell(object sender, GridViewCreateCellEventArgs e) 
        { 
            if (e.Row is GridTableHeaderRowElement && e.Column.HeaderText == "Mostrar"
            { 
                e.CellElement = new SMPDataAnalyzer.TelerikHeaderGrid.CheckBoxHeaderCell(e.Column, e.Row); 
            }  
        } 


This works fine. But the problem is the following:

I have to detect when the row's checkbox value is change to true or false. I use the ValueChanged event :
private void radGridView1_ValueChanged(object sender, EventArgs e) 
        { 
            try 
            { 
                if (this.radGridView1.ActiveEditor is RadCheckBoxEditor) 
                { 
                    if (radGridView1.CurrentRow.Tag != null
                    { 
                        if ((bool)(this.radGridView1.ActiveEditor.Value) && !tendencias.ContainsKey(Convert.ToInt32(radGridView1.CurrentRow.Tag))) 
                        { 
                            if (radGridView1.SelectedRows.Count > 0) 
                            { 
                                medidastendencia = Controller.CargarValoresMedidas 
                                    (long.Parse(radGridView1.CurrentRow.Cells["PuntoId"].Value.ToString()), 
                                    fechaIni, fechaFin, 
                                    long.Parse(radGridView1.SelectedRows[0].Cells["TipoEspectro"].Value.ToString()), 
                                    long.Parse(radGridView1.SelectedRows[0].Cells["Nbanda"].Value.ToString())); 
 
                                if (medidastendencia.Count > 0) 
                                { 
                                    ScatterPlot scatterPlot = new ScatterPlot(); 
                                    scatterPlot.XAxis = xAxis2; 
                                    scatterPlot.YAxis = yAxis2; 
                                    scatterPlot.Tag = Convert.ToInt32(radGridView1.CurrentRow.Tag); 
                                    scatterGraphTendencias.Plots.Add(scatterPlot); 
                                    int i = 0; 
                                    double[] fechas = new double[medidastendencia.Count]; 
                                    double[] values = new double[medidastendencia.Count]; 
                                    foreach (MedidaTendencia mt in medidastendencia) 
                                    { 
                                        fechas[i] = (Double)(DataConverter.Convert(mt.FechaMedida, typeof(Double))); 
                                        values[i] = mt.Valor; 
                                        i++; 
                                    } 
                                    scatterPlot.PlotXY(fechas, values); 
                                    xyCursor2.Plot = scatterPlot; 
                                    xyCursor2.SnapMode = CursorSnapMode.NearestPoint; 
                                    xyCursor2.MoveNext(); 
                                    xyCursor2.MovePrevious(); 
                                    xyCursor2.Visible = true
                                    if (!isTendenciaVisible) 
                                    { 
                                        isTendenciaVisible = true
                                        activeTend(); 
                                    } 
                                    tendencias.Add(Convert.ToInt32(radGridView1.CurrentRow.Tag), values); 
                                } 
                                else 
                                { 
                                    MessageBox.Show(Properties.Resources.ERROR_MEDIDAS_TEND); 
                                } 
                            } 
                        } 
                        else 
                        { 
                            //Se ha puesto a false 
                            if (tendencias.ContainsKey(Convert.ToInt32(radGridView1.CurrentRow.Tag))) 
                            { 
                                int i = 0; 
                                foreach (ScatterPlot plot in scatterGraphTendencias.Plots) 
                                { 
                                    if (plot.Tag.ToString() == radGridView1.CurrentRow.Tag.ToString()) 
                                    { 
                                        if (i > 1) 
                                        { 
                                            xyCursor2.Plot = scatterGraphTendencias.Plots[i - 1]; 
                                            xyCursor2.MoveNext(); 
                                            xyCursor2.MovePrevious(); 
                                        } 
                                        else 
                                        { 
                                            if (scatterGraphTendencias.Plots.Count > 2) 
                                            { 
                                                xyCursor2.Plot = scatterGraphTendencias.Plots[i + 1]; 
                                                xyCursor2.MoveNext(); 
                                                xyCursor2.MovePrevious(); 
                                            } 
                                            else 
                                            { 
                                                xyCursor2.Plot = scatterPlot2; 
                                                xyCursor2.Visible = false
 
                                                desactiveTend(); 
                                            } 
                                        } 
                                        scatterGraphTendencias.Plots.Remove(plot); 
                                        plot.Dispose(); 
                                        break
                                    } 
                                    i++; 
                                } 
                                tendencias.Remove(Convert.ToInt32(radGridView1.CurrentRow.Tag)); 
                            } 
                        } 
                    } 
                } 
            } 
            catch (Exception ex) 
            { 
                string message = ex.Message; 
            } 
        } 


But..., when I use the header checkbox in order to select/unselect all,the ActiveEditor property is null.

¿How do I this?

Thank you,

Fernan,

8 Answers, 1 is accepted

Sort by
0
Svett
Telerik team
answered on 27 May 2010, 07:35 AM
Hello Fernan,

GridHeaderCellElement is not a data cell. Therefore, it does not store a value from your data source. The ActiveEditor property of RadGridView points to editors contained only in data cells, so you cannot set your editor to the ActiveEditor property. As a workaround, you can use RadGridView's Tag property as a field where you can store the header cell's check box when you change its toggle state:
private void checkbox_ToggleStateChanged(object sender, StateChangedEventArgs args)
{
    this.GridControl.Tag = this.checkbox;
    this.GridControl.BeginEdit();
    for (int i = 0; i < this.GridControl.Rows.Count; i++)
    {
        this.GridControl.Rows[i].Cells[this.ColumnIndex].Value = this.checkbox.IsChecked;
    }
 
    this.GridControl.EndEdit();
    this.GridControl.Tag = null;
}

Then you can access it in ValueChanged event:
private void radGridView1_ValueChanged(object sender, EventArgs e)
{
     if (this.radGridView1.ActiveEditor is RadCheckBoxEditor || this.radGridView1.Tag is RadCheckBoxElement)
     {
    // TO DO: your operation here
     }
     else
     {
    // TO DO: your else-operation here
     }
}


All the best,
Svett
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
Fernan
Top achievements
Rank 1
answered on 27 May 2010, 08:06 AM
Thank you, it's a perfect solution!

Fernan,
0
Bao
Top achievements
Rank 1
answered on 26 Sep 2017, 08:13 PM
Can i check/uncheck for checkbox header through the button ?
0
Bao
Top achievements
Rank 1
answered on 26 Sep 2017, 08:45 PM
I found it . 
0
Oliver
Top achievements
Rank 1
answered on 12 Oct 2017, 07:10 AM

Hi Bao,

can you explain how to set the HeaderCheckbox programmatically?
Thanks.

Oliver

0
Dess | Tech Support Engineer, Principal
Telerik team
answered on 12 Oct 2017, 08:58 AM
Hello, Oliver,  

Thank you for writing.  

In order to toggle the checkbox in the header cell you can use the following code snippet: 
GridViewCheckBoxColumn checkBoxColumn = this.radGridView1.Columns["Discontinued"] as GridViewCheckBoxColumn;
checkBoxColumn.EnableHeaderCheckBox = true;
checkBoxColumn.Checked = Telerik.WinControls.Enumerations.ToggleState.On;

I hope this information helps. Should you have further questions I would be glad to help.

Regards,
Dess
Progress Telerik
Try our brand new, jQuery-free Angular components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
0
Oliver
Top achievements
Rank 1
answered on 12 Oct 2017, 09:42 AM

Hallo Dess,

thank you for writing.

The solution is in my case not sucessfull.

I want to toggle two Headercheckboxes.

If the col1 header checkbox is checked (all cells in col1 were checked too) and the header checkbox in col2 (and its cells) must be unchecked. 

Here my snippet:

private void ReSyncGridView_HeaderCellToggleStateChanged(object sender, GridViewHeaderCellEventArgs e)        {            string name = e.Column.Name; //colSync,colDelete            string value = e.State.ToString(); //On,Off            //toggeln            if (name == "colDelete" && value == "On")            {                //colSync auf 0 setzen                GridViewCheckBoxColumn checkBoxColumn = this.ReSyncGridView.Columns["colSync"] as GridViewCheckBoxColumn;                checkBoxColumn.EnableHeaderCheckBox = true;                checkBoxColumn.Checked = Telerik.WinControls.Enumerations.ToggleState.Off;            }            if (name == "colSync" && value == "On")            {                //colDelete auf 0 setzen                GridViewCheckBoxColumn checkBoxColumn = this.ReSyncGridView.Columns["colDelete"] as GridViewCheckBoxColumn;                checkBoxColumn.EnableHeaderCheckBox = true;                checkBoxColumn.Checked = Telerik.WinControls.Enumerations.ToggleState.Off;            }        }

Habe you any idea, whats wrong with it?

Thanks for your help.

Oliver

 

0
Dess | Tech Support Engineer, Principal
Telerik team
answered on 12 Oct 2017, 10:18 AM
Hello, Oliver,    

Thank you for writing back. 

I have attached a sample project for your reference demonstrating how to untoggle a certain GridViewCheckBoxColumn when another one is toggled.

I hope this information helps. If you have any additional questions, please let me know. 

 Regards,
Dess
Progress Telerik
Try our brand new, jQuery-free Angular components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
Tags
GridView
Asked by
Fernan
Top achievements
Rank 1
Answers by
Svett
Telerik team
Fernan
Top achievements
Rank 1
Bao
Top achievements
Rank 1
Oliver
Top achievements
Rank 1
Dess | Tech Support Engineer, Principal
Telerik team
Share this question
or