如何在c#.NET Compact Framework中编辑datagrid row backcolor

时间:2017-08-09 11:43:36

标签: c# datagrid

我有一个.NET Compact Framework 2.0智能设备应用程序。我使用datagrid(而不是datagridView,因为.NET Compact Framework智能设备应用程序不支持)。如何动态更改行的背景颜色?

my code here  

DataTable dt = new DataTable(); 
dt.Clear();
dt.Columns.Add("Name");
dt.Columns.Add("Jobs");
DataRow row= dt.NewRow();
row["Name"] = "aaaa";
row["Jobs"] = "bbbb";
dt.Rows.Add(row);

myDataGrid.DataSource = dt;

1 个答案:

答案 0 :(得分:0)

在CF中改变datagrid行的背景颜色有点复杂。

首先应该有一个ColumnStyle类,它继承自DataGridTextBoxColumn,您将在其中实现DataGridTextBoxColumn的paint事件。

namespace SmartBox
{
    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)//
        {
            _row = row;
            _column = col;
        }

        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; }
        }
    }


    class ColumnStyle : DataGridTextBoxColumn
    {
        public event CheckCellEventHandler CheckCellEven;
        public event CheckCellEventHandler CheckRowContains;

        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)
        {
            bool enabled = true;

            if (CheckCellEven != null)
            {
                DataGridEnableEventArgs e = new DataGridEnableEventArgs(RowNum,_col, enabled);//, _col
                CheckCellEven(this, e);
                if (e.MeetsCriteria)
                    BackBrush = new SolidBrush(Color.PaleGreen);

            }

            if (CheckRowContains != null)
            {
                DataGridEnableEventArgs e = new DataGridEnableEventArgs(RowNum, _col, enabled);
                CheckRowContains(this, e);
                if (e.MeetsCriteria)
                {
                    BackBrush = new SolidBrush(Color.Yellow);
                }
            }

            base.Paint(g, Bounds, Source, RowNum, BackBrush, ForeBrush, AlignToRight);
        }
    }

并在主窗体中,决定用什么条件绘制背景颜色。

private void addGridStyle(ref DataGrid dg, DataTable cem)
    {
        DataGridTableStyle dtStyle = new DataGridTableStyle();
        dtStyle.MappingName = cem.TableName;

        for (int i = 0; i < dt.Columns.Count; i++)
        {
            ColumnStyle myStyle = new ColumnStyle(i);
            myStyle.MappingName = dt.Columns[i].ColumnName;

            if (dt.Columns[i].ColumnName == "urunadi" || dt.Columns[i].ColumnName == "urunkodu" || dt.Columns[i].ColumnName == "toplanan_miktar" || dt.Columns[i].ColumnName == "miktar")
                myStyle.CheckCellEven += new CheckCellEventHandler(myStyle_isEven);

            if (dt.Columns[i].ColumnName == "urunadi" || dt.Columns[i].ColumnName == "urunkodu" || dt.Columns[i].ColumnName == "toplanan_miktar" || dt.Columns[i].ColumnName == "miktar")
                myStyle.CheckRowContains += new CheckCellEventHandler(myStyle_CheckRowContains);

            dtStyle.GridColumnStyles.Add(myStyle);
        }
        dg.TableStyles.Add(dtStyle);
        dg.ColumnHeadersVisible = true;
    }

    public void myStyle_isEven(object sender, DataGridEnableEventArgs e)
    {
        try
        {
            if ((int)toplamaGrid[e.Row, 2] == (int)toplamaGrid[e.Row, 3])
                e.MeetsCriteria = true;
            else
                e.MeetsCriteria = false;
        }
        catch (Exception ex)
        {
            e.MeetsCriteria = false;
        }
    }

    public void myStyle_CheckRowContains(object sender, DataGridEnableEventArgs e)
    {
        try
        {
            if (((int)toplamaGrid[e.Row, 2] > (int)toplamaGrid[e.Row, 3]) && (int)toplamaGrid[e.Row, 3] > 0)
                e.MeetsCriteria = true;
            else
                e.MeetsCriteria = false;
        }
        catch (Exception ex)
        {
            e.MeetsCriteria = false;
        }
    }

要清楚,我发布了关于数据网格行的改变背景颜色的整体。

我有两个方法(myStyle_CheckRowContains,myStyle_isEven)来决定我将使用哪种颜色的行背景。(黄色和绿色)。在我的datagrid中,我有4列从sql到Datatable(dt)。我逐行检查行,如果行的第3列和第4列的值相等,那么我将行背景绘制为黄色。如果第3列的值大于第4列,那么我将背景绘制为黄色。

我希望我帮助过。如果有什么可以问你。这个问题确实占用了我宝贵的一些时间。我改变并实现了我在这里看到的内容:datagrid formatting