将c#代码中的文本添加到gridview标签中

时间:2012-12-01 20:56:36

标签: 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.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之外)......但我需要从这个方法中获得我需要的格式。

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"];
}

我使用此方法获取特定字符串并在标签上使用它(我将其分配给cs文件上的代码)。 但在这种情况下......我必须在gridview的列上插入该文本......

如何获取该字符串值并将其插入templatefield / itemtemplate内的标签上?

2 个答案:

答案 0 :(得分:2)

而不是......

Text='<%#Math.Round(Convert.ToDouble(Eval("Total")), 2).ToString("C") + " M.N."%>'

...使用

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

但是,这假设GetFormatoMoneda与Web表单属于同一类。如果没有,那么你需要包括类名,例如

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

然后您需要对GetFormatoMoneda进行更改以使用对象类型参数,例如:

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"];
}

或使用另一个带有object参数的方法并调用GetFormatoMoneda(十进制),传入正确的值,例如

protected string CorrectFormat(object obj)
{
    return GetFormatoMoneda(Convert.ToDecimal(obj));
}

在这种情况下你会使用

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

答案 1 :(得分:1)

如果您想以编程方式执行此操作,则可以使用:

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();
  }
}
相关问题