-1

I have some code and string i want the string to be fixed length of 15 if string length is less than 15 than i want to add spacing to make my string 15 character wide for printing purpose

i have tried

(string.format("{xxxxxxxxxxxxxxx}",myRow.Cells["PREMIER REG"].Value.ToString())

Input string was not in correct format exception thrown

     myGraphic.DrawString("S# | DESCRIPTION                | QTY | PRICE 
             | SUBTOTAL", myFont, Mybrash, xMargin, ySpacing);
        ySpacing += 7;
        myGraphic.DrawLine(myPen, xMargin, yMargin, 50, 0);
        ySpacing += yMargin;         
        foreach (DataGridViewRow myRow in dataGridViewBill.Rows)
        {
            int i = 1;             
            myGraphic.DrawString(
                i+ "" + 
                myRow.Cells["gvProductTitle"].Value.ToString() + 
                myRow.Cells["gvQuantity"].Value + 
                myRow.Cells["gvPrice"].Value  +
                myRow.Cells["gvTotal"].Value
                , myFont, Mybrash, xMargin, ySpacing);
            ySpacing += yMargin;
        }

The problem in printing gvquantity value is being to pulled to product column

5
  • 2
    Could you re-phrase question title? As it stated, it's unclear what's your question about. Commented May 22, 2019 at 11:06
  • What Should i Do To Make my string 15 character wide no matter what the original length is Commented May 22, 2019 at 11:07
  • i want to make this string 15 character myRow.Cells["gvProductTitle"].Value.ToString() no matter how much character in it if character length is greater than fifteen then must be trim to 15 or if less then 15 then the space should be added Commented May 22, 2019 at 11:08
  • i Think The string.padright(15) will work Commented May 22, 2019 at 11:14
  • I will also recommend How to Ask, your title is not unrealated to your issue. Commented May 22, 2019 at 11:29

2 Answers 2

0

try this:

myRow.Cells["gvQuantity"].Value.ToString().PadRight(15);
Sign up to request clarification or add additional context in comments.

1 Comment

Both Way worked well but we should care about font style also we should use mono space like font
0

String formatting allows you to specify the width (and whether to left or right align) by putting a comma after the format specifier and then the field width like:

string.format("{0,15}", myRow.Cells["PREMIER REG"].Value.ToString());

See https://learn.microsoft.com/en-us/dotnet/api/system.string.format?view=netframework-4.8#controlling-formatting

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.