Show 0/1 as yes/no in DevExpress XtraGrid?

852 Views Asked by At

I had this code for a regular DataGridView, and I'm having trouble getting the same effect on the XtraGrid:

private void RosterGridView_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
    if (e.ColumnIndex == 7 && e.Value is long)
    {
        e.Value = (long)e.Value == 0 ? "No" : "Yes";
    }
}

The issue is I am using SQLite and it doesn't have any true boolean data type, or else this would be easy. The column is set up in the DB to only accept 0 and 1, kind of a makeshift boolean.

2

There are 2 best solutions below

0
DmitryG On

With DevExpress you can set up the specific column to use the RepositoryItemCheckEdit:

// Assign a repository item to a column            
RepositoryItemCheckEdit checkEdit = new RepositoryItemCheckEdit();
gridControl.RepositoryItems.Add(checkEdit);
gridView.Columns["Mark"].ColumnEdit = checkEdit;

After that, you can set up the ValueChecked, ValueUnchecked and ValueGrayed properties specify the values that correspond to the editor's checked, unchecked and indeterminate state, respectively. The default values of the ValueChecked, ValueUnchecked and ValueGrayed properties are true, false and null, respectively. You can assign custom values to these properties as your logic requires:

checkEdit.ValueChecked = (long)1;
checkEdit.ValueUnchecked = (long)0;

With this approach, you can avoid any value conversion operations when displaying and editing values in GridView.

0
erelguitars On

Another possibility is to use the CustomColumnDisplayText event on the gridView

private void gridView1_CustomColumnDisplayText(object sender, DevExpress.XtraGrid.Views.Base.CustomColumnDisplayTextEventArgs e)
{
    if (e.Column = <yourColumn>)
    {
        e.DisplayText = (long) e.Value == 1 ? "Yes" : "No";
    }
}