2011-06-16 61 views
0

这是一个两部分问题,即时处理数据集和网格视图。通过查询数据库填充数据集,然后完成一些计算并添加到数据集中。一旦完成所有计算,数据集就会绑定到网格视图。看起来像这样。如何在网格视图中绑定项目

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
     CellPadding="4" ForeColor="#333333" GridLines="None" Width="100%" CssClass="tableText" > 
     <AlternatingRowStyle BackColor="White" /> 
     <EditRowStyle BackColor="#2461BF" /> 
     <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" /> 
     <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" /> 
     <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" /> 
     <RowStyle BackColor="#EFF3FB" HorizontalAlign="Center" /> 
     <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" /> 
     <SortedAscendingCellStyle BackColor="#F5F7FB" /> 
     <SortedAscendingHeaderStyle BackColor="#6D95E1" /> 
     <SortedDescendingCellStyle BackColor="#E9EBEF" /> 
     <SortedDescendingHeaderStyle BackColor="#4870BE" /> 
     <Columns> 
     <asp:BoundField DataField="INV_GP" HeaderText="INV GP" /> 
     <asp:BoundField DataField="SORG_GP" HeaderText="SORD GP" /> 
     <asp:BoundField DataField="SRTN_GP" HeaderText="SRTN GP" /> 
     <asp:BoundField DataField="EXPEND" HeaderText="EXPEND" /> 
     <asp:BoundField DataField="TARGET" HeaderText="TARGET" /> 
     <asp:BoundField DataField="PERC_OF_TARGET" HeaderText="%" /> 
     <asp:BoundField DataField="M_PERC" HeaderText="M%" /> 
     <asp:BoundField DataField="100NEED" HeaderText="NEED FOR 100%" /> 
     </Columns> 
    </asp:GridView> 

这是我希望能够做到:

  1. 正如你可以看到列有%值,但该数据集只具有数量。如何在gridview中显示数字旁边的%?

  2. 我希望能够添加某种条件语句,以便能够以不同颜色显示文本。例如,在m%中,如果任何值小于50%,我希望文本显示为红色。

回答

3

使用TemplateField而不是BoundField并将内容分配给内容以定义颜色。

CSS

<style type="text/css"> 
.MoreThanFifty { color: green; } 
.FiftyOrLess { color: red; } 
</style> 

ASP.NET

<asp:TemplateField HeaderText="M%"> 
    <ItemTemplate> 
    <span class='<%# int.Parse(Eval("M_PERC").ToString()) > 50 ? "MoreThanFifty" : "FiftyOrLess" %>'> 
     <%# Eval("M_PERC") %> % 
    </span> 
    </ItemTemplate> 
</asp:TemplateField> 
+2

给这个人一条鱼,或教他钓鱼? – mikey 2011-06-16 15:02:11

+1

@mikey:取决于他是多么饿...... :) – Town 2011-06-16 15:04:16

+0

@Town我喜欢那个;) – mikey 2011-06-16 15:09:34

1

要格式化数字作为百分比看看在BoundField.DataFormatString。例如:

<asp:BoundField DataField="PERC_OF_TARGET" 
       HeaderText="%" 
       DataFormatString="{0:F0}%" /> 

上面的格式字符串将格式化数字与零位小数,如果你需要小数然后用{0:F2}%这将增加两位小数。