понедельник, 7 ноября 2011 г.

ASPxGridview cell color depending on row value

In this post I want to describe how to change cell color in APpxGridview. Cell color is changed in one column depending on row value.I used ASPxGridview's HtmlDataCellPrepared event handler.


CellColorInGridview.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="CellColorInGridview.aspx.cs" Inherits="CellColorInGridview" %>

<%@ Register Assembly="DevExpress.Web.ASPxGridView.v10.2, Version=10.2.4.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a"
    Namespace="DevExpress.Web.ASPxGridView" TagPrefix="dx" %>

<%@ Register assembly="DevExpress.Web.ASPxEditors.v10.2, Version=10.2.4.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a" namespace="DevExpress.Web.ASPxEditors" tagprefix="dx" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <dx:ASPxGridView ID="ASPxGridView1" runat="server" AutoGenerateColumns="False"
            ClientIDMode="AutoID" DataSourceID="SqlDataSource1" KeyFieldName="ID"
            onhtmldatacellprepared="ASPxGridView1_HtmlDataCellPrepared">
            <Columns>
                <dx:GridViewDataTextColumn FieldName="ID" ReadOnly="True" VisibleIndex="0">
                    <EditFormSettings Visible="False" />
                </dx:GridViewDataTextColumn>
                <dx:GridViewDataTextColumn FieldName="Data" VisibleIndex="1">
                </dx:GridViewDataTextColumn>
                <dx:GridViewDataTextColumn FieldName="Status" VisibleIndex="2">
                </dx:GridViewDataTextColumn>
            </Columns>
        </dx:ASPxGridView>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server"
            ConnectionString="<%$ ConnectionStrings:testConnectionString %>"
            SelectCommand="SELECT [ID], [Data], [Status] FROM [testTable]"></asp:SqlDataSource>
    </div>
    </form>
</body>
</html>




CellColorInGridview.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class CellColorInGridview : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void ASPxGridView1_HtmlDataCellPrepared(object sender, DevExpress.Web.ASPxGridView.ASPxGridViewTableDataCellEventArgs e)
    {
        string Status = ASPxGridView1.GetRowValues(Convert.ToInt32(e.VisibleIndex), "Status").ToString();

        if (e.DataColumn.FieldName == "Status")
        {
            if (Status == "Sucsess")
            {
                e.Cell.BackColor = System.Drawing.Color.FromArgb(0xF5, 0xF2, 0xED);
            }
            else if (Status == "Fault")
            {
                e.Cell.BackColor = System.Drawing.Color.FromArgb(0xE9, 0xE1, 0xD3);
            }
        } 
    }
}

Комментариев нет:

Отправить комментарий