2017-02-03 17 views
0

Im SQL Serverとaspxを使用しています。私はテーブルの値がコードビハインドファイルのSQLの値に応じて色を変更したい。したがって、表の値が== "yes"の場合、色は#ff0000に変更されます。テーブルの値が== "no"の場合は#00ff00に変更します。表はsubmitTableと呼ばれ、列はstatusです。私はGridviewを使用しています。データベースの値に応じてCSSの値を変更してください。asp.net

<asp:GridView ID="gridlistUsers" runat="server" HorizontalAlign="left" AutoGenerateColumns="false" CssClass="table table-bordered " GridLines="None"> 
    <Columns> 
     <asp:BoundField DataField="ID" HeaderText="TicketNumber" HeaderStyle-CssClass="Color" /> 
     <asp:BoundField DataField="ID" HeaderText="TicketNumber" HeaderStyle-CssClass="Color" /> 
     <asp:BoundField DataField="Status" HeaderText="Opened/Closed" HeaderStyle-CssClass="Color" /> 
    </Columns> 
</asp:GridView> 
+0

背後にあるコードを助けました?どんな種類の 'テーブル'をあなたは話していますか? – VDWWD

+0

私はいくつかのコードを追加しました@VDWWD –

答えて

1

背後

<asp:GridView ID="GridView1" runat="server" OnRowDataBound="GridView1_RowDataBound"> 

コード!これは私にあまりにもhttp://www.aspsnippets.com/Articles/Change-Background-color-of-GridView-Row-using-RowDataBound-event-in-ASPNet-using-C-and-VBNet.aspx

<asp:GridView ID="gridlistUsers" runat="server" HorizontalAlign="left" AutoGenerateColumns="false" CssClass="table table-bordered " OnRowDataBound = "OnRowDataBound"> 

//

あなたのコードや[、最小完全、かつ検証例]がある(http://stackoverflow.com/help/mcve)
protected void OnRowDataBound(object sender, GridViewRowEventArgs e) 
     { 
      if (e.Row.RowType == DataControlRowType.DataRow) 
      { 
       TableCell cell = e.Row.Cells[0]; //the 0 changes depending on the column number in sql 
       string status = (cell.Text); 
       string yes = "yes"; 
       string no = "no"; 

       if (status.Equals(no)) 
       { 
        cell.BackColor = Color.Red; 
       } 
       if (status.Equals(yes)) 
       { 
        cell.BackColor = Color.Green; 
       } 


      } 
     } 
2

このSQLテーブル用のREST APIを作成するには、aspでジョブを実行する必要があります。 次にAjax & JSを使って、各htmlテーブルに対してこの値を取得し、変数に保存することができます。シンプルで

そして:表:

if (your_sql_value === "yes") { 
    document.querySelector('.css-class').style.backgroundColor = "#ff0000" 
} else if (your_sql_value === "no") { 
    document.querySelector('.css-class').style.backgroundColor = "#00ff00" 
} 
+0

私は実際にはテーブルのgrideviewに必要です –

1

あなたの答えから、私はあなたが、ASPを使用していると仮定しています。

だから基本的に、これは行くことができる2つの方法があります。

1.あなたのテーブルには、すでにこのような場合は、この表の例をご確認

を充填してレンダリングされます。

イメージ:

enter image description here

.ASPX表コード:

<asp:Table ID="submitTable" runat="server" GridLines="Both"> 
    <asp:TableRow runat="server" HorizontalAlign="Center" TableSection="TableHeader" VerticalAlign="Middle"> 
     <asp:TableCell runat="server" Width="128px">ID</asp:TableCell> 
     <asp:TableCell runat="server" Width="128px">Status</asp:TableCell> 
    </asp:TableRow> 
</asp:Table> 

それは "yes" または "no" のランダムな値で満たされた表です。このような状況では、それぞれの行を反復可能性があり、単純に次のように色の属性を適用します。

int statusColumnIndex = 1; // Because it is the second column 

int k = 0; 
foreach (TableRow row in submitTable.Rows) 
{ 
    if(k != 0) // Do this so it won't color the header row 
    { 
     if(row.Cells[statusColumnIndex].Text.Equals("yes")) 
     { 
      row.Cells[statusColumnIndex].BackColor = Color.FromArgb(255, 0, 0); // 255, 0, 0 is #ff0000 in RGB 
     } 

     else 
     { 
      row.Cells[statusColumnIndex].BackColor = Color.FromArgb(0, 255, 0); // 0, 255, 0 is ##00ff00 in RGB 
     } 
    } 
    k++; 
} 

結果(異なるランダムな値):

enter image description here

または

2.表を塗りつぶすときに表の色を付ける場合

あなたがのTableRowを埋めるためのTableCellをインスタンス化するときこの場合、あなたはそれに色を適用することができは:

string statusValueFromSql = valueFromSqlQuery; 
Color color; 

if(statusValueFromSql.Equals("yes")) 
{ 
    color = Color.FromArgb(255, 0, 0); // 255, 0, 0 is #ff0000 in RGB 
} 
else 
{ 
    color = Color.FromArgb(0, 255, 0); // 0, 255, 0 is ##00ff00 in RGB 
} 

var cellStatus = new TableCell { BackColor = color }; 

は、最終的な結果は、私は私の答えを期待し、状況1.

とまったく同じです助けてください。

1

これにはOnRowDataBoundイベントを使用できます。私はそれが動作するように得ている

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) 
{ 
    //check if the row is a datarow 
    if (e.Row.RowType == DataControlRowType.DataRow) 
    { 
     //cast the current row as a datarowview 
     DataRowView row = e.Row.DataItem as DataRowView; 

     //check the correct column value 
     if (Convert.ToBoolean(row["Status"]) == false) 
     { 
      //set the color of the row directly 
      e.Row.BackColor = Color.Red; 

      //or change it's class 
      e.Row.CssClass = "Red"; 

      //or change a property per cell 
      e.Row.Cells[0].CssClass = "Blue"; 
      e.Row.Cells[1].BackColor = Color.Purple; 
     } 
    } 
} 
関連する問題