我有一个包含 Column 的 GridViewID
我有一个包含两列的数据表
ID
DONE
我正在绑定ID
DataTable 中的列到 GridView。
直到没有它就好了。
但现在我需要根据以下内容设置 GridView 行的背景颜色DONE
数据表中的列值。(如果DONE
值为true
必须更改行背景颜色。)
我怎样才能在不绑定的情况下实现这一目标DONE
行到GridView?
Create GridView1_RowDataBound
GridView 的事件。
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;
}
}
更详细的实现请参考以下链接:
根据条件更改 GridView 行颜色 https://stackoverflow.com/questions/5048762/change-gridview-row-color-based-on-condition-in-c-sharp
Note:如果数据源中不存在该行,那么您必须有一些逻辑从其他地方获取该行。也许你有ID
作为另一个表中的外键。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)