你的問題被標記WPF,這是的WinForms回答...
DataGridView的行與列模板(的DataGridViewCellStyle類)風格。
下面是一組拼接在一起的代碼組,將行添加到網格中。 theGrid是我們要添加行的控件。事件是從數據庫返回的POCO。
var rowCellStyle = new DataGridViewCellStyle(theMessagesGrid.DefaultCellStyle)
{
BackColor = string.IsNullOrEmpty(conditions)
? theGrid.DefaultCellStyle.BackColor
: theColor,
SelectionForeColor = Color.WhiteSmoke,
SelectionBackColor = theGrid.DefaultCellStyle.SelectionBackColor,
};
var theRow = new DataGridViewRow
{
Height = theGrid.RowTemplate.Height,
DefaultCellStyle = rowCellStyle,
Tag = Event.GroupName
};
theRow.CreateCells(theGrid);
var cellData = new object[theRow.Cells.Count];
// fill out cell data
cellData[0] = ...;
cellData[1] = ...
theRow.SetValues(cellData);
// add row to grid
try
{
theGrid.Rows.Add(theRow);
if (currentMsg == Event.Pkey) theGrid.Rows[theGrid.Rows.Count - 1].Selected = true;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, @"Error Building Grid", MessageBoxButtons.OK, MessageBoxIcon.Warning);
throw;
}
WPF應該有某種形式適用於行。添加存儲行模板的表單屬性,然後根據條件更新rowCellStyle。
問題標題和問題要求犯規的比賽。 – AnjumSKhan