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

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

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

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

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

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



        {

        //...

        //使用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ù)條件, 將相關(guān)行設置不同的背景色.
        //下例為國家(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)下載排行

        主站蜘蛛池模板: 久久久青草青青亚洲国产免观 | 中文字幕免费观看| 亚洲六月丁香六月婷婷蜜芽| 18禁超污无遮挡无码免费网站国产| 亚洲aⅴ无码专区在线观看| 亚洲综合久久夜AV | 免费观看无遮挡www的视频| 亚洲av无码专区在线电影| 亚洲高清国产AV拍精品青青草原| 希望影院高清免费观看视频| 无人视频免费观看免费视频| 亚洲精品美女视频| 亚洲国产精品成人网址天堂| 99久久免费观看| 日韩精品无码永久免费网站| 亚洲三级视频在线观看| 亚洲综合色在线观看亚洲| 97国产免费全部免费观看| 福利免费在线观看| 亚洲GV天堂GV无码男同| 久久综合亚洲色一区二区三区 | 中文字幕一精品亚洲无线一区| 福利免费观看午夜体检区| 成在线人视频免费视频| 亚洲精品无码久久久久秋霞| 亚洲成熟xxxxx电影| 2048亚洲精品国产| 国产精品无码素人福利免费| 日韩精品无码免费一区二区三区 | 黄色a三级免费看| 亚洲制服在线观看| 亚洲av无码精品网站| 亚洲国产精品狼友中文久久久| 中文字幕无码视频手机免费看| 国产拍拍拍无码视频免费| 色屁屁www影院免费观看视频| 激情亚洲一区国产精品| 亚洲天堂中文字幕| 亚洲AV无码一区二区乱孑伦AS| 亚洲 无码 在线 专区| 日韩免费无码一区二区视频|