How to bound item in a grid view
This is a two part question, im working with a dataset and a Grid view. the dataset is populated by querying a database, after this some calculations are done and added to the dataset. once all the calculation are done the dataset is then bound to a grid view. which looks like this.
<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" />
<S开发者_C百科ortedDescendingCellStyle 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>
Here is what I want to be able to do:
As you can see of the columns have % values but the data set just has number. How do I display a % next to the number in the gridview?
I want to be able to add some sort of conditional statement to be able to display the text in different colours. For example, in the m%, if any of the values is less than 50% i want the text to be shown in red.
Use a TemplateField instead of a BoundField and assign a class to the content to define the colour.
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>
To format your numbers as percentages have a look at the BoundField.DataFormatString
. For example:
<asp:BoundField DataField="PERC_OF_TARGET"
HeaderText="%"
DataFormatString="{0:F0}%" />
The above format string will format the number with zero decimal places, if you need decimal places then use {0:F2}%
which will add two decimal places.
1) Check out "templatefield" http://www.asp.net/data-access/tutorials/using-templatefields-in-the-gridview-control-cs
2) See above! EDIT: Here is a detailed example how to achieve this with templatefield: How to implement conditional formatting in a GridView
Enjoy.
精彩评论