从主DataGrid的背景颜色创建DataGrid [英] Create DataGrid From Background Color Of Primary DataGrid
问题描述
如果我们需要进一步审核,我在winform上显示了一个Datagridview,其中backgroundcolor = Red。
I have a Datagridview displayed on a winform where the backgroundcolor = Red if we need to further review.
是否有然后我可以采取所有backgroundcolor = Red行并创建第二个Datagridview并在按钮点击时显示?
Is there a way that I can then take all of the backgroundcolor = Red rows and create a second Datagridview and display on button click?
这是语法我用于主要网格为背景着色
This is the syntax I am using for the primary gird to color the background
private void dgvMain_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
foreach (DataGridViewRow row in dgvBeerBoard.Rows)
{
//checking for null values across the board
if (row.Cells["B"].Value == System.DBNull.Value && row.Cells["P"].Value == System.DBNull.Value && row.Cells["PC"].Value == System.DBNull.Value &&row.Cells["BD"].Value == System.DBNull.Value)
{
row.DefaultCellStyle.BackColor = Color.Red;
}
if (Convert.ToString(row.Cells["B Price"].Value) == "1.00")
{
row.DefaultCellStyle.BackColor = Color.Red;
}
if (Convert.ToString(row.Cells["B Price"].Value) == "0.00")
{
row.DefaultCellStyle.BackColor = Color.Red;
}
if (row.Cells["Price"].Value != System.DBNull.Value && row.Cells["P"].Value != System.DBNull.Value && row.Cells["PC"].Value != System.DBNull.Value && row.Cells["B"].Value != System.DBNull.Value)
{
row.DefaultCellStyle.BackColor = Color.White;
}
}
}
推荐答案
以下是未经测试的,因为我不打算在DataGridView中设置内容。
The following is untested as I'm not going to setup what you have for contents in a DataGridView.
你应该能够说出来红色行例如
You should be able to get say red rows e.g.
var rows = dgvBeerBoard
.Rows.OfType<DataGridViewRow>()
.Where(row => row.DefaultCellStyle.BackColor == System.Drawing.Color.Red)
.Select(row => row);
然后迭代上面的行,使用以下扩展方法将上面的行复制到另一个DataGridView。请注意,克隆方法是复制行的关键
Then iterate the above row, use the following extension method to copy the above rows to the other DataGridView. Note the Clone method is the key for copying row
public static class Extensions
{
/// <summary>
/// </summary>
/// <param name="SingleRow">Row to copy</param>
/// <param name="Target">DataGridView to recieve the SingeRow</param>
public static void CloneRowWithValues(this DataGridViewRow SingleRow, DataGridView Target)
{
DataGridViewRow Results = (DataGridViewRow)SingleRow.Clone();
for (int Row = 0; Row < SingleRow.Cells.Count; Row++)
{
Results.Cells[Row].Value = SingleRow.Cells[Row].Value;
}
Target.Rows.Add(Results);
}
}
这篇关于从主DataGrid的背景颜色创建DataGrid的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!