ItaChi
ItaChi

Reputation: 27

c# - best way to iterate through all rows in a datarepeater?

private void exam_Load(object sender, EventArgs e)
    {
        GetCount();

        MySqlConnection con = new MySqlConnection("server = localhost; user id = root; password =; database = dbtest1;");
        MySqlCommand cmd = new MySqlCommand("SELECT question, question_no, choice1, choice2, choice3, choice4 from quiz_tions where quiz_id = '" + lid + "' ORDER BY RAND() LIMIT " + count + ";", con);
        MySqlDataAdapter sda = new MySqlDataAdapter(cmd);
        DataTable dt = new DataTable();
        sda.Fill(dt);

        label5.DataBindings.Add("Text", dt, "question");
        label6.DataBindings.Add("Text", dt, "question_no");
        rb1.DataBindings.Add("Text", dt, "choice1");
        rb2.DataBindings.Add("Text", dt, "choice2");
        rb3.DataBindings.Add("Text", dt, "choice3");
        rb4.DataBindings.Add("Text", dt, "choice4");
        dataRepeater1.DataSource = dt;

        LoadData();


        label1.Text = qtitle;
        label3.Text = qtype;
    }

I have a DataRepeater control on my form which is populated using the code above. Every time this code is executed . . .

private void button3_Click(object sender, EventArgs e)
    {
        foreach (DataRepeaterItem c in dataRepeater1.Controls)
        {
            ss += ((Label)c.Controls["label5"]).Text + "\n";
        }
        MessageBox.Show(ss);
        ss = "";
    }

It gives me different results when I click different rows before clicking the button (sometimes 3 lines, sometimes 4 lines) and it's not always correct cause I have 5 rows on my DataRepeater control when I execute that. Why is this happening? What's the proper way to iterate through the rows of DataRepeater?

P.S. Another problem (maybe) unrelated to my post is that whenever I scroll down/up on the DataRepeater, sometimes it auto-checks a random RadioButton in the list. What's wrong with this control?

Upvotes: 3

Views: 615

Answers (1)

M. Wiśnicki
M. Wiśnicki

Reputation: 6203

If You using TextBox multiline you should use \r\n instead \n

Windows text boxes need CRLF as line terminators, not just LF.

You can use \r\n

 ss += ((Label)c.Controls["label5"]).Text + "\r\n";

or System.Environment.NewLine

ss += ((Label)c.Controls["label5"]).Text  + System.Environment.NewLine;

or StringBuilder

 var sb = new StringBuiler();
 foreach (DataRepeaterItem c in dataRepeater1.Controls)
 {
    sb.AppendLine(((Label)c.Controls["label5"]).Text);
 }

 ss+= sb;

Upvotes: 1

Related Questions