mtwal
Posts: 42
Score: 0
Joined: 8/4/2005
Status: offline
|
Hi, I am struggling with a problem of how to format datagrid cells based on criteria. I have seen an example in .net compact framework 2.0, but can't get it to work in 3.5. Does anyone have any experience with this? I am trying to implement a class called ColumnStyle which inherits from DataGridTextBoxColumn. This is from an example I found. private MainControl theControl; private DBInterface db; private Status theStatus; private DataTable scenResults; public frmStudents(MainControl con) { InitializeComponent(); theControl = con; theStatus = theControl.GetStatus(); db = theControl.GetDBInterface(); BindDG(); } private void BindDG() { CreateTableColumns(); ArrayList theStudents = db.StudentScenarioResults(); AddRows(theStudents); dgStudents.DataSource = scenResults; addGridStyle(ref dgStudents); dgStudents.Refresh(); } private void CreateTableColumns() { scenResults = new DataTable(); DataColumn dcol = new DataColumn("Student", typeof(System.String)); scenResults.Columns.Add(dcol); ArrayList scenarios = db.GetScenarios(); foreach (Scenarios scen in scenarios) { string sName = scen.Name; dcol = new DataColumn(sName, typeof(System.Int32)); scenResults.Columns.Add(dcol); } } private void AddRows(ArrayList results) { //Add Rows to Data Table } private void addGridStyle(ref DataGrid dg) { //Datagrid Table Style is applied to the datagrid and contains //the individual column styles DataGridTableStyle dtStyle = new DataGridTableStyle(); dtStyle.MappingName = "scenResults"; for (int i = 0; i < scenResults.Columns.Count; i++) { ColumnStyle myStyle = new ColumnStyle(i); myStyle.MappingName = scenResults.Columns.ColumnName; if (scenResults.Columns.ColumnName == "Student") myStyle.CheckSecondTest += new CheckCellEventHandler(myStyle_SecondTest); //now add this instance of our ColumnStyle to the DataGridTableStyle dtStyle.GridColumnStyles.Add(myStyle); } //add the datagridtablestyle to the datagrid dg.TableStyles.Add(dtStyle); } public void myStyle_SecondTest(object sender, DataGridEnableEventArgs e) { //do something based on the row & column to set enable flag //in this case see if we can cast the value to a double and if so //check if its below zero string name = (string)dgStudents[e.Row, e.Column]; e.MeetsCriteria = true; } public delegate void CheckCellEventHandler(object sender, DataGridEnableEventArgs e); public class DataGridEnableEventArgs : EventArgs { private int _column; private int _row; private bool _meetsCriteria; public DataGridEnableEventArgs(int row, int col, bool val) { _row = row; _column = col; _meetsCriteria = val; } public int Column { get { return _column; } set { _column = value; } } public int Row { get { return _row; } set { _row = value; } } public bool MeetsCriteria { get { return _meetsCriteria; } set { _meetsCriteria = value; } } } public class ColumnStyle : DataGridTextBoxColumn { public event CheckCellEventHandler CheckSecondTest; public event CheckCellEventHandler CheckCellContains; public event CheckCellEventHandler CheckCellEquals; private int _col; public ColumnStyle(int column) { _col = column; } protected override void Paint(Graphics g, Rectangle Bounds, CurrencyManager Source, int RowNum, Brush BackBrush, Brush ForeBrush, bool AlignToRight) { //can remove this if you don't want to vary the formatting on disabled cells bool enabled = true; //check if our event handler is not null for this cell and then signal the event //the returning result in 'e' will contain a true or false boolean value //the event can be 'receieved' anywhere in our program and the logic can be anything // we choose. The method called from the event simply has to match our 'CheckCellEventHandler' //delegate. if (CheckSecondTest != null) { DataGridEnableEventArgs e = new DataGridEnableEventArgs(RowNum, _col, enabled); CheckSecondTest(this, e); if (e.MeetsCriteria) BackBrush = new SolidBrush(Color.Orange); } //repeat again for our second event handler if (CheckCellContains != null) { DataGridEnableEventArgs e = new DataGridEnableEventArgs(RowNum, _col, enabled); CheckCellContains(this, e); if (e.MeetsCriteria) BackBrush = new SolidBrush(Color.PaleGreen); } base.Paint(g, Bounds, Source, RowNum, BackBrush, ForeBrush, AlignToRight); //out third event handler , we are going to use this draw a rectangle around our //cell so we call it after we have called the base class paint method if (CheckCellEquals != null) { DataGridEnableEventArgs e = new DataGridEnableEventArgs(RowNum, _col, enabled); CheckCellEquals(this, e); if (e.MeetsCriteria) g.DrawRectangle(new Pen(Color.Red, 2), Bounds.X + 1, Bounds.Y + 1, Bounds.Width - 2, Bounds.Height - 2); } } } The overridden OnPaint method of ColumnStyle is never called. Thanks for any help you can provide, Matt
< Message edited by mtwal -- 3/19/2008 10:58:11 PM >
|