[英]Change GridView row color based on condition
我想根據某些條件更改 gridview 的特定行顏色。 我在 C# 中使用 ASP.NET。
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
e.Row.Attributes.Add("style", "cursor:help;");
if (e.Row.RowType == DataControlRowType.DataRow && e.Row.RowState == DataControlRowState.Alternate)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor='orange'");
e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor='#E56E94'");
e.Row.BackColor = Color.FromName("#E56E94");
}
}
else
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor='orange'");
e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor='gray'");
e.Row.BackColor = Color.FromName("gray");
}
//e.Row.Cells[0].BackColor = Color.FromName("gray");
//e.Row.Cells[1].BackColor = Color.FromName("gray");
//e.Row.Cells[2].BackColor = Color.FromName("gray");
//e.Row.Cells[3].BackColor = Color.FromName("gray");
//e.Row.Cells[4].BackColor = Color.FromName("gray");
//e.Row.BorderWidth = 2;
//e.Row.BorderColor = Color.FromName("#43C6DB");
}
}
protected void DrugGridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
// To check condition on integer value
if (Convert.ToInt16(DataBinder.Eval(e.Row.DataItem, "Dosage")) == 50)
{
e.Row.BackColor = System.Drawing.Color.Cyan;
}
}
為GridView創建GridView1_RowDataBound
事件。
//Check if it is not header or footer row
if (e.Row.RowType == DataControlRowType.DataRow)
{
//Check your condition here
If(Condition True)
{
e.Row.BackColor = Drawing.Color.Red // This will make row back color red
}
}
如果其中一列中出現特定字符串(“TextToMatch”),則此方法會修改背景顏色(深紅色)和文本(白色):
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.Cells[8].Text.Equals("TextToMatch"))
{
e.Row.BackColor = System.Drawing.Color.DarkRed;
e.Row.ForeColor = System.Drawing.Color.White;
}
}
或者另一種寫作方式:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.Cells[8].Text.Equals("TextToMatch"))
{
e.Row.Attributes.CssStyle.Value = "background-color: DarkRed; color: White";
}
}
或者,您可以將行DataItem強制轉換為類,然后根據類屬性添加條件。 這是我用來將行轉換為名為TimetableModel的類/模型的示例,然后在if語句中您可以訪問所有類字段/屬性:
protected void GridView_TimeTable_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
var tt = (TimetableModel)(e.Row.DataItem);
if (tt.Unpublsihed )
e.Row.BackColor = System.Drawing.Color.Red;
else
e.Row.BackColor = System.Drawing.Color.Green;
}
}
}
protected void gridview1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
Label lbl_Code = (Label)e.Row.FindControl("lblCode");
if (lbl_Code.Text == "1")
{
e.Row.BackColor = System.Drawing.ColorTranslator.FromHtml("#f2d9d9");
}
}
}
對於未來的程序員,如果你還沒有得到解決方案。 以下代碼適用於我根據條件(包含字符串值的行的不同類型)更改行顏色:
public YourClassName()
{
InitializeComponent();
SetAlternateRowColor();
}
public void SetAlternateRowColor()
{
for (int iCount = 0; iCount < reservationDataGridView.Rows.Count; iCount++)
{
if (Convert.ToString(reservationDataGridView.Rows[iCount].Cells[7].Value) == "CellValue")
reservationDataGridView.Rows[iCount].DefaultCellStyle.BackColor = Color.Orange;
else if (Convert.ToString(reservationDataGridView.Rows[iCount].Cells[7].Value) == "CellValue")
reservationDataGridView.Rows[iCount].DefaultCellStyle.BackColor = Color.GreenYellow;
else
reservationDataGridView.Rows[iCount].DefaultCellStyle.BackColor = Color.Red;
}
}
\\loop throgh all rows of the grid view
if (GridView1.Rows[i - 1].Cells[4].Text.ToString() == "value1")
{
GridView1.Rows[i - 1].ForeColor = Color.Black;
}
else if (GridView1.Rows[i - 1].Cells[4].Text.ToString() == "value2")
{
GridView1.Rows[i - 1].ForeColor = Color.Blue;
}
else if (GridView1.Rows[i - 1].Cells[4].Text.ToString() == "value3")
{
GridView1.Rows[i - 1].ForeColor = Color.Red;
}
else if (GridView1.Rows[i - 1].Cells[4].Text.ToString() == "value4")
{
GridView1.Rows[i - 1].ForeColor = Color.Green;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.