在DevExpress XtraGrid中将0/1显示为是/否吗?

时间:2019-11-10 23:13:42

标签: c# winforms sqlite devexpress xtragrid

我为常规的DataGridView编写了此代码,但在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";
    }
}

问题是我使用的是SQLite,它没有任何真正的布尔数据类型,否则这很容易。该列在数据库中设置为仅接受0和1,这是一个临时布尔值。

2 个答案:

答案 0 :(得分:0)

借助DevExpress,您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;

然后,您可以设置ValueCheckedValueUnchecked和ValueGrayed属性来指定分别对应于编辑器的选中,未选中和不确定状态的值。 ValueChecked,ValueUnchecked和ValueGrayed属性的默认值分别为true, false and null。您可以根据逻辑要求为这些属性分配自定义值:

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

使用这种方法,可以避免在GridView中显示和编辑值时进行任何值转换操作。

答案 1 :(得分:0)

另一种可能性是在gridView上使用CustomColumnDisplayText事件

grid_table = [
{"minLng":"117.5783","minLat":"-33.6764","maxLng": "117.5787","maxLat":"-33.6766","gridName" :"5c"},
{"minLng":"117.5783","minLat":"-33.6766","maxLng":"117.5785","maxLat":"-33.6768","gridName":"4c"},
{"minLng":"117.5783","minLat":"-33.6768","maxLng":"117.5785","maxLat":"-33.6770","gridName":"3c"}
]

function getGridValue({ latitude, longitude }) {
    const data = [...grid_table];

    const item = data.find(
        item =>
            (latitude >= item.minLat  && longitude >= item.minLng) &&
            (latitude <= item.maxLat && longitude <= item.maxLng)
    );

    if (item) {
        return item.gridName;
    }

    return null;
}
const [latitude, longitude] = ["-33.6769","117.5784"];
const value = getGridValue({ latitude, longitude});

document.write(value);