Skip to content

DevExpress-Examples/winforms-grid-use-coloredit-to-edit-unbound-column-string-values

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

WinForms Data Grid - Use a Color Editor to edit colors that are stored as strings

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:

  1. Create an unbound column:
GridColumn unboundColumn = gridView1.Columns.AddField("Color");
unboundColumn.VisibleIndex = gridView1.Columns.Count;
unboundColumn.UnboundType = DevExpress.Data.UnboundColumnType.Object;
  1. Create a RepositoryItemColorEdit cell editor and assign it to the unbound column:
RepositoryItemColorEdit ce = new RepositoryItemColorEdit();
ce.ShowCustomColors = false;
unboundColumn.ColumnEdit = ce;
  1. 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;
}

Files to Review

Documentation