簡體   English   中英

使用綁定數據集中的值設置網格視圖行背景顏色

[英]Set Gridview Row Background Color using value in Binding DataSet

我有一個包含列ID的GridView

我有一個包含兩列的DataTable

ID
DONE

我將DataTable中的ID列綁定到GridView。 直到沒有它的罰款。

但是現在我需要在DataTable中的DONE列值上設置GridView行的背景顏色。(如果DONE值為true ,則必須更改行背景顏色。)

如何在不將DONE Row綁定到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;
            }
        }

有關更詳細的實施,請參閱以下鏈接
根據條件更改GridView行顏色

注意 :如果DataSource中不存在該行,那么您必須具有一些邏輯才能從其他位置獲取該行。 可能是您在另一個表中將ID作為外鍵。

此鏈接可能對您有所幫助

http://deepak-sharma.net/2012/01/27/how-to-change-the-background-color-of-rows-in-a-gridview-based-on-the-value-of-a-列在-ASP-凈-3-5 /

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;
            }

為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
    }
}

我也解決了我的病情。 但對於替代行類型,未設置背景顏色。

       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;
            }                   

        }

你能告訴我可能是什么原因嗎?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM