如何根据C#中的条件更改GridView行颜色

时间:2014-10-30 09:07:24

标签: c#-4.0 gridview

我想根据某些条件更改gridview的特定行颜色,我使用ASP.NET和c#。 以下是输出示例: Sample Output

 <cc1:PagingGridView ID="TaskNameGrid"
            runat="server"
            AllowPaging="True"
            AutoGenerateColumns="False"
            CssClass="SearchResultsTable"
            ShowHeader="true"
            virtualitemcount1="-1"
            OnRowdatabound="TaskNameGrid_Rowdatabound"
            OnRowCancelingEdit="TaskNameGrid_RowCancelingEdit" 
            OnRowEditing="TaskNameGrid_RowEditing" 
            OnRowUpdating="TaskNameGrid_RowUpdating"
             Width="400px">
        <AlternatingRowStyle CssClass="alternate" />
        <HeaderStyle ForeColor="White" />
        <RowStyle />
        <Columns>
         <asp:BoundField HeaderText="Task Name" DataField="BusinessIdentifier" SortExpression="BusinessIdentifier" ReadOnly="True"></asp:BoundField>
             <asp:TemplateField HeaderText="SLA" InsertVisible="False" SortExpression="sno">
              <EditItemTemplate>
                   <asp:TextBox ID="SlaVariationTextBox" runat="server"  Text='<%# Bind("SLA") %>'></asp:TextBox>
              </EditItemTemplate>
              <ItemTemplate>
                   <asp:Label ID="Label1" runat="server" Text='<%# Bind("SLA", "not set") %>'></asp:Label>  
              </ItemTemplate>
             </asp:TemplateField>
          <asp:CommandField ShowEditButton="true" />
        </Columns>
     </cc1:PagingGridView>

提前致谢

2 个答案:

答案 0 :(得分:1)

如蒂姆所说,使用RowDataBound

HTML:

            <div>
    <asp:GridView ID="TaskNameGrid"
        runat="server"
        AllowPaging="True"
        AutoGenerateColumns="False"
        CssClass="SearchResultsTable"
        virtualitemcount1="-1"
         Width="400px" DataSourceID="ObjectDataSource1" 
                onrowdatabound="TaskNameGrid_RowDataBound">
    <AlternatingRowStyle CssClass="alternate" />
        <Columns>
            <asp:BoundField DataField="BusinessIdentifier" HeaderText="BusinessIdentifier" 
                SortExpression="BusinessIdentifier" />
            <asp:BoundField DataField="SLA" HeaderText="SLA" SortExpression="SLA" />
        </Columns>
    <HeaderStyle ForeColor="White" />
    <RowStyle />

    </asp:GridView>


            <asp:ObjectDataSource ID="ObjectDataSource1" runat="server" SelectMethod="Get" 
                TypeName="DataForGrid">
            </asp:ObjectDataSource>


        </div>

Aspx活动

    protected void TaskNameGrid_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        var d = (DataStructure)e.Row.DataItem;

        if (string.IsNullOrEmpty(d.SLA))
        {
            e.Row.BackColor = System.Drawing.Color.Red;

        }
    }
}

数据层类

    public class DataStructure
{
    public string BusinessIdentifier { get; set; }

    public string SLA { get; set; }

    public DataStructure()
    {


    }
}

public class DataForGrid
{
    public DataForGrid()
    {
        this.Stuff = new List<DataStructure>();

        this.Stuff.Add(new DataStructure { BusinessIdentifier = "TaskName1", SLA = "1.2" });
        this.Stuff.Add(new DataStructure { BusinessIdentifier = "Taskname2", SLA = null });
        this.Stuff.Add(new DataStructure { BusinessIdentifier = "TaskName3", SLA = "1.2" });
        this.Stuff.Add(new DataStructure { BusinessIdentifier = "TaskName4", SLA = "1.2" });
        this.Stuff.Add(new DataStructure { BusinessIdentifier = "TaskName5", SLA = "1.2" });

    }
    public List<DataStructure> Stuff { get; set; }

    public List<DataStructure> Get()
    {




        return this.Stuff;
    }
}

答案 1 :(得分:0)

使用RowDataBound事件。您可以使用行DataItem来获取基础DataSource

protected void gridview1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        // modify it accoording to your datasource, use the debugger if you're not sure
        DataRow row = ((DataRowView)e.Row.DataItem).Row;
        // just an example:
        bool redCondition = row.Field<string>("SomColumn") == "Some Value";
        e.Row.BackColor = redCondition ? Color.Red : GridView1.RowStyle.BackColor;
    }
}