2017-08-11 70 views
0

我有一个Asp.net GridView,其中最后一列是我的总和col 2-x将是我的号码。显示百分比栏与背景颜色ASP.Net RowDataBound

我想显示一个栏显示的百分比

使用发现Displaying percentage bar with background color 我想修改它的工作onRowDataBound

首先我创建了一个方法来获得风格

private string colPercent(long Total, long count) 
    { 
     var y = decimal.Parse(Total.ToString())/count; 
     decimal percent =Math.Round(y,2); 

     string _return = $"height:30px; background: -webkit - linear - gradient(left, #efe3af {percent}%,#ffffff {percent}%);"; 
     _return+= $"background: -moz - linear - gradient(left, #efe3af {percent}%, #ffffff {percent}%);"; 
     _return += $"background: -ms - linear - gradient(left, #efe3af {percent}%,#ffffff {percent}%)"; 
     _return += $"background: -o - linear - gradient(left, #efe3af {percent}%,#ffffff {percent}%);"; 
     _return += $" background: linear - gradient(to right, #efe3af {percent}%,#ffffff {percent}%);"; 

     return _return; 
    } 
的例子

接下来在RowDataBind上循环列

 if (e.Row.RowType == DataControlRowType.DataRow) 
    { 

     for (int i = colstart; i < e.Row.Cells.Count; i++) 
    { 
    e.Row.Cells[i].Attributes.Add("Style", colPercent(long.Parse(e.Row.Cells [e.Row.Cells.Count -1 ].Text), long.Parse(e.Row.Cells[i].Text))); 

     } 
    } 

没有什么似乎在改变。有什么可能是错的?

回答

0

答案是先删除样式中的间距。 VS重新格式化它然后

e.Row.Cells[i].Attributes.CssStyle.Value = colPercent(long.Parse(e.Row.Cells[e.Row.Cells.Count - 1].Text), long.Parse(e.Row.Cells[i].Text));