Set ForeColor & BackgroundColor of each Column For

2019-08-07 06:53发布

I am developing a Window CE 5 Program With Visual Studio 2008.

By using a DataGrid Control in one of my forms, I need to change each row background and forecolor depending on some conditions. for doing this, I use this class:

public class ColoredDataGridTextBoxColumn : DataGridTextBoxColumn
{
    public Color BackgroundColor { get; set; }
    public Color ForeColor { get; set; }

    public ColoredDataGridTextBoxColumn()
    {
        BackgroundColor = Color.White;
        ForeColor = Color.Black;
    }

    protected override void Paint(Graphics g, Rectangle bounds, CurrencyManager source, int rowNum, Brush backBrush, Brush foreBrush, bool alignToRight)
    {
        backBrush = new SolidBrush(BackgroundColor);

        base.Paint(g, bounds, source, rowNum, backBrush, foreBrush, alignToRight);
    }
}

for creating rows and columns of grid, i use this way:

var dataTable = new DataTable();
dataTable.Columns.Add(new DataColumn("ّFirstName", typeof(string)));

var tableStyle = new DataGridTableStyle();
var tbcFirstName = new ColoredDataGridTextBoxColumn
    {
       Width = 65,
       MappingName = "FirstName",
       HeaderText = "نام"
    };

var tableStyle = new DataGridTableStyle();

dgDeliverData.TableStyles.Clear();
dgDeliverData.TableStyles.Add(tableStyle);

for (int index = 0; index < LstMemberNames.Length; index++)
{
   var memberName = LstMemberNames[index];

   var row = dataTable.NewRow();       

   row["FirstName"] = memberName;

   dataTable.Rows.Add(row);
}

dgDeliverData.DataSource = dataTable;

now i want change row background color & fore color if memberName for eg: is equal to 'member1'.

0条回答
登录 后发表回答