The most common operations with cells include painting, editing, determining location, setting appearance, custom painting and some others. Please see the list of most common cell operations below.
C# | Copy |
---|---|
public void MostCommonOperationsWithCell(Grid grid) { grid.Headers.Add(new Header()); grid.Headers[0].Add(new Column("Price")); grid.Headers[0].Add(new Column("Quantity")); grid.Rows.Add(new Product()); Row row = grid.Rows[0]; //Set a new price programmatically Cell cell = row["Price"]; cell.Value = 52.32; //Edit the cell with an appropriate editor grid.Headers[0]["Price"].Editable = true; cell.Edit(); //Highlight the cell with the red color for 2 seconds cell.Highlight(new TimeSpan(0, 0, 2), Color.Red); //Ensure the cell visible cell.EnsureVisible(); //Force the grid to resort the row and verify if the row should be filtered. I.e. if the IFilter.IsFiltered(row) returns true. //Normally this method is called automatically, if the data object implements INotifyPropertyChanged interface. cell.Update(); //Just invalidate the cell i.e. ask the grid to repaint the cell without cheching for the position //in the sorted order and checking if the row is filtered. cell.Invalidate(); //Set a back color for the cell cell.Appearance.BackColor = Color.Green; //Set some image Bitmap image = new Bitmap("Image.ico"); cell.Image = image; //Demonstrates, how to custom draw in cell. //The delegate is called while the cell's painting grid.PaintCell +=delegate(object sender, PaintCellEventArgs e) { using(Brush brush = new SolidBrush(Color.DeepPink)) { //Prevent from text painting e.Parts &= e.Parts ^ PaintPart.Text; //Set a new background color e.Appearance.BackColor = Color.Yellow; //Do default painting without the text drawing e.PaintAll(); e.Handled = true; //Draw a custom text above already pained cell e.Graphics.DrawString("Custom string", SystemFonts.StatusFont, brush, e.Cell.VirtualBounds); } }; } |
Back to .Net Grid HowTo topics