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.
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:
Helping you always. Don't forget to click "Mark as Answer" on the post that helped you.
♠ ASP.NET Core Tutorials → Start from the Beginning and become an Expert in 30 days time ♠
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];
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];
(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.)
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.
Member
309 Points
715 Posts
change format in gridview field1
Feb 22, 2020 11:03 AM|Gopi.MCA|LINK
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-FEB
but 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-04
how to change
Thank You
All-Star
53661 Points
24020 Posts
Re: change format in gridview field1
Feb 22, 2020 12:25 PM|mgebhard|LINK
Use the standard <pre> tag.
https://www.w3schools.com/tags/tag_pre.asp
I recommend learning HTML basics.
Contributor
6051 Points
2514 Posts
Re: change format in gridview field1
Feb 22, 2020 11:45 PM|KathyW|LINK
<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-FEB
to
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.
All-Star
52813 Points
15768 Posts
Re: change format in gridview field1
Feb 23, 2020 01:51 AM|oned_gk|LINK
Suwandi - Non Graduate Programmer
Participant
1253 Points
943 Posts
Re: change format in gridview field1
Feb 23, 2020 04:30 AM|yogyogi|LINK
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.NET Core Tutorials → Start from the Beginning and become an Expert in 30 days time ♠
Contributor
3140 Points
983 Posts
Re: change format in gridview field1
Feb 24, 2020 05:57 AM|Yang Shen|LINK
Hi Gopi.MCA,
The reason why the same number of characters looks differently formatted is the characters have different pixel size:
KLM-220-01-FEB
MNO-113-02-FEB
If 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:
cs:
result:
Best Regard,
Yang Shen
Member
309 Points
715 Posts
Re: change format in gridview field1
Feb 24, 2020 08:27 AM|Gopi.MCA|LINK
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
All-Star
52813 Points
15768 Posts
Re: change format in gridview field1
Feb 24, 2020 09:00 AM|oned_gk|LINK
Suwandi - Non Graduate Programmer
Contributor
6051 Points
2514 Posts
Re: change format in gridview field1
Feb 24, 2020 10:12 AM|KathyW|LINK
Some additional notes.
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.
Member
309 Points
715 Posts
Re: change format in gridview field1
Feb 25, 2020 07:58 AM|Gopi.MCA|LINK
Hello
If my data is like this below then its showing error
Contributor
6051 Points
2514 Posts
Re: change format in gridview field1
Feb 25, 2020 08:43 AM|KathyW|LINK
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.)
Contributor
3140 Points
983 Posts
Re: change format in gridview field1
Feb 25, 2020 09:31 AM|Yang Shen|LINK
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