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

Problem with passing value to custom GridDataCellElement.

2 Answers 248 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Raymond
Top achievements
Rank 1
Raymond asked on 12 Oct 2010, 11:33 AM

Hi

I created my custom cell and column:

 Cell:

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Telerik.WinControls.UI;
using System.Windows.Forms;
using System.Drawing;
using System.Diagnostics;
  
namespace GridViewUnboundMode
{
    public class EditableCheckBoxCellElement : GridDataCellElement
    {
        private RadCheckBoxElement _chk;
        private RadTextBoxElement _txtBox;
        private RadLabelElement _lbl;
  
        public EditableCheckBoxCellElement(GridViewColumn column, GridRowElement row)
            : base(column, row)
        {
        }
  
        protected override void CreateChildElements()
        {
            base.CreateChildElements();
  
            _chk = new RadCheckBoxElement();
            _chk.Margin = new Padding(0, 2, 0, 0);
            _chk.MinSize = new Size(20, 20);
            _chk.Text = string.Empty;
            _chk.ToggleState = Telerik.WinControls.Enumerations.ToggleState.Off;
            _chk.ToggleStateChanged += new StateChangedEventHandler(_chk_ToggleStateChanged);
  
            _txtBox = new RadTextBoxElement();
            _txtBox.Margin = new Padding(0, 2, 0, 0);
            _txtBox.MinSize = new Size(10, 20);
            _txtBox.Text = "sss";
  
            _lbl = new RadLabelElement();
            _lbl.Text = "Not assigned";
            _lbl.Margin = new Padding(0, 4, 0, 0);
            _lbl.MinSize = new Size(1, 20);
            _lbl.TextWrap = false;
            _lbl.Font = new Font(_lbl.Font.Name, _lbl.Font.Size, FontStyle.Italic);
            _lbl.ForeColor = Color.Gray;
  
            this.Children.Add(_chk);
            this.Children.Add(_lbl);
        }
  
        void _chk_ToggleStateChanged(object sender, StateChangedEventArgs args)
        {
            if (_chk.ToggleState == Telerik.WinControls.Enumerations.ToggleState.On)
            {
                if (this.Children.Contains(_lbl))
                { this.Children.Remove(_lbl); }
                this.Children.Add(_txtBox);
            }
            else if (_chk.ToggleState == Telerik.WinControls.Enumerations.ToggleState.Off)
            {
                if (this.Children.Contains(_txtBox))
                { this.Children.Remove(_txtBox); }
                this.Children.Add(_lbl);
            }
        }
   
  
        protected override SizeF ArrangeOverride(SizeF finalSize)
        {
            if (this.Children.Count == 2)
            {
                this.Children[0].Arrange(new RectangleF(0, 0, 20, 20));             
                this.Children[1].Arrange(new RectangleF(21, 0, finalSize.Width - 28, 20));
                if (this.Children[1] == _lbl)
                { _lbl.MinSize = new Size(Convert.ToInt32(finalSize.Width - 28), 20); }
            }
            return finalSize;
        }
  
        public override object Value
        {
            get
            {
                return _txtBox.Text;
            }
            set
            {
                _txtBox.Text = value.ToString();
            }
        }
  
        public bool IsChecked
        {
            get
            {
                return _chk.ToggleState == Telerik.WinControls.Enumerations.ToggleState.On;
            }
            set
            {
                if (value)
                { _chk.ToggleState = Telerik.WinControls.Enumerations.ToggleState.On; }
                else
                { _chk.ToggleState = Telerik.WinControls.Enumerations.ToggleState.Off; }
            }
        }
    }
}

 


 Column:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Telerik.WinControls.UI;
  
namespace GridViewUnboundMode
{
    public class EditableCheckBoxColumn : GridViewDataColumn
    {
        public EditableCheckBoxColumn(): base()
        {
        }
   
        public override Type GetCellType(GridViewRowInfo row)
        {
            if (row is GridViewDataRowInfo)
            {
                return typeof(EditableCheckBoxCellElement);
            }        
            return base.GetCellType(row);
        }
    }
}

 

When I create new row I do something like this:

private void btnAddNewRows_Click(object sender, EventArgs e)
{
    GridViewRowInfo rowInfo = this.radGridView1.Rows.AddNew();
    int columnIndex = 0;
    int rowIndex = rowInfo.Index;
    foreach (GridViewCellInfo cellInfo in rowInfo.Cells)
    {
        cellInfo.Value = string.Format("{0}-{1}", columnIndex, rowIndex);
        columnIndex++;
    }
}

 

The problem is that in this loop when I set GridViewCellInfo.Value to some value, property EditableCheckBoxCellElement.Value is not set to this value.


I also don`t know how to get access to EditableCheckBoxCellElement because I need to control property EditableCheckBoxCellElement.IsChecked.

 


How can I control values for controls that are inside my EditableCheckBoxCellElement?

 


Data binding is not an option in my case.

Regards

 

2 Answers, 1 is accepted

Sort by
0
Accepted
Emanuel Varga
Top achievements
Rank 1
answered on 12 Oct 2010, 01:13 PM
Hello Raymond,

You can access the custom cell element through the CellFormatting event. You can force this event to fire by calling the InvalidateCell or InvalidateRow event.

Please consider the following example:
void radGridView1_CellFormatting(object sender, CellFormattingEventArgs e)
{
    if (e.CellElement.ColumnInfo.Name == "CustomColumn")
    {
        e.CellElement.Value = string.Format("{0}-{1}", e.CellElement.ColumnIndex, e.CellElement.RowIndex);
    }
}

Hope this helps, if you have any other questions or comments, please let me know,

Best Regards,
Emanuel Varga
0
Raymond
Top achievements
Rank 1
answered on 13 Oct 2010, 12:20 PM
it works ok, thanks
Tags
GridView
Asked by
Raymond
Top achievements
Rank 1
Answers by
Emanuel Varga
Top achievements
Rank 1
Raymond
Top achievements
Rank 1
Share this question
or