1

Compact Framework DataGrid の行の背景色を変更しようとしてきましたが、.NET CF の DataGrid は Windows Forms の対応するものに比べて制限があるため、ほとんど成功していません。私の目標を達成するための唯一の成功は、値に応じて単一のセルの背景色を変更できるようになったことです。私は C# が苦手なので、Google で取得したコードを操作できませんでした。ただし、これは私が持っているコードです:

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"];
            }
        }
    }

コードのどの部分を変更すれば、セルが条件を満たしている場合に、それ自体のセルだけでなく行全体が色付けされるようになりますか?

4

2 に答える 2

1

さて、私は戻って、より高度なDataGridViewが出る前など、デスクトップでこれを行った数年前のコードを見つけました。

まず最初に、MicrosoftがWindowsフォームDataGridをカスタマイズするこのチュートリアルがあります。これは、行全体を強調表示する方法を説明しています。

コードを確認し、列ごとにカスタムの列スタイルを追加し、処理したメインフォームにイベントを発生させてから、そのレコードの適切な色を決定する必要がありました。次に、args.Colorプロパティを設定すると、DataGridColumnが正しい色を描画します。そうです、実際には各列をカスタムのフォーマット可能なクラスにする必要があります。そうすれば、アプリケーションロジックがイベントを処理し、レコードデータを取得し、色を決定できます。

**更新:ここに簡単な例があります**

public partial class Form1 : Form
{
    FormattableTextBoxColumn firstNameColumn = new FormattableTextBoxColumn();
    FormattableTextBoxColumn lastNameColumn = new FormattableTextBoxColumn();

    public Form1()
    {
        InitializeComponent();

        // add first name col
        firstNameColumn.MappingName = "FirstName";
        dataGridTableStyle1.GridColumnStyles.Add(firstNameColumn);
        firstNameColumn.SetCellFormat += new FormatCellEventHandler(ColumnSetCellFormat);

        // add last name col
        lastNameColumn.MappingName = "LastName";
        lastNameColumn.SetCellFormat += new FormatCellEventHandler(ColumnSetCellFormat);
        dataGridTableStyle1.GridColumnStyles.Add(lastNameColumn);

        // This just sets up a dummy data source, since I don't have a database in this example
        List<PersonTest> peopleList = new List<PersonTest>();
        peopleList.Add(new PersonTest
        {
            FirstName = "Alan",
            LastName = "QQQQQ",
            HighlightPerson = true
        });
        peopleList.Add(new PersonTest
        {
            FirstName = "John",
            LastName = "Smith",
            HighlightPerson = false
        });
        BindingSource peopleDataSource = new BindingSource();
        peopleDataSource.DataSource = peopleList;
        dataGridTableStyle1.MappingName = peopleDataSource.GetListName(null);
        dataGrid1.DataSource = peopleDataSource;
    }

    // I'll cache this brush in the form, just make sure to dispose it (see designer.cs disposing)
    SolidBrush highlightBrush = new SolidBrush(Color.Yellow);

    // here is the event you can handle to determine the color of your row!
    private void ColumnSetCellFormat(object sender, DataGridFormatCellEventArgs e)
    {
        if ((e.Source.List[e.Row] as PersonTest).HighlightPerson)
            e.BackBrush = highlightBrush;
    }

    // example test class
    public class PersonTest
    {
        public String FirstName { get; set; }
        public String LastName { get; set; }
        public bool HighlightPerson { get; set; }
    }
}

そして、カスタムデータグリッド列

public class FormattableTextBoxColumn : DataGridTextBoxColumn
{
    public event FormatCellEventHandler SetCellFormat;

    protected override void Paint(Graphics g, Rectangle bounds, CurrencyManager source, int rowNum, Brush backBrush, Brush foreBrush, bool alignToRight)
    {
        DataGridFormatCellEventArgs e = new DataGridFormatCellEventArgs(rowNum, source);
        e.ForeBrush = foreBrush;
        e.BackBrush = backBrush;
        OnSetCellFormat(e);
        base.Paint(g, bounds, source, rowNum, e.BackBrush, e.ForeBrush, alignToRight);
    }

    private void OnSetCellFormat(DataGridFormatCellEventArgs e)
    {
        FormatCellEventHandler handler = SetCellFormat;

        if (handler != null)
            handler(this, e);
    }
}

このDataGridCellEventArgs.csも必要になります

public delegate void FormatCellEventHandler(object sender, DataGridFormatCellEventArgs e);

public class DataGridFormatCellEventArgs : EventArgs
{
    public int Row;
    public CurrencyManager Source;
    public Brush BackBrush;
    public Brush ForeBrush;

    public DataGridFormatCellEventArgs(int row, CurrencyManager manager)
    {
        this.Row = row;
        this.Source = manager;
    }
}

これがあなたのためのサンプルプロジェクトです:

DataGridTest.zip

于 2012-10-19T22:41:40.150 に答える
0

私は CF で作業したことがありませんが、私はこれをそこに捨てると思っていました... セルにアクセスできる場合、行はそれではないでしょうNamingContainerか? その場合は、行にドリルアップしてスタイルを適用するか、CSS クラスを使用して属性を追加できます。

于 2012-10-16T06:43:21.667 に答える