• <label id="pxtpz"><meter id="pxtpz"></meter></label>
      1. <span id="pxtpz"><optgroup id="pxtpz"></optgroup></span>

        當前位置:雨林木風下載站 > 技術開發(fā)教程 > 詳細頁面

        將DataGrid中滿足條件的行設為不同的背景色(WinForm).

        將DataGrid中滿足條件的行設為不同的背景色(WinForm).

        更新時間:2022-05-16 文章作者:未知 信息來源:網絡 閱讀次數(shù):

        由于項目需要, 需要對DataGrid的數(shù)據(jù)行, 按不同的條件以不同的背景色相區(qū)別。 由于DataGrid中沒有相關的屬性和方法可以直接設置,要完成這個功能還挺費些功夫。在網上搜了半天,也沒找到解決方案。只好自己動手,豐衣足食了,:) 。研究了半天, 終于搞定它了。好東西不敢獨享,特貼出來,希望能給需要的人帶來些幫助。



        {

        //...

        //使用DataGridTableStyle 顯示DataGrid.

        DataGridTableStyle tableStyle = new DataGridTableStyle();
        tableStyle.MappingName = "customers";

        int numCols = _dataSet.Tables["customers"].Columns.Count;
        DataGridCellColorTextBoxColumn columnTextColumn ;
        for(int i = 0; i < numCols; ++i)
        {
        columnTextColumn = new DataGridCellColorTextBoxColumn();
        columnTextColumn.HeaderText = _dataSet.Tables["customers"].Columns[i].ColumnName;
        columnTextColumn.MappingName = _dataSet.Tables["customers"].Columns[i].ColumnName;

        //為每個單元格建立設置背景色的事件.
        columnTextColumn.CheckCellColor += new CellColorEventHandler(SetColorValues);

        tableStyle.GridColumnStyles.Add(columnTextColumn);
        }

        dataGrid1.TableStyles.Clear();
        dataGrid1.TableStyles.Add(tableStyle);

        dataGrid1.DataSource = _dataSet.Tables["customers"];

        }



        public void SetColorValues(object sender, DataGridCellColorEventArgs e)
        {
        //根據(jù)條件, 將相關行設置不同的背景色.
        //下例為國家(datagrid中第9列)為Mexico的行設置為紅色,USA的行設為黃色.
        if(Convert.ToString(dataGrid1[e.Row,8]) == "Mexico")
        e.BackColor = Color.Red;
        else if(Convert.ToString(dataGrid1[e.Row,8]) == "USA")
        e.BackColor = Color.Yellow;
        }


        public class DataGridCellColorEventArgs : EventArgs
        {
        private int _row;
        private Color _backcolor;

        public DataGridCellColorEventArgs(int row, Color val)
        {
        _row = row;
        _backcolor = val;
        }
        public int Row
        {
        get{ return _row;}
        set{ _row = value;}
        }
        public Color BackColor
        {
        get{ return _backcolor;}
        set{ _backcolor = value;}
        }
        }



        //為事件建立委托.
        public delegate void CellColorEventHandler(object sender, DataGridCellColorEventArgs e);

        public class DataGridCellColorTextBoxColumn : DataGridTextBoxColumn
        {
        public event CellColorEventHandler CheckCellColor;

        public DataGridCellColorTextBoxColumn()
        {
        }

        //繼承DataGridTextBoxColumn的Pain事件.
        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)
        {
        if(CheckCellColor != null)
        {
        //重繪畫時,設置當前行的背景色
        DataGridCellColorEventArgs e = new DataGridCellColorEventArgs(rowNum, Color.White);
        CheckCellColor(this, e);

        if(e.BackColor != Color.White)
        backBrush = new SolidBrush(e.BackColor);
        }

        base.Paint(g, bounds, source, rowNum, backBrush, foreBrush, alignToRight);
        }

        protected override void Edit(System.Windows.Forms.CurrencyManager source, int rowNum, System.Drawing.Rectangle bounds, bool readOnly, string instantText, bool cellIsVisible)
        {
        base.Edit(source, rowNum, bounds, readOnly, instantText, cellIsVisible);
        }
        }

        溫馨提示:喜歡本站的話,請收藏一下本站!

        本類教程下載

        系統(tǒng)下載排行

        主站蜘蛛池模板: 日本视频一区在线观看免费| 色婷婷亚洲十月十月色天| 亚洲精品无码日韩国产不卡av| 91精品啪在线观看国产线免费| 亚洲av伊人久久综合密臀性色 | 亚洲av永久无码精品秋霞电影影院| 日本永久免费a∨在线视频 | 韩国免费三片在线视频| 亚洲第一男人天堂| 免费看大黄高清网站视频在线| 风间由美在线亚洲一区| 免费人成视频x8x8入口| 五月天婷婷免费视频| 亚洲日韩中文无码久久| 无码国产精品一区二区免费16| 久久丫精品国产亚洲av不卡| 日本成年免费网站| 蜜臀亚洲AV无码精品国产午夜.| 亚洲精品国产自在久久| a级毛片免费播放| 久久久久亚洲AV片无码下载蜜桃| 思思re热免费精品视频66| 亚洲人成网站免费播放| 亚洲精品国产高清嫩草影院| 久久国产精品免费网站| 亚洲人成影院77777| 免费在线不卡视频| 免费在线观影网站| 亚洲免费视频观看| 亚洲AV网站在线观看| 欧洲人免费视频网站在线| 亚洲综合色区中文字幕| 亚洲国产a级视频| 9277手机在线视频观看免费| 男人的天堂av亚洲一区2区| 亚洲一区无码中文字幕| 久久久www成人免费毛片| 一区二区三区AV高清免费波多| 亚洲天天在线日亚洲洲精| 全免费a级毛片免费看不卡| 中国一级特黄高清免费的大片中国一级黄色片 |