在gridview列中格式化数据

在gridview列中格式化数据

问题描述:


我在数据库中有一个名为agent的表,该表具有自动生成的int类型的"agent id".我使用此表将数据输入到gridview中.当我在gridview中显示数据时,我想将代理ID"显示为A0001而不是1.如何使用C#或sql实现此目的?
在此先谢谢您.

Hi,
I have a table in database called agent which has an autogenerated "agent id" of type int.I use this table to get data in to a gridview. when i display data in a gridview i want to show the "agent id" as A0001 instead of 1. how can i achieve this using either c# or sql?
Thanks in advance.

您能尝试一下吗,因为我认为它可以解决您的问题
Can you try like that, as I think it may solve your problem
<columns>
 <asp:templatefield>
      <itemtemplate>
           <%#"A"+string.Format("{0:0000}", Eval("agent_ID")) >
      </itemtemplate>
 </asp:templatefield>
</columns>


如何使用c#
来实现这一点 在您的gridview中,有一个方法RowDataBound(用于Datagrids的ItemDataBound),请使用此方法.在进行数据绑定时,将为每一行调用此方法.您可以根据需要更改任何单元格的值.
how can i achieve this using either c#
In your gridview, there is a method RowDataBound (ItemDataBound for Datagrids), use this method. While databind, this method would be called for every row. You can change the value of any cell as desired.
protected void myGridview_RowDataBound(object sender, GridViewRowEventArgs e)
{
  if (e.Row.RowType == DataControlRowType.DataRow)
  {
    // Get the AgentId cell and modify the text as per desired
  }
} 




如何使用任一SQL
实现此目的 在获取数据时,必须使用查询.在查询中,从列中获取数据时,只需将必要的字符串附加到该列中即可.就像,




how can i achieve this using either SQL
While fetching the data, you must be using a query. In the query, while getting data from the column just append the necessary string to it. Like,

SELECT "A000"+ cast(agentID as nvarchar) as AgentId FROM myTable




我建议使用SQL方式进行修改,因为这将变得简单,容易.




I would suggest going with SQL way to modify it as it would be simple and easier.