Home Forums WinForms controls Xceed Grid for WinForms problem with GridCheckBox

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • User (Old forums)
    Member
    Post count: 23064
    #14187 |

    In my application if the user doesn’t select the check box in the column shouldn’t it return the value of false?

    Here is my code to build the checkbox and column to hold it:
    <code>
    Xceed.Grid.Column colControl = new Xceed.Grid.Column(“CONTROL”, typeof(bool));
    colControl.Width = 64;
    Xceed.Grid.Editors.GridCheckBox chkControl = new Xceed.Grid.Editors.GridCheckBox();

    chkControl.AutoCheck = true;
    chkControl.ThreeState = false;
    colControl.CellEditor = chkControl;
    colControl.CellViewer = new CustomGridCheckBoxViewer();
    gridControl1.Columns.Add(colControl);
    </code>

    The code for CustomGridCheckBoxViewer was in the forum here and the code for it is this:
    <code>
    public class CustomGridCheckBoxViewer : ICellViewer
    {
    private ICellViewer viewer;
    private bool defaultValue;

    public CustomGridCheckBoxViewer()
    {
    viewer = new Xceed.Grid.Editors.GridCheckBox();
    defaultValue = false;
    }

    public bool DefaultValue
    {
    get { return defaultValue; }
    set { defaultValue = value; }
    }

    #region ICellViewer Members
    public int GetFittedHeight(Cell cell, Xceed.Grid.AutoHeightMode mode) { return viewer.GetFittedHeight(cell, mode); }
    public int GetFittedWidth(Cell cell) { return viewer.GetFittedWidth(cell); }
    public bool PaintCellValue(GridPaintEventArgs e, Cell cell)
    {
    if (!(cell.Value is bool)) cell = new CheckBoxCell(cell, defaultValue);
    return viewer.PaintCellValue(e, cell);
    }
    #endregion

    private class CheckBoxCell : Cell
    {
    private bool value;
    public CheckBoxCell(Cell cell, bool value):base(cell)
    {
    this.value = value;
    HorizontalAlignment = cell.HorizontalAlignment;
    VerticalAlignment = cell.VerticalAlignment;
    BackColor = cell.GetBackColorToPaint();
    ForeColor = cell.GetForeColorToPaint();
    }

    protected override Cell CreateInstance() { return new CheckBoxCell(this, value); }
    protected override object GetValue() { return value; }
    protected override void SetValue(object value) { }
    }
    }
    }
    </code>

    I’m looping through the rows in the grid and am attempting to get the value of the checkbox:
    <code>
    foreach (Xceed.Grid.DataRow myRow in gridControl1.DataRows)
    {
    if (myRow.Cells[“CONTROL”].Value != null)
    {
    if (myRow.Cells[“CONTROL”].Value.ToString() == “True”)
    {
    strControl = “T”;
    }
    else
    {
    strControl = “F”;
    }
    }
    }
    </code>

    But instead of getting True/False as the value it’s actually <undefined value>.

    Does anyone know what I’m doing wrong?

    Thank you

    Imported from legacy forums. Posted by Nathan (had 3011 views)

    User (Old forums)
    Member
    Post count: 23064

    The initial value of a cell is usually <b>null</b>, which means <i>empty</i> or <i>has no value</i>. As long as no value is assigned to the cell (or the field in the underlying DataTable/DataView) the value will stay null (or <b>DBNull.Value</b>).

    Imported from legacy forums. Posted by Tommy (had 226 views)

    User (Old forums)
    Member
    Post count: 23064

    Hi Tommy,

    I’m still having an issue with the value of the control in the cell:

    I changed the code to:
    <code>
    if (myRow.Cells[“CONTROL”].Value != DBNull.Value)
    {
    if (myRow.Cells[“CONTROL”].Value.ToString() == “True”)
    {
    strControl = “T”;
    }
    else
    {
    strControl = “F”;
    }
    }
    </code>

    and it passes through to the 2nd if statement but when it tries to retrieve .Value.ToString(), i get object reference not set…and in the watch window

    myRow.Cells[“CONTROL”].Value = <undefined value>

    I’m missing something. If I select the checkbox it is passing True, but if I don’t click it I’m not getting false or null, but rather <undefined value>.

    Do you have any idea why this would be happening?

    Thanks again

    Imported from legacy forums. Posted by Nathan (had 418 views)

    User (Old forums)
    Member
    Post count: 23064

    Nevermind,

    I actually just fixed it using this code:

    <code>
    if ((string)Convert.ToString(myRow.Cells[“CONTROL”].Value) != “”)
    {
    if (myRow.Cells[“CONTROL”].Value.ToString() == “True”)
    {
    strControl = “T”;
    }
    else
    {
    strControl = “F”;
    }
    }
    else
    {
    strControl = “F”;
    }
    </code>

    I had to cast it as a string to do the comparison. Hopefully this might help someone else in the future.

    Thanks Tommy.

    Imported from legacy forums. Posted by Nathan (had 4022 views)

Viewing 4 posts - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.