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

Changing column types?

3 Answers 130 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Morten
Top achievements
Rank 1
Morten asked on 07 Sep 2011, 03:21 PM
Hi, 

I'm using the same RadGrid control for several different datasources, and thus I'd like to have it add columns automatically as it is now. However I'm having a problem converting specified int columns into checkbox columns (the values are all 0 and 1). Is there any way to cast/change the column from a GridNumericColumn into a checkbox column, or do you have any advice on how to resolve the situation?

3 Answers, 1 is accepted

Sort by
0
Morten
Top achievements
Rank 1
answered on 08 Sep 2011, 12:44 PM
I can make the wanted rows into checkboxes in normal mode by changing it on ItemDataBound...

protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
        {
            if (e.Item is GridDataItem)
            {
                GridDataItem item = (GridDataItem)e.Item;
                CreateCheckBox(item, "TilladFotoInternt");
                CreateCheckBox(item, "TilladFotoEksternt");
            }
        }
 
        private void CreateCheckBox(GridDataItem item, string columnName)
        {
            var cell = item[columnName];
            if (cell != null)
            {
                bool chk = (item.IsInEditMode ? (cell.Controls[0] as TextBox).Text : cell.Text) == "1";
 
                var chkbx = new CheckBox();
                chkbx.Checked = chk;
                chkbx.Enabled = item.IsInEditMode;
                //cell.Text = "";
                cell.Controls.Add(chkbx);
            }
        }


This works fine while I'm not in editmode, but as soon as I press Editmode, the grid never goes into editmode - if I comment out adding the chkbx, it works fine.

Any clues as to why I can't add/change controls in editmode? Or any ideas on other ways to achieve what I want (converting named int columns into checkbox columns, without having to explicitly declare them in my aspx).
0
Morten
Top achievements
Rank 1
answered on 08 Sep 2011, 03:17 PM
I've ended up solving the problem by doing as above in non-edit mode, and then making a custom text editor:

public class CustomCheckboxColumnEditor : GridTextColumnEditor
{
     private CheckBox checkBox;
 
     protected override void LoadControlsFromContainer()
     {
         this.checkBox = this.ContainerControl.Controls[0] as CheckBox;
     }
     public override bool IsInitialized
     {
        get
        {
           return this.checkBox != null;
        }
     }
     public override string Text
     {
        get
        {
           return this.checkBox.Checked ? "1" : "0";
        }
        set
        {
            this.checkBox.Checked = value == "1";
        }
     }
     protected override void AddControlsToContainer()
     {
        //this.ContainerControl.Controls.Add(new LiteralControl("<strong>Custom multi-line text editor<strong><br>"));
        this.checkBox = new CheckBox();
        this.ContainerControl.Controls.Add(this.checkBox);
     }
 }

Which i then apply by:

protected void RadGrid1_CreateColumnEditor(object sender, GridCreateColumnEditorEventArgs e)
        {
            if (e.Column is GridBoundColumn)
            {
                var column = e.Column as GridBoundColumn;
                if (_checkboxcolumns.Contains(column.DataField))
                {
                    e.ColumnEditor = new CustomCheckboxColumnEditor();
                }
            }
        }
0
Morten
Top achievements
Rank 1
answered on 08 Sep 2011, 03:20 PM
- But if anyone has better suggestions, I'm still interested in hearing them :)
Tags
Grid
Asked by
Morten
Top achievements
Rank 1
Answers by
Morten
Top achievements
Rank 1
Share this question
or