Answered by:
change format in gridview field1

Question
-
User-807418713 posted
Hello
I have 10 fileds in gridview
For example field1 data shows like this
ABC-110-01-Feb
KLM-220-01-FEB
MNO-113-02-FEB
KML-170-03-FEB
NBV-170-03-FEB
UYT-60-04-FEBbut i want as below with perfect alignment easy to read
ABC - 110 -Feb-01
KLM - 220 -FEB-01
MNO - 113 -FEB-02
KML - 170 -FEB-03
NBV - 170 -FEB-03
UYT - 60 -FEB-04how to change
Thank You
Saturday, February 22, 2020 11:03 AM
Answers
-
User-719153870 posted
Hi Gopi.MCA,
For example field1 data shows like this
ABC-110-01-Feb
KLM-220-01-FEB
MNO-113-02-FEB
KML-170-03-FEB
NBV-170-03-FEB
UYT-60-04-FEBbut i want as below with perfect alignment easy to read
ABC - 110 -Feb-01
KLM - 220 -FEB-01
MNO - 113 -FEB-02
KML - 170 -FEB-03
NBV - 170 -FEB-03
UYT - 60 -FEB-04The reason why the same number of characters looks differently formatted is the characters have different pixel size:
KLM-220-01-FEB
MNO-113-02-FEBIf you just want to make them look neat and easy to read, i suggest you could use the
font-family: monospace;
font style.So, update @oned_gk‘s code:
aspx:
<html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> <style> body { font-family: monospace; } </style> </head> <body> <form id="form1" runat="server"> <div> <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false"> <Columns> <asp:TemplateField> <ItemTemplate> <%# Newformat(Eval("field1").ToString()) %> </ItemTemplate> </asp:TemplateField> </Columns> </asp:GridView> </div> </form> </body> </html>
cs:
protected void Page_Load(object sender, EventArgs e) { DataTable dt = new DataTable(); dt.Columns.AddRange(new DataColumn[1] { new DataColumn("field1") }); dt.Rows.Add("ABC-110-01-Feb"); dt.Rows.Add("KLM-220-01-FEB"); dt.Rows.Add("MNO-113-02-FEB"); dt.Rows.Add("KML-170-03-FEB"); dt.Rows.Add("NBV-170-03-FEB"); dt.Rows.Add("UYT-60-04-FEB"); GridView1.DataSource = dt; GridView1.DataBind(); } public string Newformat(string str) { string result = ""; string[] arr = str.Split('-'); if (arr.Count() == 4) { result = arr[0] + " - " + arr[1] + "-" + arr[3] + "-" + arr[2]; } else { result = "wrong format"; } return result; }
result:
Best Regard,
Yang Shen
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Monday, February 24, 2020 5:57 AM
All replies
-
User475983607 posted
how to changeUse the standard <pre> tag.
https://www.w3schools.com/tags/tag_pre.asp
I recommend learning HTML basics.
Saturday, February 22, 2020 12:25 PM -
User409696431 posted
<pre> does not do what Gopi.MCA asked for.
It simply changes
ABC-110-01-Feb
KLM-220-01-FEB
MNO-113-02-FEB
KML-170-03-FEB
NBV-170-03-FEB
UYT-60-04-FEBto
ABC-110-01-Feb KLM-220-01-FEB MNO-113-02-FEB KML-170-03-FEB NBV-170-03-FEB UYT-60-04-FEB
It changes the font to a fixed-width font and preserves spaces and linebreaks.. It does not put whitespace before and after the first hyphen (more in front of it than behind it) and add extra space before the first number to make it take up the space of 3 digits when it has less than 3 digits, and space before the second hypen, which is what the OP shows as the desired result.
There is no simple way to format the results like that.
Saturday, February 22, 2020 11:45 PM -
User-1716253493 posted
public string Newformat(string str) { string result = ""; string[] arr = str.Split('-'); if (arr.Count() == 4) { result = arr[0] + " - " + arr[1] + "-" + arr[3] + "-" + arr[2]; } else { result = "wrong format"; } return result; }
<asp:TemplateField> <ItemTemplate> <%# Newformat(Eval("field1").ToString()) %> </ItemTemplate> </asp:TemplateField>
Sunday, February 23, 2020 1:51 AM -
User-2054057000 posted
Note: This is another method which will be applicable in certain cases only. I am taking into account that you are fetching first part (ABC) from field1 of your database table and other part (110-01-Feb) from field2.
In this case use a bit of inline CSS in the binding code in your .aspx page:
<asp:TemplateField> <ItemTemplate> <span style="margin-right: 30px;"><%# Eval("field1").ToString()) %> -
</span><span style="padding-left: 20px"><%# Eval("field2").ToString()) %></span> </ItemTemplate> </asp:TemplateField>Sunday, February 23, 2020 4:30 AM -
User-719153870 posted
Hi Gopi.MCA,
For example field1 data shows like this
ABC-110-01-Feb
KLM-220-01-FEB
MNO-113-02-FEB
KML-170-03-FEB
NBV-170-03-FEB
UYT-60-04-FEBbut i want as below with perfect alignment easy to read
ABC - 110 -Feb-01
KLM - 220 -FEB-01
MNO - 113 -FEB-02
KML - 170 -FEB-03
NBV - 170 -FEB-03
UYT - 60 -FEB-04The reason why the same number of characters looks differently formatted is the characters have different pixel size:
KLM-220-01-FEB
MNO-113-02-FEBIf you just want to make them look neat and easy to read, i suggest you could use the
font-family: monospace;
font style.So, update @oned_gk‘s code:
aspx:
<html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> <style> body { font-family: monospace; } </style> </head> <body> <form id="form1" runat="server"> <div> <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false"> <Columns> <asp:TemplateField> <ItemTemplate> <%# Newformat(Eval("field1").ToString()) %> </ItemTemplate> </asp:TemplateField> </Columns> </asp:GridView> </div> </form> </body> </html>
cs:
protected void Page_Load(object sender, EventArgs e) { DataTable dt = new DataTable(); dt.Columns.AddRange(new DataColumn[1] { new DataColumn("field1") }); dt.Rows.Add("ABC-110-01-Feb"); dt.Rows.Add("KLM-220-01-FEB"); dt.Rows.Add("MNO-113-02-FEB"); dt.Rows.Add("KML-170-03-FEB"); dt.Rows.Add("NBV-170-03-FEB"); dt.Rows.Add("UYT-60-04-FEB"); GridView1.DataSource = dt; GridView1.DataBind(); } public string Newformat(string str) { string result = ""; string[] arr = str.Split('-'); if (arr.Count() == 4) { result = arr[0] + " - " + arr[1] + "-" + arr[3] + "-" + arr[2]; } else { result = "wrong format"; } return result; }
result:
Best Regard,
Yang Shen
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Monday, February 24, 2020 5:57 AM -
User-807418713 posted
Hello Yang Shen
Please Check This Error
Compilation Error
Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.
Compiler Error Message: CS0117: 'System.Array' does not contain a definition for 'Count'
Source Error:
Line 74: string result = ""; Line 75: string[] arr = str.Split('-'); Line 76: if (arr.Count() == 4) Line 77: { Line 78: result = arr[0] + " - " + arr[1] + "-" + arr[3] + "-" + arr[2];
Thanking You
Monday, February 24, 2020 8:27 AM -
User-1716253493 posted
if (arr.Length == 4)
Monday, February 24, 2020 9:00 AM -
User409696431 posted
Some additional notes.
<style> body { font-family: monospace; } </style>
You probably don't want the whole page to be monospace font. Apply the style where you want it. (The whole gridview? Just a specific column?)
Also if you want the second part to be aligned even if the number is not 3 digits, as you showed in your first post, you could use the string function PadLeft(3), which will make it 3 characters long, padded with space(s) on the left if needed.
{ result = arr[0] + " - " + arr[1] + "-" + arr[3] + "-" + arr[2]; would become { result = arr[0] + " - " + arr[1].PadLeft(3) + "-" + arr[3] + "-" + arr[2];
Monday, February 24, 2020 10:12 AM -
User-807418713 posted
Hello
If my data is like this below then its showing error
DataTable dt = new DataTable(); dt.Columns.AddRange(new DataColumn[1] { new DataColumn("field1") }); dt.Rows.Add("ABCRA-110-01-Feb"); dt.Rows.Add("KLM-220-01-FEB"); dt.Rows.Add("MEEO-113-02-FEB"); dt.Rows.Add("KMHREFERL-170-03-FEB"); dt.Rows.Add("V-170-03-FEB"); dt.Rows.Add("UVCDST-60-04-FEB"); GridView1.DataSource = dt; GridView1.DataBind();
Tuesday, February 25, 2020 7:58 AM -
User409696431 posted
What error?
(And, of course, with different numbers of letters in the first section, it will not line up as your first post indicated. Your first post had all the same number of letters in the first section.)
Tuesday, February 25, 2020 8:43 AM -
User-719153870 posted
Hi Gopi.MCA,
The solution provided by the community is for the situation you raised earlier and seems the data changes now.
There must be a clear description of your requirement, in this case, please tell the MAX length of each part of your record(XXXXXX-XXX-XX-XXX) so that we can make it look with perfect alignment easy to read.
Anyway, the idea is to fill short strings with spaces so that all strings in the corresponding position have the same length.
Best Regard,
Yang Shen
Tuesday, February 25, 2020 9:31 AM