C# 使用绑定数据集中的值设置 Gridview 行背景颜色
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13153579/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Set Gridview Row Background Color using value in Binding DataSet
提问by Krishna Thota
I'm having a GridView which contains a Column ID
我有一个包含一列的 GridView ID
I'm having a DataTable which contains two columns
我有一个包含两列的数据表
ID
DONE
I'm Binding the IDColumn in the DataTable to the GridView.
Until no Its fine.
我ID将 DataTable 中的列绑定到 GridView。直到没有它的罚款。
But now I need to set the Background color of the GridView Row Basing on the DONEColumn Value in DataTable.( If DONEvalue is truethe Row Background Color has to be changed.)
但现在我需要根据DONEDataTable 中的列值设置 GridView 行的背景颜色。(如果DONE值是true行背景颜色,则必须更改。)
How can I achieve this without binding the DONERow to the GridView??
如何在不将DONERow绑定到 GridView 的情况下实现这一点?
采纳答案by Niranjan Singh
Create GridView1_RowDataBoundevent for your GridView.
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
}
}
example code snippet:
示例代码片段:
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;
}
}
Refer following link for more detailed implementation:
Change GridView row color based on condition
有关更详细的实现,请参阅以下链接:
根据条件更改 GridView 行颜色
Note: If that row does not exist in DataSource then you must have some logic to get that from other place. May be you have IDas Foreign Key in another table.
注意:如果该行在 DataSource 中不存在,那么您必须有一些逻辑才能从其他地方获取该行。可能你ID在另一个表中有外键。
回答by m4ngl3r
Create MyGridView _RowDataBound event for your GridView.
为您的 GridView 创建 MyGridView _RowDataBound 事件。
if (e.Row.RowType = DataControlRowType.DataRow)
{
//Check your condition here, Cells[1] for ex. is DONE/Not Done column
If(e.Row.Cells[1].Text == "DONE")
{
e.Row.BackColor = Drawing.Color.Green // This will make row back color green
}
}
回答by Rajesh Murugan
I also solved my condition. But For Alternative row type, the background color is not set.
我也解决了我的情况。但是对于替代行类型,未设置背景颜色。
if (e.Row.RowType == DataControlRowType.DataRow)
{
Label LabelStatus = (Label)e.Row.FindControl("lblStatus");
if(LabelStatus.Text.Trim().ToLower().Equals("inactive"))
{
e.Row.BackColor = System.Drawing.Color.Gray;
}
}
Can you please let me know what may be the reason?
你能告诉我可能是什么原因吗?
回答by Ramu
This link might help you
这个链接可能对你有帮助
if (e.Row.RowType == DataControlRowType.DataRow)
{
// determine the value of the UnitsInStock field
if((DataBinder.Eval(e.Row.DataItem,"strShift")).ToString() =="Alarm")
{
// color the background of the row yellow
e.Row.BackColor = Color.Yellow;
}

