The WinForms ColorEdit is designed to work with values of the Color
type. In this example, the data source stores colors as strings in the "ColorString" field. Follow the steps below to use the Color Editor to display and edit "ColorString" field values:
- Create an unbound column:
GridColumn unboundColumn = gridView1.Columns.AddField("Color");
unboundColumn.VisibleIndex = gridView1.Columns.Count;
unboundColumn.UnboundType = DevExpress.Data.UnboundColumnType.Object;
- Create a RepositoryItemColorEdit cell editor and assign it to the unbound column:
RepositoryItemColorEdit ce = new RepositoryItemColorEdit();
ce.ShowCustomColors = false;
unboundColumn.ColumnEdit = ce;
- Handle the GridView's CustomUnboundColumnData event to supply data for the unbound column (to convert string values to
Color
):
private void gridView1_CustomUnboundColumnData(object sender, DevExpress.XtraGrid.Views.Base.CustomColumnDataEventArgs e) {
GridView view = sender as GridView;
DataView dv = view.DataSource as DataView;
if (e.IsGetData)
e.Value = GetColorFromString(dv[e.ListSourceRowIndex]["ColorString"].ToString());
else
dv[e.ListSourceRowIndex]["ColorString"] = ((Color)e.Value).Name;
}
Color GetColorFromString(string colorString) {
Color color = Color.Empty;
ColorConverter converter = new ColorConverter();
try {
color = (Color)converter.ConvertFromString(colorString);
}
catch { }
return color;
}
(you will be redirected to DevExpress.com to submit your response)