Click here to Skip to main content
15,900,378 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
here is my code(PART OF CODE)

SqlCommand cmd = new SqlCommand("SELECT * FROM SEATING", cs);
SqlDataReader reader = cmd.ExecuteReader();
foreach (var c in groupBox1.Controls)
{

reader.Read();
CheckBox temp = (CheckBox)c;
int seat = reader.GetInt32(0);
int status = reader.GetInt32(2);

if (status == 1 && seat == Convert.ToInt32(temp.AccessibleName))
{
temp.Checked = true;
temp.Enabled = false;
}


}

this does the work, i.e marks the checkboxes as checked if its status is 1, and disables them, but the order in which they get disabled is reverse( i.e from end to first)
the problem is because the foreach loop , it start iterating from the end
can u help me make it iterate from start?

What I have tried:

Well i read about something called LINQ but didnt understood it
Posted
Updated 6-Jun-17 1:52am
Comments
F-ES Sitecore 6-Jun-17 7:35am    
The foreach isn't iterating from the end. What is more likely is that your "select *" is retrieving records in a different order than the groupBox1 is bound to as your code implies they have the same ordering but we don't know how groupBox1 is generated. It could be you simply need to add the right "order" clause, it's impossible to tell from what you've posted.

If you need to iterate in tab order then use the Control.GetNextControl Method (Control, Boolean) (System.Windows.Forms)[^].
 
Share this answer
 
An alternative to setting the tab order might be something like this:
1) you fetch the Checkboxes out of the groupBox controls collection and sort them e.g. by name

      List<CheckBox> listCheckboxesOrderedByName = 
        groupBox1.Controls
          .Cast<CheckBox>()
          .OrderBy(c => c.Name)
          .ToList();


foreach (var c in listCheckboxesOrderedByName )
{
  // 2) now this loop runs through the checkboxes 
  // that have been sorted by name

}
 
Share this answer
 
v2
Comments
chinu1d 6-Jun-17 8:25am    
no i meant i dint understood use of LINQ can u help me doing it with the foreach loop only
chinu1d 6-Jun-17 11:55am    
THANK YOU it helped

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900