2013-09-25 24 views
-1
if (e.Row.RowType == DataControlRowType.DataRow) 
    { 
     int esal = int.Parse(e.Row.Cells[3].Text.ToString()); 
     if (esal > 12000) 
     { 
      e.Row.ForeColor = System.Drawing.Color.Blue; 
      e.Row.BackColor = System.Drawing.Color.LightPink; 
      e.Row.Font.Italic = true; 
     } 
     else if (esal == 15000) 
     { 
      e.Row.ForeColor = System.Drawing.Color.Brown; 
      e.Row.BackColor = System.Drawing.Color.LightBlue; 
      e.Row.Font.Italic = true; 
     } 
     else 
     { 
      e.Row.ForeColor = System.Drawing.Color.White; 
      e.Row.BackColor = System.Drawing.Color.LightGreen; 
      e.Row.Font.Italic = true; 
     } 

    } 

我嘗試這樣做,但我得到了像輸入字符串的例外是不正確format..please幫我...如何設置排顏色的基礎上Emp_sal列GridView控件值

+1

e.Row.Cells [3] .Text.ToString()是什麼值? – rach

+0

我使用int數據類型爲數據庫中的Emp_sal列 – Sambasiva

+1

它是否獲取正確的單元格值? – rach

回答

1
if (e.Row.RowType == DataControlRowType.DataRow) 
{ 
if (!string.IsNullOrEmpty(e.Row.Cells[3].Text)) 
{ 

    int esal = int.Parse(e.Row.Cells[3].Text.ToString()); 
    if (esal > 12000) 
    { 
     e.Row.ForeColor = System.Drawing.Color.Blue; 
     e.Row.BackColor = System.Drawing.Color.LightPink; 
     e.Row.Font.Italic = true; 
    } 
    else if (esal == 15000) 
    { 
     e.Row.ForeColor = System.Drawing.Color.Brown; 
     e.Row.BackColor = System.Drawing.Color.LightBlue; 
     e.Row.Font.Italic = true; 
    } 
    else 
    { 
     e.Row.ForeColor = System.Drawing.Color.White; 
     e.Row.BackColor = System.Drawing.Color.LightGreen; 
     e.Row.Font.Italic = true; 
    } 
} 
} 
+0

我使用int數據類型爲此 – Sambasiva

+0

Samba Siva - 我編輯的答案 –

1

試試這個

if (e.Row.RowType == DataControlRowType.DataRow) 
{ 
    int esal = -1; 
    if(int.TryParse(e.Row.Cells[3].Text.ToString(),out esal)) 
    { 
     if (esal > 12000) 
     { 
      e.Row.ForeColor = System.Drawing.Color.Blue; 
      e.Row.BackColor = System.Drawing.Color.LightPink; 
      e.Row.Font.Italic = true; 
     } 
     else if (esal == 15000) 
     { 
      e.Row.ForeColor = System.Drawing.Color.Brown; 
      e.Row.BackColor = System.Drawing.Color.LightBlue; 
      e.Row.Font.Italic = true; 
     } 
     else 
     { 
      e.Row.ForeColor = System.Drawing.Color.White; 
      e.Row.BackColor = System.Drawing.Color.LightGreen; 
      e.Row.Font.Italic = true; 
     } 
    } 
    else 
    { 
     //show message the that e.Row.Cells[3].Text.ToString() doesn't contain integer. 
    } 
} 
相關問題