views:

29

answers:

2

I tried to add labels and rows to tablelayout. When i used adding to listview like that:

ListViewItem ite = new ListViewItem(tag);
ite.Text = (tag + " " + description + war);
listView2.Items.Add(ite.Text);

it work,but when i try another it doesn't work. Why? There aren't any errors or exceptions.

 foreach (DataElement elementy in sq)
 {                       
       for (int k = 0; k == row_number; k = k + 1)
        {
        tag = elementy.Tag.ToString();
        description = elementy.VR.Tag.GetDictionaryEntry().Description;



       // ListViewItem ite = new ListViewItem(tag);
        //ite.Text = (tag + " " + description + war);
        //listView2.Items.Add(ite.Text);

            tableLayoutPanel1.GrowStyle = TableLayoutPanelGrowStyle.AddRows;
            Label et_tag = new Label();
            et_tag.AutoSize= true;
            et_tag.Text = tag;
            tableLayoutPanel1.Controls.Add(et_tag, 0, k);
            Label op = new Label();
            op.AutoSize = true;
            op.Text = description;
            tableLayoutPanel1.Controls.Add(op, 1, k);

        }
 }
+1  A: 

Your inner for loop won't run unless row_number is 0. Otherwise, it will fail its condition immediately after K is initialized and, as a consequence, do nothing. Your loop almost certainly isn't running. If row_number actually is 0, your loop will run exactly once for every item in your outer foreach loop.

What condition do you want the loop to run on? Do you mean k < row_number instead? Do you just want to do this for a single k where k == row_number, in which case you should simply assign row_number to k and get rid of the loop entirely? I don't know where row_number comes from, so I don't know what this code wants to do.

Kistaro Windrider
row number is a number of tags for each element in sequence. Each element has two tags describing it. I want to show these tags in tablelayout.
luc
You want a loop bound of k < row_number (since you're starting your count at zero), but there's another problem here. Nothing involving elementy involves k. You're just going to print the same tag row_number times, because you haven't made the program look anywhere else for tag data.
Kistaro Windrider
Oh, that's right -> 'k< row_number' or 'k== row_number-1'. But i don't know what you mean about another problem. How can i repair it?
luc
I repaired my program. Thanks a lot:)
luc
It's not just the off-by-one. That == evaluates as true only for the last desired iteration of the loop; the < evaluates as true for all iterations. But a for loop stops as soon as it sees the first false, so the loop will, once again, never run at all (or run only once) with the k == row_number - 1. It's not an "end condition" for the loop, it's the "loop condition"- the condition under which the loop runs.
Kistaro Windrider
As for the second problem- which it sounds like you fixed!- what do you need to do to tell elementY which tag you're interested in? Right now, it looks like elementY only has one tag, which it will repeat again and again, row_number times.
Kistaro Windrider
+1  A: 

Not sure how you're initializing row_number, but perhaps it should be something like:

for (int k = 0; k <= row_number; k = k + 1)
micahtan
I repaired my program. Thanks a lot:)
luc