Category / Section
How to get the RGB value when a color is selected in the ColorEdit cell in WinForms GridControl?
1 min read
GridControl with ColorEdit cell
You can obtain the selected color from Renderer.ControlValue of the current cell in CurrentCellCloseDropDown event handler.
C#
private void Form1_Load(object sender, EventArgs e) { this.gridControl1.ColWidths[3] = 100; //Set cell type as ColorEdit cell this.gridControl1[3, 3].CellType = "ColorEdit"; this.gridControl1[3, 3].CellValueType = typeof(Color); this.gridControl1[3, 3].CellValue = "Blue"; this.gridControl1.CurrentCellCloseDropDown += gridControl1_CurrentCellCloseDropDown; } void gridControl1_CurrentCellCloseDropDown(object sender, PopupClosedEventArgs e) { //Get Grid's CurrentCell GridCurrentCell cc = this.gridControl1.CurrentCell; if (cc.Renderer is GridDropDownColorUICellRenderer) { //Convert CurrentCell control value to Color Color c = (Color)cc.Renderer.ControlValue; Console.WriteLine(c); Console.WriteLine("R-Val:"+c.R); Console.WriteLine("G-Val:"+c.G); Console.WriteLine("B-Val:"+c.B); } }
VB
Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load Me.gridControl1.ColWidths(3) = 100 'Set cell type as ColorEdit cell Me.gridControl1(3, 3).CellType = "ColorEdit" Me.gridControl1(3, 3).CellValueType = GetType(Color) Me.gridControl1(3, 3).CellValue = "Blue" AddHandler Me.gridControl1.CurrentCellCloseDropDown, AddressOf gridControl1_CurrentCellCloseDropDown End Sub Private Sub gridControl1_CurrentCellCloseDropDown(ByVal sender As Object, ByVal e As PopupClosedEventArgs) 'Get Grid's CurrentCell Dim cc As GridCurrentCell = Me.gridControl1.CurrentCell If TypeOf cc.Renderer Is GridDropDownColorUICellRenderer Then 'Convert CurrentCell control value to Color Dim c As Color = CType(cc.Renderer.ControlValue, Color) Console.WriteLine(c) Console.WriteLine("R-Val:" & c.R) Console.WriteLine("G-Val:" & c.G) Console.WriteLine("B-Val:" & c.B) End If End Sub
The following screenshot displays the Grid control with ColorEdit cell.
Figure 1: Grid Control with ColorEdit Cell
The following is the output screenshot.
Figure 2: Output screenshot
Samples:
C#: GetRGBValue
VB:
GetRGBValue