Search code examples
gridviewwebformsdecimalnumber-formatting

How can I transform all "Integer-like" values to append ".0" prior to being displayed?


I have records with values in a Decimal(2,1) SQL Server field such as 7.0, 8.0, and 9.0 that, when displayed in my GridView, display instead as "7", "8", or "9"

I want them to display as "7.0", "8.0", and "9.0" so that they look like their "brethren" who are 8.3, 7.4, 9.1, etc.

How can I tell the GridView on my WebForms site to display their full value (with the decimal point and 0), even though (for example) 8 is the same thing as 8.0?

UPDATE

Here's where I get the data and assign it to the GridView:

private void GetFilteredData(string completeQuery)
{
    string connStr = ConfigurationManager.ConnectionStrings 
                ["Gr8GooglyMooglyConnectionString"].ToString();
    try
    {
        using (SqlConnection connection = new SqlConnection(connStr))
        {
            using (SqlCommand cmd = new SqlCommand(completeQuery, 
                                                   connection))
            {
                cmd.Parameters.Add(new SqlParameter("@IMDBMinRating", 
                    SqlDbType.Decimal) { Precision = 2, Scale = 1, Value = 
                    _imdbThreshold });
                cmd.Parameters.Add("@EarliestYear", SqlDbType.Char, 4).Value 
                    = _yearBegin;
                cmd.Parameters.Add("@LatestYear", SqlDbType.Char, 4).Value = 
                    _yearEnd;
                
                SqlDataAdapter dAdapter = new SqlDataAdapter(cmd);
                DataSet ds = new DataSet();
                dAdapter.Fill(ds);
                GridView1.DataSource = ds;
                GridView1.DataBind();
            }
        }  
    }
    catch (Exception ex)
    {
        string s = ex.Message;
        s = s + "Oopsa-daisy!";
    }
}

UPDATE 2

.aspx of grid

<div style="width: 100%; height: 500px; overflow: scroll">
    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"          
            OnDataBound="GridView1_DataBound" OnPreRender="GridView1_PreRender"              
            OnSelectedIndexChanged="GridView1_SelectedIndexChanged"          
            AutoGenerateSelectButton="True">
        <Columns>
            <asp:BoundField DataField="MovieId" HeaderText="Movie Id" ItemStyle- 
                Width="0%" SortExpression="MovieId" />
            <asp:BoundField DataField="MovieTitle" HeaderText="MovieTitle"       
                SortExpression="MovieTitle" />
            <asp:BoundField DataField="IMDBRating" HeaderText="IMDBRating"       
                SortExpression="IMDBRating" />
            <asp:BoundField DataField="MPAARating" HeaderText="MPAARating"       
                SortExpression="MPAARating" />
            <asp:BoundField DataField="YearReleased" HeaderText="YearReleased"       
                SortExpression="YearReleased" />
            <asp:BoundField DataField="Minutes" HeaderText="Minutes" 
                SortExpression="Minutes" />
        </Columns>
    </asp:GridView>
</div>

   

Solution

  • You can use DataFormatString property on BoundField to specify format in which data should be rendered. For example for displaying digit after number you can use F1, something like:

    <asp:BoundField DataField="IMDBRating" HeaderText="IMDBRating"       
                    SortExpression="IMDBRating" DataFormatString="{0:F1}" />