-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

Anas Alweish
  • 2,818
  • 4
  • 30
  • 44
Ahmad Ali
  • 31
  • 6
  • 2
    Could you re-phrase question title? As it stated, it's unclear what's your question about. – Dennis May 22 '19 at 11:06
  • What Should i Do To Make my string 15 character wide no matter what the original length is – Ahmad Ali May 22 '19 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 – Ahmad Ali May 22 '19 at 11:08
  • i Think The string.padright(15) will work – Ahmad Ali May 22 '19 at 11:14
  • I will also recommend [ask], your title is not unrealated to your issue. – xdtTransform May 22 '19 at 11:29

2 Answers2

0

try this:

myRow.Cells["gvQuantity"].Value.ToString().PadRight(15);
akg179
  • 1,287
  • 1
  • 6
  • 14
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

Steve Todd
  • 1,250
  • 6
  • 13