-1

I want to output the contents of my GridView to a TextBox and each row gets its own line. This is what I have right now but it only outputs the first row into the TextBox

for (int i = 0; i < POSDGV.Rows.Count; ++i)
{
    string Item = POSDGV.Rows[i].Cells[0].Value.ToString();
    string Amount = POSDGV.Rows[i].Cells[1].Value.ToString();
    string Receipt = Item + " " + Amount;
    Receipttxt.Text = Receipt + "\n";
}

Thx for the help

Lucifer
  • 1,594
  • 2
  • 18
  • 32
  • can you try `i++` – Lucifer Aug 20 '20 at 08:53
  • 1
    still doesn't work, i++ and ++i are the same thing – Vikil Chandrapati Aug 20 '20 at 08:56
  • 1
    you need to do `Receipttxt.Text += Receipt + "\n";` – Lucifer Aug 20 '20 at 08:59
  • 2
    `i++ and ++i are the same thing`...that depends on the context. – ADyson Aug 20 '20 at 08:59
  • Normally a textbox in a webpage is not multiline by default. Did you set yours to be multiline with `TextMode="MultiLine"` in the markup? (I'm assuming you're talking about ASP.NET Webforms here, because of the mention of GridView. But if you actually mean WinForms and DataGridView, it might be different. You really need to clarify - there are tags you can add to your post for the relevant technologies). – ADyson Aug 20 '20 at 09:02
  • Does this answer your question? [How to add a line to a multiline TextBox?](https://stackoverflow.com/questions/8536958/how-to-add-a-line-to-a-multiline-textbox) – Drag and Drop Aug 20 '20 at 09:07
  • I found the issue apparenlty it works when i do ```Environment.Newline``` but not ```/n``` – Vikil Chandrapati Aug 20 '20 at 09:07
  • That's because in Windows a newline is triggered by `\r\n` (carriage return + line feed) not just `\n` (line feed). https://en.wikipedia.org/wiki/Newline#Representation shows you the newline characters required in various operating systems. `Environment.NewLine` is a constant provided by .NET so that you don't need to know this for yourself and can just let the framework handle it depending on the O/S it's running on. – ADyson Aug 20 '20 at 09:14
  • 2
    __Do not__ call a `DataGridView`a `GridView` or a `DataGrid` and vice versa!! This is wrong and confusing as those are different controls. Always call things by their __right__ name! – TaW Aug 20 '20 at 09:40

1 Answers1

1

Depends on the technology, but for Windows.Forms there is the Multiline attribute that you can use: https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.textbox.multiline?view=netcore-3.1

Check this answer here How to add a line to a multiline TextBox? for an example. Notable mention the eventing method:

public static class WinFormsExtensions
{
   public static void AppendLine(this TextBox source, string value)
   {
      if (source.Text.Length==0)
         source.Text = value;
      else
         source.AppendText("\r\n"+value);
   }
}
Athanasios Kataras
  • 25,191
  • 4
  • 32
  • 61