Click here to Skip to main content
15,899,937 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
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.
Posted

Can you try like that, as I think it may solve your problem
XML
<columns>
 <asp:templatefield>
      <itemtemplate>
           <%#"A"+string.Format("{0:0000}", Eval("agent_ID")) >
      </itemtemplate>
 </asp:templatefield>
</columns>
 
Share this answer
 
v2
Comments
Ankur\m/ 22-Apr-11 0:31am    
That should help, 5!
CodeHawkz 22-Apr-11 1:15am    
That's indeed the correct answer. But just on a side note, this line below is not recommended practice.
<%#"A"+string.Format("{0:0000}", Eval("agent_ID")) >

You should change this to,
<%#string.Format("A{0:0000}", Eval("agent_ID")) >

The reason is that, string.Format uses string buffers internally to create the required string. But the use of "+" in string concatenation creates a memory problem (I know it's not significant here). Just as a best practice :)
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.
C#
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
  }
} 



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,
SQL
SELECT "A000"+ cast(agentID as nvarchar) as AgentId FROM myTable



I would suggest going with SQL way to modify it as it would be simple and easier.
 
Share this answer
 
Comments
CodeHawkz 22-Apr-11 1:17am    
This would work as long as the AgentId is a single digit number. But this will not work beyond that. For instance, your solution will generate "A0001" for AgentID = 1 and "A00010" for AgentID = 10 where as the expected result is "A0010". One less zero :) I think you should revise your solution.
Sandeep Mewara 22-Apr-11 1:53am    
And why so? OP did not said that it has to be 5 characters always!

Based on the question, I think the solution is fine and needs no change.
CodeHawkz 22-Apr-11 2:28am    
Common sense :) You wouldn't create an ID which grows the number right? If you wanted it to grow, you wouldn't add the preceding 0's right? Would you?

In other words, you would keep it like "A1", "A2", "A234" or you would keep it like "A001", "A002", "A999". Does this sound sane to you? >> "A001", "A0010", "A00100". It would be perfectly allright, if the the "A00" part was something like "ATX" like a prefix :)

Hope I made some sense :)
Sandeep Mewara 22-Apr-11 2:31am    
Common sense :) You wouldn't create an ID which grows the number right? If you wanted it to grow, you wouldn't add the preceding 0's right? Would you?
I disagree. I have seen requirements like having a fixed string always attached.

I too can contradict your logic by saying what if the number is more than 4 digits - say 99999? Common sense? :)
CodeHawkz 22-Apr-11 2:36am    
Requirements? I have too. :lol: But look at this instance, not other instances. This requirement :)

If the fixed string is not preferred you do not add the preceding 0's. He meaning to use the 3 digit fixed length number along with "A" prefix means, he needs one. Isn't it? Any idea or experience of requirements which contradicts that one?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900