我一直在尝试更改 Compact Framework DataGrid 中一行的背景颜色,但收效甚微,因为 .NET CF 上的 DataGrid 与其 Windows 窗体对应物相比受到限制。我实现目标的唯一一点成功是我现在能够根据其值更改单个单元格的背景颜色。我无法操纵从谷歌搜索得到的代码,因为我在 C# 方面不是那么好。但是,这是我拥有的代码:
namespace GridColor
{
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, 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; }
}
}
public partial class ColumnStyle : DataGridTextBoxColumn
{
//public event CheckCellEventHandler CheckCellEquals;
public event CheckCellEventHandler CheckCellContains;
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 (CheckCellContains != null)
{
DataGridEnableEventArgs e = new DataGridEnableEventArgs(RowNum, _col, enabled);
CheckCellContains(this, e);
if (e.MeetsCriteria)
//g.DrawRectangle(new Pen(Color.Red, 2), Bounds.Y + 1, Bounds.Width - 2, Bounds.Height - 2);
BackBrush = new SolidBrush(Color.PaleGreen);
}
base.Paint(g, Bounds, Source, RowNum, BackBrush, ForeBrush, AlignToRight);
}
}
}
现在对于我的表格,我有这个:
namespace GridColor
{
public partial class Form1 : Form
{
DataSet ds;
SqlDataAdapter da;
private List<string> compareValues = new List<string>();
public Form1()
{
InitializeComponent();
try
{
addGridStyle(ref dataGrid1);
compareValues.Add("OK");
compareValues.Add("Filling");
}
catch (Exception ex)
{
MessageBox.Show("Error: " + ex.ToString());
}
}
private void addGridStyle(ref DataGrid dg)
{
DataGridTableStyle dtStyle = new DataGridTableStyle();
dtStyle.MappingName = "Test";
string connString = "Data Source=192.168.2.16,1433;Initial Catalog=TestDB;User ID=sa;Password=ABC12abc;";
SqlConnection conn = new SqlConnection(connString);
conn.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandType = CommandType.Text;
cmd.CommandText = "SELECT * FROM Test";
ds = new DataSet();
da = new SqlDataAdapter(cmd);
da.Fill(ds, "Test");
for (int i = 0; i < ds.Tables["Test"].Columns.Count; i++)
{
ColumnStyle myStyle = new ColumnStyle(i);
myStyle.MappingName = ds.Tables["Test"].Columns[i].ToString();
if (i == 1)
{
if (ds.Tables["Test"].Columns[i].DataType == System.Type.GetType("System.String"))
myStyle.CheckCellContains += new CheckCellEventHandler(myStyle_CheckCellContains);
}
dtStyle.GridColumnStyles.Add(myStyle);
}
dg.TableStyles.Add(dtStyle);
}
public void myStyle_CheckCellContains(object sender, DataGridEnableEventArgs e)
{
try
{
if (compareValues.Contains((string)dataGrid1[e.Row, e.Column]))
e.MeetsCriteria = true;
else
e.MeetsCriteria = false;
}
catch (Exception ex)
{
e.MeetsCriteria = false;
}
}
private void Form1_Load(object sender, EventArgs e)
{
dataGrid1.DataSource = ds.Tables["Test"];
}
}
}
我应该在代码的哪一部分进行更改,以便如果一个单元格符合条件,它的整行将被着色,而不仅仅是它自己的单元格?