我有一个包含 Column ID
我有一个包含两列的数据表
ID
DONE
我将 DataTable 中的 ID
列绑定(bind)到 GridView。
直到没有它的罚款。
但现在我需要根据 DataTable 中的 DONE
列值设置 GridView 行的背景颜色。(如果 DONE
值为 true
必须更改行背景颜色。)
如何在不将 DONE
行绑定(bind)到 GridView 的情况下实现这一目标?
最佳答案
为您的 GridView 创建 GridView1_RowDataBound
事件。
if (e.Row.RowType == DataControlRowType.DataRow)
{
//Check your condition here
//Get Id from here and based on Id check value in the
//underlying dataSource Row where you have "DONE" column value
// e.g.
// (gridview.DataSource as DataTable), now you can find your row and cell
// of "Done"
If(Condition True)
{
e.Row.BackColor = Drawing.Color.Red; // your color settings
}
}
示例代码片段:
protected void EmployeeAvailabilityGridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
try
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if(Convert.ToBoolean(DataBinder.Eval(e.Row.DataItem, "DONE")))
{
e.Row.BackColor = System.Drawing.Color.LightPink;
}
}
}
catch (Exception ex)
{
//ErrorLabel.Text = ex.Message;
}
}
更详细的实现请引用以下链接:
Change GridView row color based on condition
注意:如果 DataSource 中不存在该行,那么您必须有一些逻辑从其他地方获取它。可能是您将 ID
作为另一个表中的外键。
关于c# - 使用绑定(bind)数据集中的值设置 Gridview 行背景颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13153579/