對DevExpress GridControl控制元件,實現通過checkbox選中時整行高亮
看了官方7.0版本demo和tutorial,發現沒有例子講對於gridconrol中的gridview通過checkbox選中時,chebox也是高亮顯示,而不是變白,同其他單元格一樣高亮顯示。
程式碼如下:
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>
/// Summary description for GridFocusedRow.
/// </summary>
public partial class GridFocusedRow : TutorialControl
{
/// <summary>
/// 記錄雙擊位置是否有行被選中
/// </summary>
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;
}
//<radioGroup1>
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();
}
//<gridControl1>
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();
}
//</radioGroup1>
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();
}
}
//</gridControl1>
}
}
有需要專案的,可以加我qq,99024265.