簡體   English   中英

根據數據更改gridview中行的顏色

[英]Changing the color of a row in gridview according to the data

我在 ASP.Net - C# 中有一個 Gridview。 我有一列名為AssignmentExam 在該列中,我有作業或考試的名稱,例如: "Exam 1" , "Assignment 5"我希望作為作業的每一行都應該是紅色的,而考試應該是藍色的。

在 SQL Server 中或在我的代碼中執行此操作的最佳方法是什么? 如果是這樣,正確的代碼是什么?

您可以通過為每一行單獨設置BackColor屬性來設置 Gridview 中一行的背景顏色。 要根據行中的數據執行此操作,您需要在行被綁RowDataBound檢查該行,您可以在RowDataBound事件中執行此操作。 這是我們連接到服務器端事件的基本 Gridview 的一些快速標記:

<asp:GridView runat="server" AutoGenerateColumns="False" OnRowDataBound="TestGridView_RowDataBound" ID="TestGridView">
    <Columns>
        <asp:BoundField DataField="Type" HeaderText="Assignment/Exam" />
        <asp:BoundField DataField="Name" HeaderText="Name" />
    </Columns>
</asp:GridView>

protected void Page_Load(object sender, EventArgs e)
{
    DataTable tests = new DataTable();
    tests.Columns.Add(new DataColumn("Type"));
    tests.Columns.Add(new DataColumn("Name"));
    tests.AcceptChanges();

    tests.Rows.Add(new []{"Assignment","StackOverflow Basics"});
    tests.Rows.Add(new[]{"Exam","Expert Markdown"});
    tests.Rows.Add(new[]{"Exam","Upvoting"});
    tests.Rows.Add(new[]{"Assignment","Rep Changes"});

    TestGridView.DataSource = tests;
    TestGridView.DataBind();
}

在事件的代碼中,我們可以獲取要綁定到的單個數據行並檢查值,因此我們可以相應地設置 BackColor:

protected void TestGridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
    // Ignore the first row which is the header
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        // Get hold of the row and then the DataRow that it's being bound to
        GridViewRow row = e.Row;
        DataRow data = ((DataRowView)row.DataItem).Row;

        // Look at the value and set the colour accordingly
        switch (data.Field<string>("Type"))
        {
            case "Assignment":
                row.BackColor = System.Drawing.Color.FromName("Blue");
                break;
            case "Exam":
                row.BackColor = System.Drawing.Color.FromName("Red");
                break;
        }
    }
}

這很好用,盡管您可能還想考慮將文本顏色設置為白色,這樣閱讀起來會更容易一些。

但是,您將來可能希望獲得更大的靈活性,例如,如果您添加名為“實驗室”的第三種評估類型,顏色為綠色,則您需要更改/重新編譯/重新測試/重新部署代碼。 相反,如果您從數據庫中向上傳遞命名顏色,然后在 RowDataBound 事件中使用它,則可以避免其中的一些工作,例如:

protected void TestGridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
    // Ignore the first row which is the header
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        // Get hold of the row and then the DataRow that it's being bound to
        GridViewRow row = e.Row;
        DataRow data = ((DataRowView)row.DataItem).Row;

        row.BackColor = System.Drawing.Color.FromName(data.Field<string>("BackColour");
        row.ForeColor = System.Drawing.Color.FromName(data.Field<string>("TextColour");
    }
}

首先在變量中找到行索引。 在下面的代碼中,我使用了 index 作為變量。

grdWithLocation.Rows[index].BackColor = Color.FromArgb(255, 238, 238, 238);

我在我的庫存數據庫中使用它。 它會檢查它是什么日期,並將它與數據庫中每個訂單的日期進行比較。 然后根據他們坐了多久,用不同的顏色為每一行着色。 它在 GridView 的 RowDataBound 上

您應該能夠修改代碼以更改考試/作業的日期

if (e.Row.RowType == DataControlRowType.DataRow)
{
    DateTime datToday = DateTime.Today();
    DateTime O1 = datToday.AddDays(0);
    DateTime O2 = datToday.AddDays(-1);
    DateTime O3 = datToday.AddDays(-4);

    string strMediaX = e.Row.Cells[2].Text;

    if (Information.IsDate(strMediaX))
    {
        DateTime MediaX = e.Row.Cells[2].Text;
        if (MediaX < O3)
        {
            e.Row.Cells[2].BackColor = Drawing.Color.OrangeRed;
            e.Row.Cells[2].ForeColor = Drawing.Color.White;
        }
        else if (MediaX < O2)
        {
            e.Row.Cells[2].BackColor = Drawing.Color.Orange;
            e.Row.Cells[2].ForeColor = Drawing.Color.White;
        }
        else if (MediaX < O1)
            e.Row.Cells[2].BackColor = Drawing.Color.Gold;
    }
    // checks the current stock of the item and if it is below 10 then it changes the colour to highlight that it needs to be ordered.
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        if ((Label)e.Row.Cells[9].FindControl("lblCurrentStock").Text < 11)
        {
            e.Row.Cells[9].BackColor = System.Drawing.Color.OrangeRed;
            e.Row.Cells[9].ForeColor = Drawing.Color.White;
            e.Row.Cells[9].Font.Bold = true;
        }
    }
}

暫無
暫無

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

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