添加从C#code文本到GridView标签 [英] Add text from c# code to a gridview label
问题描述
我需要在一个ItemTemplate上一个GridView添加特定文本...
I need to add an specific text in an itemtemplate on a gridview...
现在我有这在我的GridView控件
right now I have this in my gridview
<asp:TemplateField HeaderText="Total" SortExpression="Total" ItemStyle-Width="100px">
<ItemTemplate>
<asp:Label ID="lblTotal" runat="server" Text='<%#Math.Round(Convert.ToDouble(Eval("Total")), 2).ToString("C") + " M.N."%>'>
</asp:Label>
</ItemTemplate>
</asp:TemplateField>
在那里说的部分。
<asp:Label ID="lblTotal" runat="server" Text='<%#Math.Round(Convert.ToDouble(Eval("Total")), 2).ToString("C") + " M.N."%>'>
我做出了具体的文本,但它永远是相同的文本(以及除当然EVAL)...但我需要得到我从这个方法需要的格式。
I made an specific text, but it will always be the same text (well except in the Eval of course)... But I need to get the format I need from this method.
public static string GetFormatoMoneda(decimal decCantidad)
{
//Get data from currency (Dollars, Pesos, Euros, etc.)
DataRow dr = ConexionBD.GetInstanciaConexionBD().GetTipoDeMonedaPrincipal((int)HttpContext.Current.Session["Grupo"]);
return dr["Signo"] + Math.Round(decCantidad, 2).ToString("C").Substring(1) + " " + dr["Abreviatura"];
}
我用这个方法来得到一个特定的字符串,并把它用在标签(我为它分配在code在CS文件)..
但在这种情况下,我还上插入一个GridView的列...
I use this method to get a specific string and use it on labels (I assign it on code on the cs file).. But in this case... I have to insert that text on the column of a gridview...
我怎样才能得到字符串值,并将其插入标签上一个TemplateField内/ ItemTemplate中??
How can I get that string value and insert it on a label inside of a templatefield/itemtemplate??
推荐答案
而不是...
Text='<%#Math.Round(Convert.ToDouble(Eval("Total")), 2).ToString("C") + " M.N."%>'
...使用
Text='<%#GetFormatoMoneda(Eval("Total"))%>'
不过,这种假设是GetFormatoMoneda在同一类的网页表单。如果没有,那么你需要包括类的名称,例如
However, this assumes that GetFormatoMoneda is in the same class as the web form. If not, then you need to include the class name, e.g.
Text='<%#MyClass.GetFormatoMoneda(Eval("Total"))%>'
然后,你要么需要作出改变,以GetFormatoMoneda使用对象类型参数,例如
Then you either need to make a change to GetFormatoMoneda to use an object type parameter, e.g.
public static string GetFormatoMoneda(object objCantidad)
{
var decCantidad = Convert.ToDecimal(decCantidad);
//Get data from currency (Dollars, Pesos, Euros, etc.)
DataRow dr = ConexionBD.GetInstanciaConexionBD().GetTipoDeMonedaPrincipal((int)HttpContext.Current.Session["Grupo"]);
return dr["Signo"] + Math.Round(decCantidad, 2).ToString("C").Substring(1) + " " + dr["Abreviatura"];
}
或使用其他方法与对象的参数,并调用GetFormatoMoneda(十进制),传递正确的值,如
or use another method with an object parameter and call GetFormatoMoneda(decimal), passing in the correct value, such as
protected string CorrectFormat(object obj)
{
return GetFormatoMoneda(Convert.ToDecimal(obj));
}
在这种情况下,你可以使用
in which case you would use
Text='<%#CorrectFormat(Eval("Total"))%>'
这篇关于添加从C#code文本到GridView标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!