iT邦幫忙

0

(asp.net) gridview 讀取預存程序出來後的欄位 想要轉換顏色

duby 2021-10-03 23:26:431390 瀏覽
  • 分享至 

  • xImage

自己找資料也嘗試了蠻久
但皆以失敗告終 想問各位有經驗的大大該怎麼做

aspx
<asp:GridView ID="GridView_mem_report" runat="server" HeaderText="人員銷售排名報表" AutoGenerateColumns="true" DataSourceID="SqlDataSource_order_data_mem" OnRowCreated="GridView_mem_report_RowCreated">
<asp:SqlDataSource ID="SqlDataSource_order_data_mem" runat="server" ConnectionString="<%$ ConnectionStrings:dubyprjConnectionString %>"
SelectCommand="[dbo].[order_data_mem]" SelectCommandType="StoredProcedure">

cs

protected void GridView_mem_report_RowCreated(object sender, GridViewRowEventArgs e)
{

        if (e.Row.Cells[2].ToString() != "銷售總額")
        {
            int number = 0; 
          bool b1 = int.TryParse(e.Row.Cells[2].ToString(), out number); 
            if (number > 10000)
            {
                string str = number.ToString();
                
            }

        }
    }

我自己是想從抓欄位順序下手
並把欄位名稱排除掉 然後設定條件改變字體顏色
但我轉型之後的str 卻找不到該用什麼還改變顏色
已經找資料找到有點茫然 想問有沒有高手有解

圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

2 個回答

0
asd3733653
iT邦新手 2 級 ‧ 2021-10-04 08:27:16
e.Row.Cells[1].ForeColor = System.Drawing.Color.Red;
看更多先前的回應...收起先前的回應...

如果可以的話 你可以截圖一下你的畫面 然後再詳細描述一下你要達成的效果

duby iT邦新手 5 級 ‧ 2021-10-05 00:37:20 檢舉

https://ithelp.ithome.com.tw/upload/images/20211005/2014273776UFW36En5.png

不好意思 這麼晚才回 好比這張圖片
我想要試著將銷售總額 達到1萬以上 和 獎金超過1500 的時候
讓兩個欄位內值的字體改變顏色

判斷你應該會寫吧?

但我轉型之後的str 卻找不到該用什麼還改變顏色

我看你的問題是不知道用什麼方式改變顏色
如果是這樣的話用上面的語法就可以了

e.Row.Cells[1].ForeColor = System.Drawing.Color.Red;
if (e.Row.Cells[2].ToString() != "銷售總額")
{
    int number = 0; 
    bool b1 = int.TryParse(e.Row.Cells[2].ToString(), out number); 
    if (number > 10000)
    {
        e.Row.Cells[2].ForeColor = System.Drawing.Color.Red;
        //改什麼顏色你可以自己選
    }

}
0
jack8900
iT邦新手 2 級 ‧ 2021-10-04 08:46:41
//前端
<asp:GridView ID="gv" runat="server" OnRowDataBound="gv_RowDataBound">
//.......
</asp:GridView>

//後端
protected void gv_RowDataBound(object sender, GridViewRowEventArgs e)
{
    GridViewRow gvr = e.Row;
    //底下條件自己下,我只是舉例
    //這樣就可以做到任何欄位想怎樣變換都可以
    if (Convert.ToInt64(gvr.Cells[15].Text) < 0)
    {
        //後面的是Css,需要在前端先設定好
        gvr.Cells[15].CssClass = "SPNum";
    }
}
duby iT邦新手 5 級 ‧ 2021-10-05 00:48:24 檢舉

https://ithelp.ithome.com.tw/upload/images/20211005/20142737WrrAx4HCBH.png

不好意思這麼晚回應
我有試著花一些時間做了 但是會跑出字串格式不正確的問題
但是我有額外將標題作排除 資料庫也沒有空值的資料
有點不能理解為何還是不正確

jack8900 iT邦新手 2 級 ‧ 2021-10-05 14:09:45 檢舉

1810.0000
嘗試底下的換看看?

 //Convert.ToInt32(0);
 Convert.ToDouble(0.00); 
 Convert.ToDecimal(0.00);

依照你的數字去選擇要轉換的方式,只不過通常都建議跟錢有關的使用Decimal

我要發表回答

立即登入回答