对DevExpress GridControl控件,实现通过checkbox选中时整行高亮

看了官方7.0版本demo和tutorial,发现没有例子讲对于gridconrol中的gridview通过checkbox选中时,chebox也是高亮显示,而不是变白,同其他单元格一样高亮显示。

对DevExpress GridControl控件,实现通过checkbox选中时整行高亮_第1张图片

代码如下:

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using DevExpress.Utils;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraGrid.Views.Grid.ViewInfo;
using DevExpress.XtraGrid.Demos.Tutorials;


namespace GridTutorials
{
    ///


    /// Summary description for GridFocusedRow.
    ///

    public partial class GridFocusedRow : TutorialControl
    {
        ///
        /// 记录双击位置是否有行被选中
        ///

        private GridHitInfo hitInfo;
        public GridFocusedRow() {
            //
            // Required for Windows Form Designer support
            //
            InitializeComponent();

            //
            // TODO: Add any constructor code after InitializeComponent call
            //
        }

        private void GridFocusedRow_Load(object sender, System.EventArgs e) {
            InitNWindData();
            InitGrid();
        }

        string tblName = "Products";
        protected override void InitMDBData(string connectionString) {
            DataSet ds = new DataSet();
            System.Data.OleDb.OleDbDataAdapter oleDbDataAdapter = new System.Data.OleDb.OleDbDataAdapter("SELECT * FROM " + tblName, connectionString);
            oleDbDataAdapter.Fill(ds, tblName);
            gridControl1.DataSource = ds.Tables[tblName];
        }
        protected override void InitXMLData(string dataFileName) {
            DataSet ds = new DataSet();
            ds.ReadXml(dataFileName);
            gridControl1.ShowOnlyPredefinedDetails = true;
            gridControl1.DataSource = ds.Tables[tblName];
        }

        private void InitGrid() {
            gridView1.FocusedColumn = gridView1.Columns["UnitPrice"];
            gridView1.FocusedRowHandle = 1;
            radioGroup1.SelectedIndex = 2;
        }

      //
        private void radioGroup1_SelectedIndexChanged(object sender, System.EventArgs e) {
            int style = (int)radioGroup1.EditValue;
            gridView1.FocusRectStyle = DrawFocusRectStyle.CellFocus;
            gridView1.OptionsSelection.EnableAppearanceFocusedCell = true;
            gridView1.OptionsSelection.EnableAppearanceFocusedRow = true;
            gridView1.OptionsSelection.InvertSelection = false;
            switch(style) {
                case 1: //Row
                    gridView1.OptionsSelection.EnableAppearanceFocusedCell = false;
                    gridView1.FocusRectStyle = DrawFocusRectStyle.RowFocus;
                    break;
                case 2: //Cell
                    gridView1.OptionsSelection.InvertSelection = true;
                    break;
                case 4: //Cell Rect Only
                    gridView1.OptionsSelection.EnableAppearanceFocusedCell = false;
                    gridView1.OptionsSelection.EnableAppearanceFocusedRow = false;
                    break;
                case 5: //None
                    gridView1.FocusRectStyle = DrawFocusRectStyle.None;
                    gridView1.OptionsSelection.EnableAppearanceFocusedCell = false;
                    gridView1.OptionsSelection.EnableAppearanceFocusedRow = false;
                    break;
                
            }
            SetFocusedColumnAppearance(null, gridView1.FocusedColumn);
            gridControl1.Focus();
        }

      //
        private void SetFocusedColumnAppearance(DevExpress.XtraGrid.Columns.GridColumn col1, DevExpress.XtraGrid.Columns.GridColumn col2) {
            if (radioGroup1.EditValue == null) return;
            if (col1 != null) col1.AppearanceCell.Reset();
            if (col2 != null)
                if (3.Equals(radioGroup1.EditValue))
                {
                    col2.AppearanceCell.BackColor = SystemColors.Highlight;
                    col2.AppearanceCell.ForeColor = SystemColors.HighlightText;
                }
                else
                    col2.AppearanceCell.Reset();
        }
      //


        private void gridView1_FocusedColumnChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedColumnChangedEventArgs e) {
            SetFocusedColumnAppearance(e.PrevFocusedColumn, e.FocusedColumn);
        }

        private void gridControl1_MouseDown(object sender, MouseEventArgs e)
        {
            hitInfo = this.gridView1.CalcHitInfo(new Point(e.X, e.Y));
            if (hitInfo.Column == null || hitInfo.RowHandle == -1)
            {
                return;
            }
            if (hitInfo.Column.FieldName!="Discontinued")
            {
                return;
            }
            this.gridView1.FocusedRowHandle = hitInfo.RowHandle;
            SetCheckBoxValue();
        }


        private void SetCheckBoxValue()
        {
            object obj = this.gridView1.GetRowCellValue(this.gridView1.FocusedRowHandle, "Discontinued");
            if (obj==null)
            {
                return;
            }
            if (obj.ToString()=="True")
            {
                this.gridView1.SetRowCellValue(this.gridView1.FocusedRowHandle, "Discontinued", "False");
            }
            if (obj.ToString()=="False")
            {
                this.gridView1.SetRowCellValue(this.gridView1.FocusedRowHandle, "Discontinued", "True");
            }
        }
        protected  void InitNWindData()
        {
            string DBFileName = string.Empty;
            if (Is64BitOS)
            {
                DBFileName = DevExpress.Utils.FilesHelper.FindingFileName(Application.StartupPath, "Data\\nwind.xml");
                if (DBFileName != string.Empty)
                {
                    InitXMLData(DBFileName);
                }
            }
            else
            {
                DBFileName = DevExpress.Utils.FilesHelper.FindingFileName(Application.StartupPath, "Data\\nwind.mdb");
                if (DBFileName != string.Empty)
                {
                    InitMDBData("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + DBFileName);
                }
            }
        }
        public static bool Is64BitOS
        {
            get
            {
                return false;
                //return DevExpress.Utils.OSVersionHelper.Is64BitOS();
            }
        }
      //
    }
}

有需要项目的,可以加我qq,99024265.

你可能感兴趣的:(devexpress,gridview,c#,NET,gridcontrol,小技巧,代码)