This project is read-only.

HowTo switch CellType at Runtime

Mar 2, 2011 at 11:34 AM

Hi there.

First of all this is a great GridTool.

I am very new  to SourceGrid, so maybe I just couldn't find the option...?


Is it possible to switch the CellType from a "normal" Text-Cell to a Checkbox and back?

I want to select a cell and then choose the type (TextCell  or Checkbox) via dropdownbox, radiobox, propertygrid or so.


I have already tried to implement it, the visual representation changes to the new type (so from textcell to checkbox), but i lose the reference from the cell to the Grid.


Is this already implemented?

or if not? What is the best way to achieve this functionality?


Thanks in advance for your help




Mar 4, 2011 at 10:43 AM

Did you try to actually change the CellType at runtime?

Mar 7, 2011 at 12:00 PM
Edited Mar 7, 2011 at 12:13 PM

I use the grid and have a PropertyGrid to change properties of each single cell.

After changing my custom property CellType, I'd like to change the visual representation of the selected cell from Text to Checkbox.


I tried the following:


SourceGrid.Cells.CheckBox checkbox = new SourceGrid.Cells.CheckBox(displayText, true);
Grid[Row.Index,  Column.Index] = checkbox;


As a result the visual representation of the cell changes successfully to a Checkbox, but I lose the reference to the Grid

(so cell.Grid is null)

Because of that I am not able to change other properties (like BackColor) in the future.


Different approach:


SourceGrid.Cells.CheckBox checkbox = new SourceGrid.Cells.CheckBox(displayText, true);
checkbox.BindToGrid(grid, position);

Here the reference to the grid remains, but the Cell still is a Textbox (at least displayed as a textbox).
The text gets centered and if I select the cell again, an error occurs that value "true" is not allowed, etc.

Did i mix something up?

So to sum up:

My goal is to switch by changing a proeprty in my custom Propertygrid from a TextCell to a Checkbox Cell
and keep all the properties (BackColor,...) from the TextCell.

Thanks for your help
Mar 7, 2011 at 9:38 PM

I have a column that contains cells that may or may not be checkboxes depending on other criteria.  If column 2 contains data, the checkbox is shown.  If it does not, an empty textbox is shown. 

The checkboxView is: checkboxView = SourceGrid.Cells.Views.CheckBox();

The default view is: defaultView = Cell(); 

Then I can programatically switch between a checkbox and a textbox.

grid[row, RAD].View = checkboxView;   // Sets the cell to display a chechbox

grid[row, RAD].View = defaultView;  // Set cell to textbox

You can pick up the colors from the cell before you switch and then apply them if necessary.  In my case, I color the row based on the content of column 2 and some other criteria.

Maybe this will give you an idea on how to proceed.

Mar 8, 2011 at 8:22 AM
Edited Mar 8, 2011 at 8:23 AM

Thanks for your answer.


Does this approach work for you when you switch from a Textcell to a CheckboxCell ?


I tried to use this, but i receive an error (Null Pointer) in PrepareVisualElementCheckBox(CellContext context) (CheckBox.cs), because Models.ICheckBox checkBoxModel = (Models.ICheckBox)context.Cell.Model.FindModel(typeof(Models.ICheckBox));  returns NULL.

The Cell doesn't get switched to a Checkbox, the Type of the cell remains Textbox.


Did I miss something?



My Code now:


switch (value)
case CheckBox:

SourceGrid.Cells.Views.CheckBox checkboxView = new SourceGrid.Cells.Views.CheckBox(); 
//checkboxView.ElementCheckBox.Style = DevAge.Drawing.ControlDrawStyle.Normal; 
//checkboxView.ElementCheckBox.CheckBoxState = DevAge.Drawing.CheckBoxState.Checked; 
grid[row, column].View = checkboxView; 

case TextBox: SourceGrid.Cells.Views.CheckBox cellView = new SourceGrid.Cells.Views.Cell(); grid[row, column].View = cellView ; break; }



Mar 11, 2011 at 10:53 AM

Sry to push the thread.


Any Idea?

May 4, 2011 at 2:40 PM

I'll push the thread again, hoping someone here has an idea to help me ;)