How can I display numbers in grid view footer and ItemTemplate using the format "###,###.000"?

How can I display numbers in the format "###,###.000" within a grid view footer and ItemTemplate?

Footer in HTML :


       <FooterTemplate>
        <asp:Label ID="totallblCredAmount" runat="server" />
     </FooterTemplate> 

Footer in code :


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

       Label totallblCAmount = (Label)e.Row.FindControl("totallblDebAmount");
       totallblCAmount.Text = ViewState["TotalPric"].ToString();

       Label totallblCredAmount = (Label)e.Row.FindControl("totallblCredAmount");
       totallblCredAmount.Text = ViewState["TotalPrice"].ToString();

   }

ItemTemplate HTML :


 <asp:TemplateField HeaderText="credit">
            <ItemTemplate>
                 <asp:Label  runat="server" Text='<%#(Eval("credit"))%>'></asp:Label>
            </ItemTemplate>  
        </asp:TemplateField>

Answer №1

To properly format numeric values, you need to ensure you correctly handle conversion between string, decimal, and double data types. Storing values in ViewState requires careful casting and formatting.

Below is an example code snippet for handling numeric values in a DataControlRowType.Footer:

if (e.Row.RowType == DataControlRowType.Footer)
{
   Label totallblCAmount = (Label)e.Row.FindControl("totallblDebAmount");
   decimal pricValue = (decimal)ViewState["TotalPric"];
   totallblCAmount.Text = pricValue.ToString("###,###.000");

   Label totallblCredAmount = (Label)e.Row.FindControl("totallblCredAmount");
   decimal priceValue = (decimal)ViewState["TotalPrice"];
   totallblCAmount.Text = priceValue.ToString("###,###.000");
}

For parsing string values stored in ViewState as decimals:

string stringPriceValue = (string)ViewState["TotalPrice"];
decimal priceValue = decimal.Parse(stringPriceValue);
totallblCAmount.Text = priceValue.ToString("###,###.000");

It's advised to avoid storing decimal-number values as strings due to potential localization issues and the need for constant parsing.


To format labels in the ItemTemplate of your ASPX page:

<asp:Label  runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "credit", "{0:###,###.000}") %>' ID="LblCredit"></asp:Label>

If you prefer code-behind implementation using RowDataBound:

protected void GrdidView_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        Label lblCredit = (Label) e.Row.FindControl("LblCredit");
        DataRow row = ((DataRowView) e.Row.DataItem).Row;
        double credit = row.Field<double>("credit");
        lblCredit.Text = credit.ToString("###,###.000");
    }
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Develop a standard property within an interface

I am looking to create an interface that includes a method called GetId(). The challenge I'm facing is that the return type for GetId() needs to be flexible based on the subitems, which could be an int, string, or any other data type. I initially trie ...

Making an asynchronous call from Index.html to PHP Script

I am currently working on implementing an AJAX call to my PHP Script. While I can successfully echo the results from my data.php file, I am now facing a challenge regarding how to initiate the call from index.html in order to retrieve the table results s ...

Using a jQuery hover effect to slide a span element

I have designed custom share buttons to enhance the appearance of the traditional like, tweet, pin, etc. However, I am facing an issue with hover functionality in a jsfiddle demo. The buttons almost function as intended, but I need the span element to rema ...

Make the height of the next/image element 100vh

It's really amazing. I've tried everything to make the image component in nextjs 100vh in height, with automatic width. I've read through all the documentation and examples using the layout property, but nothing seems to work. Goal: 100vh h ...

Tips for generating a WhatsApp share link for a specific webpage

Is there a way to generate a share link through WhatsApp using the current URL? Hello, I have a website with multiple social media share buttons like Facebook, Twitter, Pinterest, and email. However, I am looking to replace Pinterest with WhatsApp and Ins ...

Is it possible to create and access a PDF file using file handling in PHP?

In my PHP code, I am utilizing file handling to write and then read a PDF file. However, after successfully creating the PDF file, I encounter an issue where I am unable to open it. When attempting to open the file, Adobe Reader displays an error message s ...

Update panel triggering complete postback due to user control custom events

I've come across this question before, but none of the solutions seem to be effective in my case. Here is the section for my UpdatePanel (inside hi.ascx): <asp:UpdatePanel runat="server" ID="upUL" UpdateMode="Conditional" > <ContentTemplate& ...

Steps for implementing a class with dependency injection without constructor parameters

I have implemented dependency injections in my project. However, I am facing an issue where the dependency injection does not work when creating an instance using the new keyword. public class MyClass { ILoginTokenKeyApi _loginTokenKeyApi; public ...

Using Jinja2 to iterate through a dictionary while having the ability to choose which key-value pair to access

I'm attempting to generate an HTML table from data received on the web as a dictionary app.py: client = boto3.client('ec2') vpc_ids = client.describe_vpcs() for i in vpc_ids.get('Vpcs'): for tag in i.get('Tags'): ...

Mastering ReactJS: Error Encountered - Unexpected import Token

Just getting started with ReactJS and trying out some code from egghead.io. Unfortunately, I keep running into this error: Uncaught SyntaxError: Unexpected token import I've tried loading babel again and making sure to follow the lesson step by step ...

Having trouble removing data from the database using Laravel 6.0, as the destroy function seems to be ineffective

I'm struggling to remove an event that I previously created. When attempting to use the destroy() function, it seems ineffective. I suspect the issue may lie within the route in the view. <!--Displayed below is the code snippet from the view conta ...

Struggling with aligning a div in the center

I've been struggling to get my "boxes" div centered on my page so that everything inside that div is centered instead of being pushed to the left. Unfortunately, my CSS vision didn't turn out as expected. If someone could take a look at my jsbi ...

Switch Up Your Page Title Instantly Using an Anchor Tag Click

I am looking to use Javascript to change the title of a page when a specific anchor tag is clicked. <a href="index.html" title="Home">Home</a> <a href="about.html" title="About Us">About Us</a> Is it possible to achieve this by ad ...

Tips for passing a timestamp to a Postgresql DB using a Button in a Form instead of a traditional rails time_select box

I am currently developing a CAD App in Rails 4 with Ruby 2. The goal is to provide users with a button on the screen to log an on-scene time and clear scene time, especially since most users will be using touch screen computers. Instead of using the defaul ...

Guide to updating HTML table data using php and Javascript

Currently, my webpage fetches data from a MySQL database and displays it in an HTML table as soon as the page loads. Now I want to provide users with more advanced filtering options, such as filtering by name, age, etc. When a user submits their filterin ...

Twitter Bootstrap column overflowing the container boundaries

Hey there, I'm encountering a couple of small issues. First, the col class is extending beyond the container area = https://i.sstatic.net/bvEAv.jpg Additionally, I need to add space between the two divs = https://i.sstatic.net/KWFAB.jpg When I try a ...

Bootstrap is not supporting the use of justify-content-between in cards

Incorporating Bootstrap 4, I am designing a horizontal card layout for a classified ad. My goal is to have the ad title on the left and the price on the right within the header section using a Django template: <section class="card"> ...

Linking a watcher property to control the opacity value of inline styles

Struggling to connect the opacity of a div with the value of a slider. <div class="container" v-bind:style="opacity">test content</div> Despite my efforts, I can't seem to make the binding work correctly. When I check in the developer to ...

Achieving perfect alignment of two elements using flexbox: centering one and aligning the other to the right

Can you help me with aligning two elements to the center and right edge using flex? I am trying to achieve a layout similar to the image. XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX https://i.sstatic.net/cvmHX.png CSS Styles .flex{ display: flex; justify- ...

PHP: The submit button is not successfully capturing user input

Here is a snapshot of my database: link link <?php $con=mysqli_connect("localhost","root","","organisation"); // Check connection if (mysqli_connect_errno()) { echo "Failed to connect to MySQL: " . mysqli_connect_error(); } $result = mysqli_query($c ...