Set ForeColor & BackgroundColor of each Column For Each Row Separately in .Net Compact Framework 3.5

﹥>﹥吖頭↗ 提交于 2019-12-24 06:38:05

问题


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'.

来源:https://stackoverflow.com/questions/19923549/set-forecolor-backgroundcolor-of-each-column-for-each-row-separately-in-net-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!