0

I have ID as 10 digits int ex: 1023456789 and I need to set DataGridView format to have there "102-34567 add. 89".

string.Format("###-##### add. ##") doesn't work. So how to do that?

3
  • Is the original data actually a number or is it a string? Commented Feb 13, 2023 at 11:00
  • String.Format works. It's job is to to format a non-string value as a string. It doesn't work with string inputs, nor is it meant to. What did you actually try? Commented Feb 13, 2023 at 11:02
  • 2
    Assuming it actually is a number, you simply assign the format specifier to the DefaultCellStyle.Format property of the column. You don't call String.Format yourself. The grid calls ToString on the data and passes the format specifier that you provided. Commented Feb 13, 2023 at 11:04

2 Answers 2

4
var result = 1023456789.ToString("###-##### add\\. ##");
Sign up to request clarification or add additional context in comments.

2 Comments

This is for a DataGridView though, so there should not be any String.Format or ToString call at all.
You could do it in the CellFormatting event if you wanted to. In any case the format string would be the same.
0

If the ID length is always 10 characters,you can use String.Insert(Int32, String)

 string s = "1023456789";
 string resultt = s.Insert(3, "_").Insert(9, " add. "); //"102-34567 add. 89"

1 Comment

This won't work if the input is actually a 10-digit int. It won't work with a DataGridView at all

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.