Skip to main content
fixed code formatting
Source Link
Tom V
  • 15.7k
  • 7
  • 64
  • 87

if (Collection.Count > 0) { bool firstRecord = true; bool secondRecord = false; FirstRowEntiry ent = null;

if (Collection.Count > 0)
        {
            bool firstRecord = true;
            bool secondRecord = false;
            FirstRowEntiry ent = null;

            foreach (SecondRowEntity ope in opc)
            {                                   
              
               if (firstRecord == true)
               {                     
                   //saving first row data in Ent row
                   ent = ope;
                   firstRecord = false;
                   secondRecord = true;
                   continue;
               }
              
               if (secondRecord == true)
               {

        -----------------------------------------------------
        //checking first and second records match exists  
        ------------------------------------------------------
                   if (ope.PrimaryKeyId == ent.PrimaryKeyId && ope.Type == ent.Type)
                   {

        // Add all items to listview
                       lvi.SubItems.Add(ent.PrimaryKeyId.ToString());
                       lvi.SubItems.Add("");
                       firstRecord = true;
                       secondRecord = false;
                       listView1.Items.Add(lvi);
                   }
                   else
                   {
        -----------------------------------------------------------
        // If id and type doenotmatch at all then will follows
        ---------------------------------------------------------------

                       bool entDone = false;
                       bool opeDone = false;

                         if (ent.Type == "Type1" && entDone == false)
                           {

              // Add items for required columns and other columns make empty
               lvi = new ListViewItem(ope.PrimaryKeyId.ToString());
                               lvi.SubItems.Add(ent.PrimaryKeyId.ToString());
                               lvi.SubItems.Add(ent.Type.ToString());                                   
                               lvi.SubItems.Add("");
                               lvi.SubItems.Add("");
                               lvi.SubItems.Add("");
                               lvi.SubItems.Add("");                                  
                               listView1.Items.Add(lvi);
                               entDone = true;
                           }
                           if (ent.Type == "Type2" && opeDone == false)
                           {
             // Add items for required columns and other columns make empty
                               lvi.SubItems.Add("");
                               lvi.SubItems.Add("");
                               lvi.SubItems.Add("");
                               lvi.SubItems.Add("");
                               lvi.SubItems.Add(ent.PrimaryKeyId.ToString());
                               lvi.SubItems.Add(ent.Type.ToString());  
                               listView1.Items.Add(lvi);
                               entDone = true;
                           }

                           if (ope.Type == "Type1" && opeDone == false)
                           {
                               lvi = new ListViewItem(ope.PrimaryKeyId.ToString());
                               lvi.SubItems.Add(ope.Type.ToString());
                               lvi.SubItems.Add(ope.Type.ToString());  
                               lvi.SubItems.Add("");
                               lvi.SubItems.Add("");
                               lvi.SubItems.Add("");
                               lvi.SubItems.Add("");                                   
                               listView1.Items.Add(lvi);
                               opeDone = true;
                           }
                           if (ope.Type == "Type2" && opeDone == false)
                           {
                               lvi = new ListViewItem(ope.PrimaryKeyId.ToString());
                               lvi.SubItems.Add("");
                               lvi.SubItems.Add("");
                               lvi.SubItems.Add("");
                               lvi.SubItems.Add("");
                               lvi.SubItems.Add(ope.Type.ToString());
                               lvi.SubItems.Add(ope.Type.ToString());  
                               listView1.Items.Add(lvi);
                               opeDone = true;
                           }
                      
                       firstRecord = true;
                       secondRecord = false;
                   }
               }

                
            }
        }

if (Collection.Count > 0) { bool firstRecord = true; bool secondRecord = false; FirstRowEntiry ent = null;

            foreach (SecondRowEntity ope in opc)
            {                                   
              
               if (firstRecord == true)
               {                     
                   //saving first row data in Ent row
                   ent = ope;
                   firstRecord = false;
                   secondRecord = true;
                   continue;
               }
              
               if (secondRecord == true)
               {

        -----------------------------------------------------
        //checking first and second records match exists  
        ------------------------------------------------------
                   if (ope.PrimaryKeyId == ent.PrimaryKeyId && ope.Type == ent.Type)
                   {

        // Add all items to listview
                       lvi.SubItems.Add(ent.PrimaryKeyId.ToString());
                       lvi.SubItems.Add("");
                       firstRecord = true;
                       secondRecord = false;
                       listView1.Items.Add(lvi);
                   }
                   else
                   {
        -----------------------------------------------------------
        // If id and type doenotmatch at all then will follows
        ---------------------------------------------------------------

                       bool entDone = false;
                       bool opeDone = false;

                         if (ent.Type == "Type1" && entDone == false)
                           {

              // Add items for required columns and other columns make empty
               lvi = new ListViewItem(ope.PrimaryKeyId.ToString());
                               lvi.SubItems.Add(ent.PrimaryKeyId.ToString());
                               lvi.SubItems.Add(ent.Type.ToString());                                   
                               lvi.SubItems.Add("");
                               lvi.SubItems.Add("");
                               lvi.SubItems.Add("");
                               lvi.SubItems.Add("");                                  
                               listView1.Items.Add(lvi);
                               entDone = true;
                           }
                           if (ent.Type == "Type2" && opeDone == false)
                           {
             // Add items for required columns and other columns make empty
                               lvi.SubItems.Add("");
                               lvi.SubItems.Add("");
                               lvi.SubItems.Add("");
                               lvi.SubItems.Add("");
                               lvi.SubItems.Add(ent.PrimaryKeyId.ToString());
                               lvi.SubItems.Add(ent.Type.ToString());  
                               listView1.Items.Add(lvi);
                               entDone = true;
                           }

                           if (ope.Type == "Type1" && opeDone == false)
                           {
                               lvi = new ListViewItem(ope.PrimaryKeyId.ToString());
                               lvi.SubItems.Add(ope.Type.ToString());
                               lvi.SubItems.Add(ope.Type.ToString());  
                               lvi.SubItems.Add("");
                               lvi.SubItems.Add("");
                               lvi.SubItems.Add("");
                               lvi.SubItems.Add("");                                   
                               listView1.Items.Add(lvi);
                               opeDone = true;
                           }
                           if (ope.Type == "Type2" && opeDone == false)
                           {
                               lvi = new ListViewItem(ope.PrimaryKeyId.ToString());
                               lvi.SubItems.Add("");
                               lvi.SubItems.Add("");
                               lvi.SubItems.Add("");
                               lvi.SubItems.Add("");
                               lvi.SubItems.Add(ope.Type.ToString());
                               lvi.SubItems.Add(ope.Type.ToString());  
                               listView1.Items.Add(lvi);
                               opeDone = true;
                           }
                      
                       firstRecord = true;
                       secondRecord = false;
                   }
               }

                
            }
        }
if (Collection.Count > 0)
        {
            bool firstRecord = true;
            bool secondRecord = false;
            FirstRowEntiry ent = null;

            foreach (SecondRowEntity ope in opc)
            {                                   
              
               if (firstRecord == true)
               {                     
                   //saving first row data in Ent row
                   ent = ope;
                   firstRecord = false;
                   secondRecord = true;
                   continue;
               }
              
               if (secondRecord == true)
               {

        -----------------------------------------------------
        //checking first and second records match exists  
        ------------------------------------------------------
                   if (ope.PrimaryKeyId == ent.PrimaryKeyId && ope.Type == ent.Type)
                   {

        // Add all items to listview
                       lvi.SubItems.Add(ent.PrimaryKeyId.ToString());
                       lvi.SubItems.Add("");
                       firstRecord = true;
                       secondRecord = false;
                       listView1.Items.Add(lvi);
                   }
                   else
                   {
        -----------------------------------------------------------
        // If id and type doenotmatch at all then will follows
        ---------------------------------------------------------------

                       bool entDone = false;
                       bool opeDone = false;

                         if (ent.Type == "Type1" && entDone == false)
                           {

              // Add items for required columns and other columns make empty
               lvi = new ListViewItem(ope.PrimaryKeyId.ToString());
                               lvi.SubItems.Add(ent.PrimaryKeyId.ToString());
                               lvi.SubItems.Add(ent.Type.ToString());                                   
                               lvi.SubItems.Add("");
                               lvi.SubItems.Add("");
                               lvi.SubItems.Add("");
                               lvi.SubItems.Add("");                                  
                               listView1.Items.Add(lvi);
                               entDone = true;
                           }
                           if (ent.Type == "Type2" && opeDone == false)
                           {
             // Add items for required columns and other columns make empty
                               lvi.SubItems.Add("");
                               lvi.SubItems.Add("");
                               lvi.SubItems.Add("");
                               lvi.SubItems.Add("");
                               lvi.SubItems.Add(ent.PrimaryKeyId.ToString());
                               lvi.SubItems.Add(ent.Type.ToString());  
                               listView1.Items.Add(lvi);
                               entDone = true;
                           }

                           if (ope.Type == "Type1" && opeDone == false)
                           {
                               lvi = new ListViewItem(ope.PrimaryKeyId.ToString());
                               lvi.SubItems.Add(ope.Type.ToString());
                               lvi.SubItems.Add(ope.Type.ToString());  
                               lvi.SubItems.Add("");
                               lvi.SubItems.Add("");
                               lvi.SubItems.Add("");
                               lvi.SubItems.Add("");                                   
                               listView1.Items.Add(lvi);
                               opeDone = true;
                           }
                           if (ope.Type == "Type2" && opeDone == false)
                           {
                               lvi = new ListViewItem(ope.PrimaryKeyId.ToString());
                               lvi.SubItems.Add("");
                               lvi.SubItems.Add("");
                               lvi.SubItems.Add("");
                               lvi.SubItems.Add("");
                               lvi.SubItems.Add(ope.Type.ToString());
                               lvi.SubItems.Add(ope.Type.ToString());  
                               listView1.Items.Add(lvi);
                               opeDone = true;
                           }
                      
                       firstRecord = true;
                       secondRecord = false;
                   }
               }

                
            }
        }
Source Link

Getting multiple rows from db and iterate them

if (Collection.Count > 0) { bool firstRecord = true; bool secondRecord = false; FirstRowEntiry ent = null;

            foreach (SecondRowEntity ope in opc)
            {                                   
              
               if (firstRecord == true)
               {                     
                   //saving first row data in Ent row
                   ent = ope;
                   firstRecord = false;
                   secondRecord = true;
                   continue;
               }
              
               if (secondRecord == true)
               {

        -----------------------------------------------------
        //checking first and second records match exists  
        ------------------------------------------------------
                   if (ope.PrimaryKeyId == ent.PrimaryKeyId && ope.Type == ent.Type)
                   {

        // Add all items to listview
                       lvi.SubItems.Add(ent.PrimaryKeyId.ToString());
                       lvi.SubItems.Add("");
                       firstRecord = true;
                       secondRecord = false;
                       listView1.Items.Add(lvi);
                   }
                   else
                   {
        -----------------------------------------------------------
        // If id and type doenotmatch at all then will follows
        ---------------------------------------------------------------

                       bool entDone = false;
                       bool opeDone = false;

                         if (ent.Type == "Type1" && entDone == false)
                           {

              // Add items for required columns and other columns make empty
               lvi = new ListViewItem(ope.PrimaryKeyId.ToString());
                               lvi.SubItems.Add(ent.PrimaryKeyId.ToString());
                               lvi.SubItems.Add(ent.Type.ToString());                                   
                               lvi.SubItems.Add("");
                               lvi.SubItems.Add("");
                               lvi.SubItems.Add("");
                               lvi.SubItems.Add("");                                  
                               listView1.Items.Add(lvi);
                               entDone = true;
                           }
                           if (ent.Type == "Type2" && opeDone == false)
                           {
             // Add items for required columns and other columns make empty
                               lvi.SubItems.Add("");
                               lvi.SubItems.Add("");
                               lvi.SubItems.Add("");
                               lvi.SubItems.Add("");
                               lvi.SubItems.Add(ent.PrimaryKeyId.ToString());
                               lvi.SubItems.Add(ent.Type.ToString());  
                               listView1.Items.Add(lvi);
                               entDone = true;
                           }

                           if (ope.Type == "Type1" && opeDone == false)
                           {
                               lvi = new ListViewItem(ope.PrimaryKeyId.ToString());
                               lvi.SubItems.Add(ope.Type.ToString());
                               lvi.SubItems.Add(ope.Type.ToString());  
                               lvi.SubItems.Add("");
                               lvi.SubItems.Add("");
                               lvi.SubItems.Add("");
                               lvi.SubItems.Add("");                                   
                               listView1.Items.Add(lvi);
                               opeDone = true;
                           }
                           if (ope.Type == "Type2" && opeDone == false)
                           {
                               lvi = new ListViewItem(ope.PrimaryKeyId.ToString());
                               lvi.SubItems.Add("");
                               lvi.SubItems.Add("");
                               lvi.SubItems.Add("");
                               lvi.SubItems.Add("");
                               lvi.SubItems.Add(ope.Type.ToString());
                               lvi.SubItems.Add(ope.Type.ToString());  
                               listView1.Items.Add(lvi);
                               opeDone = true;
                           }
                      
                       firstRecord = true;
                       secondRecord = false;
                   }
               }

                
            }
        }