C# 将c代码中的文本添加到gridview标签
我需要在gridview上的itemtemplate中添加特定文本 现在我的gridview中有这个C# 将c代码中的文本添加到gridview标签,c#,asp.net,gridview,label,itemtemplate,C#,Asp.net,Gridview,Label,Itemtemplate,我需要在gridview上的itemtemplate中添加特定文本 现在我的gridview中有这个 <asp:TemplateField HeaderText="Total" SortExpression="Total" ItemStyle-Width="100px"> <ItemTemplate> <asp:Label ID="lblTotal" runat="server" Text='<%#Math.Round(Convert.
<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>
我使用此方法获取特定字符串并在标签上使用它(我在cs文件的代码上分配它)。。
但在这种情况下。。。我必须在gridview的列中插入该文本
如何获取该字符串值并将其插入到templatefield/itemtemplate中的标签上???而不是
Text='<%#Math.Round(Convert.ToDouble(Eval("Total")), 2).ToString("C") + " M.N."%>'
或者使用另一个带有对象参数的方法并调用GetFormatoMoneda(decimal),传入正确的值,例如
protected string CorrectFormat(object obj)
{
return GetFormatoMoneda(Convert.ToDecimal(obj));
}
在这种情况下,您将使用
Text='<%#CorrectFormat(Eval("Total"))%>'
Text=''
如果您希望以编程方式执行此操作,则可以使用以下方法:
Default.aspx:
<asp:GridView ID="gvGrid" runat="server" AutoGenerateColumns="false" OnRowDataBound="gvGrid_RowDataBound">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:Label ID="lblTotal" runat="server" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Text=''有效吗?
Text='<%#MyClass.GetFormatoMoneda(Eval("Total"))%>'
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"];
}
protected string CorrectFormat(object obj)
{
return GetFormatoMoneda(Convert.ToDecimal(obj));
}
Text='<%#CorrectFormat(Eval("Total"))%>'
<asp:GridView ID="gvGrid" runat="server" AutoGenerateColumns="false" OnRowDataBound="gvGrid_RowDataBound">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:Label ID="lblTotal" runat="server" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
//Generate fake data
var data = Enumerable.Range(1, 20);
//Give the data to the grid
gvGrid.DataSource = data;
gvGrid.DataBind();
}
}
protected void gvGrid_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
//Find the control
var lblTotal = (Label)e.Row.FindControl("lblTotal");
//Get the data for this row
var data = (int)e.Row.DataItem;
//Display the data
lblTotal.Text = data.ToString();
}
}