Example E3234
Visible to All Users

WinForms Data Grid - Update the row style immediately after changing a value in the cell editor

The example handles the EditValueChanged event to respond to changing an editor's value. The example calls the PostEditor method to save the modified value to a data source. Once the value is posted to a data source, the Data Grid control invalidates/repaints the cell. The RowCellStyle event is handled to customize row appearance settings based on a specific condition:

C#
void gridView1_RowCellStyle(object sender, RowCellStyleEventArgs e) { if (!true.Equals(gridView1.GetRowCellValue(e.RowHandle, "UseStyles"))) return; e.Appearance.BackColor = GetRowBackColor(e.RowHandle); }

Files to Review

Does this example address your development requirements/objectives?

(you will be redirected to DevExpress.com to submit your response)

Example Code

Form1.cs(vb)
C#
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using DevExpress.XtraEditors.Repository; using DevExpress.XtraGrid.Views.Grid; using DevExpress.Utils; namespace WindowsApplication1 { public partial class Form1 : Form { Random r = new Random(); public int GetRandomValue() { return r.Next(100) + 50; } private DataTable CreateTable(int RowCount) { DataTable tbl = new DataTable(); tbl.Columns.Add("Name", typeof(string)); tbl.Columns.Add("UseStyles", typeof(bool)); tbl.Columns.Add("R", typeof(int)); tbl.Columns.Add("G", typeof(int)); tbl.Columns.Add("B", typeof(int)); for (int i = 0; i < RowCount; i++) tbl.Rows.Add(new object[] { String.Format("Name{0}", i), false, GetRandomValue(), GetRandomValue(), GetRandomValue() }); return tbl; } public Form1() { AppearanceObject.DefaultFont = new Font(AppearanceObject.DefaultFont.FontFamily, 14, FontStyle.Bold); InitializeComponent(); gridControl1.DataSource = CreateTable(5); RepositoryItemCheckEdit riCheckEdit = new RepositoryItemCheckEdit(); gridView1.Columns["UseStyles"].ColumnEdit = riCheckEdit; RepositoryItemTrackBar riTrackBar = new RepositoryItemTrackBar(); riTrackBar.Maximum = 255; gridView1.Columns["R"].ColumnEdit = riTrackBar; gridView1.Columns["G"].ColumnEdit = riTrackBar; gridView1.Columns["B"].ColumnEdit = riTrackBar; riCheckEdit.EditValueChanged += OnEditValueChanged; riTrackBar.EditValueChanged += OnEditValueChanged; gridView1.RowCellStyle += new RowCellStyleEventHandler(gridView1_RowCellStyle); } void gridView1_RowCellStyle(object sender, RowCellStyleEventArgs e) { if (!true.Equals(gridView1.GetRowCellValue(e.RowHandle, "UseStyles"))) return; e.Appearance.BackColor = GetRowBackColor(e.RowHandle); } void OnEditValueChanged(object sender, EventArgs e) { gridView1.PostEditor(); } private Color GetRowBackColor(int rowHandle) { return Color.FromArgb(GetColorValue(rowHandle, "R"), GetColorValue(rowHandle, "G"), GetColorValue(rowHandle, "B")); } private int GetColorValue(int rowHandle, string fieldName) { return (int)gridView1.GetRowCellValue(rowHandle, fieldName); } } }

Disclaimer: The information provided on DevExpress.com and affiliated web properties (including the DevExpress Support Center) is provided "as is" without warranty of any kind. Developer Express Inc disclaims all warranties, either express or implied, including the warranties of merchantability and fitness for a particular purpose. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.

Confidential Information: Developer Express Inc does not wish to receive, will not act to procure, nor will it solicit, confidential or proprietary materials and information from you through the DevExpress Support Center or its web properties. Any and all materials or information divulged during chats, email communications, online discussions, Support Center tickets, or made available to Developer Express Inc in any manner will be deemed NOT to be confidential by Developer Express Inc. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.