简体   繁体   中英

ASP.NET how to add text to label in gridview using a class in C# code?

I need to know how can I use a class to get a string and add it to a label inside an itemtemplate in agridview...

I previously posted this question: Add text from c# code to a gridview label

I got an answer where I noticed I get the string from a class in a cs file... to be more specific, here's my class:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace ClientesPagos
{
    public class Funciones
    {
        public static string GetFormatoMoneda(decimal decCantidad)
        {
            DataRow dr = ConexionBD.GetInstanciaConexionBD().GetTipoDeMonedaPrincipal((int)HttpContext.Current.Session["Grupo"]);
            return dr["Signo"] + Math.Round(decCantidad, 2).ToString("C").Substring(1) + " " + dr["Abreviatura"];
        }
    }
}

from one of the suggestions, I tried to use this:

Text='<%#Funciones.GetFormatoMoneda(Eval("Total"))%>'

Did not work...

then I tried something that I don't want to do, but just for testing I tried it. My gridview is in a file called Ventas.aspx... so I added that same class on Ventas.aspx.cs and then I switch the text to:

Text='<%#GetFormatoMoneda(Eval("Total"))%>'

also, I tried switching in GetFormatoMoneda(decimal decCantidad) to GetFormatoMoneda(object objCantidad), with no success at all...

Do you know a way to fix this? or if you could provide a different answer on the other question on the link above?

您可以使用gridview.rowdatabound事件在后面的代码中操作网格行。

It should be:

    Text='<%# Eval(Funciones.GetFormatoMoneda(1.0))%>'

Just replace the 1.0 I have written inside the function call GetFormatoMoneda .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM