Kani
Kani

Reputation: 47

How to add new line in c#?

I have a code to create dynamic labels in c#. I need to add a newline after every labels.

 MySqlDataReader dr = cmd.ExecuteReader();  
 for (int i = 0;dr.Read(); i++)
 {
     Label NewLabel = new Label();
     NewLabel.ID = dr.GetString(0);
     NewLabel.Text = dr.GetString(1);
     this.pnlInfo.Controls.Add(NewLabel);

 }

How to add?

Upvotes: 0

Views: 1394

Answers (4)

fubo
fubo

Reputation: 46005

MySqlDataReader dr = cmd.ExecuteReader();
for (int i = 0; dr.Read(); i++)
{
    Label NewLabel = new Label();
    NewLabel.ID = dr.GetString(0);
    NewLabel.Text = dr.GetString(1);
    this.pnlInfo.Controls.Add(NewLabel);
    this.pnlInfo.Controls.Add(new Literal() { Text = System.Environment.NewLine }); //here
}

Upvotes: 2

shree.pat18
shree.pat18

Reputation: 21757

You can add an HTML line break to your panel after you add the label, like so:

this.pnlInfo.Controls.Add(new LiteralControl("<br>"));

This will actually add a line break after your label on the page.

Upvotes: 3

Tushar Gupta
Tushar Gupta

Reputation: 15933

You can Use Environment.NewLine

Upvotes: 2

Micha&#235;l Hompus
Micha&#235;l Hompus

Reputation: 3489

NewLabel.Text = dr.GetString(1) + Environment.NewLine;

Upvotes: 5

Related Questions