问题
I need to add an specific text in an itemtemplate on a 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>
in the part where it says
<asp:Label ID="lblTotal" runat="server" Text='<%#Math.Round(Convert.ToDouble(Eval("Total")), 2).ToString("C") + " M.N."%>'>
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"];
}
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...
How can I get that string value and insert it on a label inside of a templatefield/itemtemplate??
回答1:
Instead of ...
Text='<%#Math.Round(Convert.ToDouble(Eval("Total")), 2).ToString("C") + " M.N."%>'
...use
Text='<%#GetFormatoMoneda(Eval("Total"))%>'
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"))%>'
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"];
}
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"))%>'
回答2:
If you wanted to do it programmatically, this would work:
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>
Default.aspx.cs:
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();
}
}
来源:https://stackoverflow.com/questions/13663668/add-text-from-c-sharp-code-to-a-gridview-label