【问题标题】:How to Change GridView row color based on condition in C#如何根据 C# 中的条件更改 GridView 行颜色
【发布时间】:2014-10-30 09:07:24
【问题描述】:

我想根据某些条件更改 gridview 的特定行颜色,我正在使用带有 c# 的 ASP.NET。 这是示例输出:

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

提前致谢

【问题讨论】:

    标签: c#-4.0 gridview


    【解决方案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;
        }
    }
    

    【讨论】:

    • 这是一个 ASP.NET 网格
    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-06-19
      • 2018-03-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-04
      • 2013-08-28
      相关资源
      最近更新 更多