DateGrid行变色—winform—pda

 

Q:如何根据数据值列值修改dateGrid背景色?

1.重写DataGridTextBoxColumn,如下代码:

public class DataGridColoredTextBoxColumn : DataGridTextBoxColumn
    {        
        private System.Data.DataRow[] m_coloredDataRows;
        private System.Drawing.Brush m_foreBrush;
        private System.Drawing.Brush m_backBrush;
        private DataView _currentDataView;

        /// <summary>
        /// 设置或获取需要更改的数据集
        /// </summary>
        public DataView currentView
        {
            get
            {
                return _currentDataView;
            }
            set
            {
                _currentDataView = value;
            }
        }

        //设置行色彩
        public void SetRowsColor(DataRow[] aDataRows, System.Drawing.Brush aForeBrush, System.Drawing.Brush aBackBrush)
        {
            //设置要改变颜色的行
            m_coloredDataRows = aDataRows;
            //设置前景色
            m_foreBrush = aForeBrush;
            //设置背景色
            m_backBrush = aBackBrush;
        }

        //重载绘制单元格的函数,如果该单元格所在行符合ColoredView中的条件,就改变前景和背景色
        protected override void Paint(System.Drawing.Graphics g,
         System.Drawing.Rectangle bounds, System.Windows.Forms.CurrencyManager
         source, int rowNum, System.Drawing.Brush backBrush, System.Drawing.Brush
         foreBrush, bool alignToRight)
        {           
            
            //搜索当前行是否在设置行色彩的ColoredView当中
            DataRow currentRow = currentView[rowNum].Row;
            foreach (DataRow coloredRow in m_coloredDataRows)
            {
                if (currentRow == coloredRow)
                {
                    backBrush = m_backBrush;
                    foreBrush = m_foreBrush;
                    break;
                }
            }

            //调用基类的绘制函数
            base.Paint(g, bounds, source, rowNum, backBrush, foreBrush, alignToRight);
        }

    } 

 

2.调用重写类,set设置背景色,代码如下:

       

       /// <summary>
       /// 行变色
        /// </summary>
        /// <param name="temp"></param>
        void DataBackColor(DataTable temp)
        {
            if (temp == null || temp.Rows.Count == 0)
            {
                return;
            }

            DataGridTableStyle dgTabSty = new DataGridTableStyle();
            dgTabSty.MappingName = temp.TableName;

            foreach (DataGridColumnStyle col in this.dgWMInventory.TableStyles[0].GridColumnStyles)
            {
                DataGridColoredTextBoxColumn obj = new DataGridColoredTextBoxColumn();
                obj.currentView = temp.DefaultView;
                obj.MappingName = col.MappingName;
                obj.HeaderText = col.HeaderText;
                obj.NullText = col.NullText;
                obj.Width = col.Width;
                //if (col.MappingName = "KFlag")
                    obj.SetRowsColor(temp.Select(" KFlag='K' "), new SolidBrush(Color.White), new SolidBrush(Color.OrangeRed));
                dgTabSty.GridColumnStyles.Add(obj);
            }

            this.dgWMInventory.TableStyles.Clear();
            this.dgWMInventory.BackgroundColor = System.Drawing.Color.FromArgb(((int)(((byte)(128)))), ((int)(((byte)(128)))), ((int)(((byte)(128)))));
            //this.dgWMInventory.Location = new System.Drawing.Point(0, 40);
            this.dgWMInventory.SelectionForeColor = System.Drawing.Color.White;
            this.dgWMInventory.SelectionBackColor = System.Drawing.Color.Green;
            this.dgWMInventory.TableStyles.Add(dgTabSty);

            this.dgWMInventory.DataSource = temp;
        }


 

你可能感兴趣的:(DateGrid行变色—winform—pda)