Search
 
SCRIPT & CODE EXAMPLE
 

CSHARP

c# datafield change cell background color

<%@ Page Language="C#" AutoEventWireup="true"%>
<%@ Import Namespace="System" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.Sql" %>
<%@ Import Namespace="System.Drawing" %>

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

<script runat="server">
    protected void Page_Load(object sender, EventArgs e)
    {
        //Doing the binding when the page is loading for the first time (not on postbacks)
        if (!IsPostBack)
        {
            //Test datasource (Creating a datatable with 10 columns. Then adding 3 rows. cell indeces are 0 based.)

            DataTable dt = new DataTable();

            DataColumn dc1 = new DataColumn("col1");
            DataColumn dc2 = new DataColumn("col2");
            DataColumn dc3 = new DataColumn("col3");
            DataColumn dc4 = new DataColumn("col4");
            DataColumn dc5 = new DataColumn("col5");
            DataColumn dc6 = new DataColumn("col6");
            DataColumn dc7 = new DataColumn("col7");
            DataColumn dc8 = new DataColumn("col8");
            DataColumn dc9 = new DataColumn("col9");
            DataColumn dc10 = new DataColumn("col10");

            dt.Columns.Add(dc1);
            dt.Columns.Add(dc2);
            dt.Columns.Add(dc3);
            dt.Columns.Add(dc4);
            dt.Columns.Add(dc5);
            dt.Columns.Add(dc6);
            dt.Columns.Add(dc7);
            dt.Columns.Add(dc8);
            dt.Columns.Add(dc9);
            dt.Columns.Add(dc10);

            //Second row index 9 has "Missing" as the text
            dt.Rows.Add(new object[] { "cell1", "cell2", "cell3", "cell4", "cell5", "cell6", "cell7", "cell8", "cell9", "cell10" });
            dt.Rows.Add(new object[] { "cell1", "cell2", "cell3", "cell4", "cell5", "cell6", "cell7", "cell8", "cell9", "Missing" });
            dt.Rows.Add(new object[] { "cell1", "cell2", "cell3", "cell4", "cell5", "cell6", "cell7", "cell8", "cell9", "cell10" });

            //Set datasource. Then bind it. (here the grid is using auto generated columns)
            SYSGrid.DataSource = dt;
            SYSGrid.DataBind();
        }
    }

    protected void SYSGrid_RowDataBound(object sender, GridViewRowEventArgs e)    
    {
       if (e.Row.RowType == DataControlRowType.DataRow)
       {
          if (e.Row.Cells[9].Text == "Missing")
          {
              e.Row.Cells[9].BackColor = Color.Red;
              e.Row.Cells[9].ForeColor = Color.White;
           }
        }
    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>    
        <asp:gridview runat="server" ID="SYSGrid" OnRowDataBound="SYSGrid_RowDataBound"></asp:gridview>
    </div>
    </form>
</body>
</html>
Comment

PREVIOUS NEXT
Code Example
Csharp :: copy file image in c# 
Csharp :: c# string interpolation float format 
Csharp :: remove starting 0 in astring C# 
Csharp :: add RowDefinition from cs xamarin 
Csharp :: delay a function on winform 
Csharp :: angular === vs == 
Csharp :: c# aabb box rotate 
Csharp :: to string c# fields 
Csharp :: Custom Encrypted String Type 
Csharp :: ExecuteResultAsync 
Csharp :: writeline in C# 
Csharp :: chrome devtools capture all styling an element uses 
Csharp :: .net framework method 
Csharp :: get path c# application 
Csharp :: c# language 
Csharp :: C# resize window without title bar 
Csharp :: PasswordBox Helper 
Csharp :: ignore collision unity 2d 
Csharp :: SETTING UP ARRAY FOR TEST SCORES IN C# 
Csharp :: wpf repository pattern query async with includes properties 
Csharp :: c# parsing datetime from string irrespctive of culture 
Csharp :: how to do multiplication with button c# 
Csharp :: Destroy(GameObject.Find("Turret_Laser_Hit"), 0.2f); 
Csharp :: invalid length for a base-64 char array or string. frombase64string c#Add Answer 
Csharp :: c# string replace multiple matches with one charactar 
Csharp :: xamarin c# switch on hotspot Programmatically 
Csharp :: What is the best way to lock cache in asp.net? 
Csharp :: Get dwgexport setting reivit api 
Csharp :: linq query languages 
Csharp :: skrivetænking 
ADD CONTENT
Topic
Content
Source link
Name
5+1 =