3

devexpress グリッドビューで行の色を緑に変更しようとしています。検索すると、これを行う方法の多くの例が見つかりました...ウェブサイトでしかし、これはWindowsアプリケーションです。Windows アプリケーションで何かを見つけることができないので、誰かがここで少し助けてくれます。

単一の行の色を緑に変更したいだけです。

言い忘れましたが、これは C# アプリケーションです。

助けてくれてありがとう。

4

2 に答える 2

6

実行時に行の色を変更するには、RowStyle イベントを処理します。

    public Color color1;
    public Color color2;
    public int rowhandle;

    private void gridView1_RowStyle(object sender, DevExpress.XtraGrid.Views.Grid.RowStyleEventArgs e)
    {
        try
        {
            if (e.RowHandle == rowhandle)
            {
                if (color1 != null && color2 != null)
                {
                    e.Appearance.BackColor = color1;
                    e.Appearance.BackColor2 = color2;
                }
            }
        }
        catch
        {
        }
    }

    private void button1_Click(object sender, EventArgs e)
    {
        color1 = Color.BurlyWood;
        color2 = Color.DarkOrchid;
        rowhandle = gridView1.FocusedRowHandle;
        gridView1.RefreshRow(rowhandle);
    }

次のコードは色を維持します。

public partial class Form1 : Form
{

    public Color color1;
    public Color color2;
    public int rowhandle;
    public List<int> rowhandles;

    private void gridView1_RowStyle(object sender, DevExpress.XtraGrid.Views.Grid.RowStyleEventArgs e)
    {
        try
        {
            if (rowhandles.Any(x=>x==e.RowHandle))
            {
                if (color1 != null && color2 != null)
                {
                    e.Appearance.BackColor = color1;
                    e.Appearance.BackColor2 = color2;
                }
            }
        }
        catch
        {
        }
    }

    private void button1_Click(object sender, EventArgs e)
    {
        color1 = Color.BurlyWood;
        color2 = Color.DarkOrchid;
        rowhandle = gridView1.FocusedRowHandle;
        if (!rowhandles.Any(x => x == rowhandle))
            rowhandles.Add(rowhandle);
        gridView1.RefreshRow(rowhandle);
    }

    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        Dictionary<int, string> l = new Dictionary<int, string>();
        l.Add(1,"one");
        l.Add(2,"two");
        l.Add(3,"three");
        l.Add(4, "four");
        l.Add(5, "five");
        l.Add(6, "six");
        l.Add(7, "seven");
        l.Add(8, "eight");
        l.Add(9, "nine");

        gridControl1.DataSource = l.ToList();

        rowhandles = new List<int>();
    }

}
于 2013-08-12T13:56:12.357 に答える
0

I suggest you use the following simple solution:

using DevExpress.XtraGrid;
using DevExpress.XtraGrid.Views.Base;
//...
gridControl1.DataSource = new List<DataObj> {
    new DataObj(){ ID=0, Name="A" },
    new DataObj(){ ID=1, Name="B" },
    new DataObj(){ ID=2, Name="C" },
    new DataObj(){ ID=3, Name="D" },
};
gridView1.CustomDrawCell += gridView1_CustomDrawCell;
//...
void gridView1_CustomDrawCell(object sender, RowCellCustomDrawEventArgs e) {
    if(selectedRowHandle.GetValueOrDefault(GridControl.InvalidRowHandle) == e.RowHandle) {
        e.Appearance.BackColor = Color.Green;
    }
}
int? selectedRowHandle;
void button1_Click(object sender, EventArgs e) {
    int prevSelectedRowHandle = selectedRowHandle.GetValueOrDefault(GridControl.InvalidRowHandle);
    if(prevSelectedRowHandle != GridControl.InvalidRowHandle)
        gridView1.RefreshRow(prevSelectedRowHandle); // reset row-style to default
    selectedRowHandle = gridView1.FocusedRowHandle;
    gridView1.InvalidateRow(gridView1.FocusedRowHandle); // row painting request
}

This solution is based on the GridView.CustomDrawCell event that is preferred when it is only needed to change some specific row appearance. It also works for currently selected row rather then GridView.RowCellStyle.

Related help topic: Customizing Appearances of Individual Rows and Cells

于 2013-08-12T15:00:40.640 に答える