如何在Datagridview行单元格中设置Backcolor
问题描述:
任何人都可以帮我解决我的问题。
我在子格式中有一个有界的数据网格视图。当我在mdi中加载子表单(使MdiParent = Me)时,我的自定义颜色的行不会显示。如果我删除MdiParent = Me然后着色工作。
Hi,
any one can help me on my problems.
I have a bounded datagridview in a child form. When I load the child form in the mdi (making MdiParent = Me) then my custom color of the rows does not show. If I remove MdiParent = Me then the coloring works.
{ DataTable dt = new DataTable();
dt = genbl.GetReminderData();
dgvReminderSetting.DataSource = dt;
DataTable dt2 = new DataTable();
DataColumn dc = dt2.Columns.Add("cl", typeof( Color));
for (int i = 0; i < dt.Rows.Count; i++)
{
Color c = Color.FromArgb(Convert.ToInt32(dt.Rows[i].ItemArray[6].ToString()));
dgvReminderSetting.Rows[i].Cells["ColorColumn"].Style.BackColor = c;
}
//Color.FromArgb(-8323073);
this.Invalidate();
dgvReminderSetting.Columns["ReminderDescription"].ReadOnly = true;
dgvReminderSetting.Columns["ColorColumn"].ReadOnly = true;
dgvReminderSetting.Columns["ReminderSettingId"].Visible = false;
}
这是我在frm_load中的代码
这里没有在datagridview单元格中显示backcolor
可以从dialogcolor中保存数据库中的颜色然后绑定到c中的gridview#
先谢谢
this is my code in frm_load
here not showing backcolor in datagridview cells
it is possible to save color in database from dialogcolor then bind to gridview in c#
Thanks in Advance
答
使用网格的CellFormatting evetn;
Hi,
Use CellFormatting evetn of grid like;
private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
if (e.Value != null && e.Value.Equals("something"))
{
e.CellStyle.BackColor = Color.Yellow;
e.CellStyle.SelectionBackColor = Color.Yellow;
e.CellStyle.SelectionForeColor = Color.Black;
}
else if (e.Value != null && e.Value.Equals("else"))
{
e.CellStyle.BackColor = Color.Red;
e.CellStyle.SelectionBackColor = Color.Red;
}
}