Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Managed C++/CLI
  4. Fill the ListView using DataSet

Fill the ListView using DataSet

Scheduled Pinned Locked Moved Managed C++/CLI
helpdatabase
7 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    ksandy45
    wrote on last edited by
    #1

    Hi I need to know the method to fill the ListView with my database contents. I tried with the help of the Dataset, but could not find any solution to it. I am not able to populate the list view with my dataset contents. Please send me any sample code to solve this problem. Kindly do me a favour by helping me to find the solution to this. Thank you :) -- modified at 4:36 Tuesday 5th December, 2006

    Kind Regards Sandy

    T 1 Reply Last reply
    0
    • K ksandy45

      Hi I need to know the method to fill the ListView with my database contents. I tried with the help of the Dataset, but could not find any solution to it. I am not able to populate the list view with my dataset contents. Please send me any sample code to solve this problem. Kindly do me a favour by helping me to find the solution to this. Thank you :) -- modified at 4:36 Tuesday 5th December, 2006

      Kind Regards Sandy

      T Offline
      T Offline
      teejayem
      wrote on last edited by
      #2

      Here is a code snippet that i have used to update the listview from a DataSet. This snippet goes the each row in the dataset (in my case myDataSet) and Creates a ListViewItem to add to your ListView Control (in my case myListView1).

      				 for each (DataRow^ r in myDataSet->Tables\[0\]->Rows)
      				 {
                                                   //Create a listView Item and add the first column of the dataset to it
      					 ListViewItem^ lvindex = gcnew ListViewItem(r\[0\]->ToString());
                                                   //Create a subitem and add the seconed column to it
      					 ListViewItem::ListViewSubItem^ sublvindex = gcnew ListViewItem::ListViewSubItem(lvindex,r\[1\]->ToString());
                                                   //Add the 1st subitem to the listview item
      					 myListView1->SubItems->Add(sublvindex);
                                                   //Create the seconed ListView SubItem and add the third column of the Dataset to it.
      					 sublvindex = gcnew ListViewItem::ListViewSubItem(lvindex,r\[2\]->ToString());
                                                   //Add the 2nd subitem to the listview item
      					 myListView1->SubItems->Add(sublvindex);
                                                   //Add the entire ListViewItem to the next available row in myListView
      					 myListView1->Items->Add(lvindex);
      				 }
      

      hope this helps.

      Don't be overcome by evil, but overcome evil with good

      K 1 Reply Last reply
      0
      • T teejayem

        Here is a code snippet that i have used to update the listview from a DataSet. This snippet goes the each row in the dataset (in my case myDataSet) and Creates a ListViewItem to add to your ListView Control (in my case myListView1).

        				 for each (DataRow^ r in myDataSet->Tables\[0\]->Rows)
        				 {
                                                     //Create a listView Item and add the first column of the dataset to it
        					 ListViewItem^ lvindex = gcnew ListViewItem(r\[0\]->ToString());
                                                     //Create a subitem and add the seconed column to it
        					 ListViewItem::ListViewSubItem^ sublvindex = gcnew ListViewItem::ListViewSubItem(lvindex,r\[1\]->ToString());
                                                     //Add the 1st subitem to the listview item
        					 myListView1->SubItems->Add(sublvindex);
                                                     //Create the seconed ListView SubItem and add the third column of the Dataset to it.
        					 sublvindex = gcnew ListViewItem::ListViewSubItem(lvindex,r\[2\]->ToString());
                                                     //Add the 2nd subitem to the listview item
        					 myListView1->SubItems->Add(sublvindex);
                                                     //Add the entire ListViewItem to the next available row in myListView
        					 myListView1->Items->Add(lvindex);
        				 }
        

        hope this helps.

        Don't be overcome by evil, but overcome evil with good

        K Offline
        K Offline
        ksandy45
        wrote on last edited by
        #3

        I am really greatfull to you for the support replay... But i guess the code is not for VC++.Net, I need to it in managed C++, where i am not able to use the keywords like "for Each" & Table[0]->. I am geeting error for this code. Can you kindly tell me if there is a problem with my code or else i need alter the code provided by you... Please do me a favour by giving me this piece of Info. Thanks a lot

        Kind Regards Sandy

        T 1 Reply Last reply
        0
        • K ksandy45

          I am really greatfull to you for the support replay... But i guess the code is not for VC++.Net, I need to it in managed C++, where i am not able to use the keywords like "for Each" & Table[0]->. I am geeting error for this code. Can you kindly tell me if there is a problem with my code or else i need alter the code provided by you... Please do me a favour by giving me this piece of Info. Thanks a lot

          Kind Regards Sandy

          T Offline
          T Offline
          teejayem
          wrote on last edited by
          #4

          Yes, this snippet is for Managed C++. Please post your code and copy the errors your getting from the error window and i'll see if i can fix it. Thanks

          Don't be overcome by evil, but overcome evil with good

          K 1 Reply Last reply
          0
          • T teejayem

            Yes, this snippet is for Managed C++. Please post your code and copy the errors your getting from the error window and i'll see if i can fix it. Thanks

            Don't be overcome by evil, but overcome evil with good

            K Offline
            K Offline
            ksandy45
            wrote on last edited by
            #5

            Hi, Yeah kindly find the list of errors below which are fired when i use this piece of code. This is my code : private: System::Void Form1_Load(System::Object * sender, System::EventArgs e) { System::String * SqlStr; ListView * listView1 = new ListView(); try { SqlStr = "Select IC, Part_Code, PART_NUMBER, PART_NAME from vdcsssp_PARTS_mst" ; SqlCommand* objCmd = new SqlCommand(); SqlDataAdapter* objDA = new SqlDataAdapter(); DataSet* objDS = new DataSet(); SqlConnection* objCon = new SqlConnection(); objCon->ConnectionString = "User ID=sa;pwd=qil2004;Data Source=qipl-server;Initial Catalog=Qvdcsssp"; if(objCon->get_ConnectionString() != "") { objCon->Open(); objCmd->CommandText = SqlStr; objCmd->Connection = objCon; objDA->SelectCommand = objCmd; objDA->Fill(objDS); } else MessageBox::Show("Error connection Failed"); return; for each (DataRow* r in objDS->Tables[0]->Rows) { //Create a listView Item and add the first column of the dataset to it ListViewItem* lvindex = gcnew ListViewItem(r[0]->ToString()); //Create a subitem and add the seconed column to it ListViewItem::ListViewSubItem* sublvindex = gcnew ListViewItem::ListViewSubItem(lvindex,r[1]->ToString()); //Add the 1st subitem to the listview item listView1->SubItems->Add(sublvindex); //Create the seconed ListView SubItem and add the third column of the Dataset to it. sublvindex = gcnew ListViewItem::ListViewSubItem(lvindex,r[2]->ToString()); //Add the 2nd subitem to the listview item listView1->SubItems->Add(sublvindex); //Add the entire ListViewItem to the next available row in myListView listView1->Items->Add(lvindex); } } catch(Exception* ex) { MessageBox::Show(ex->Message); } ----------------------------------------------------------------------------- This is my errors: e:\Sandeep\VC++ .Net\Test1\Form1.h(376) : error C2061: syntax error : identifier 'each' e:\Sandeep\VC++ .Net\Test1\Form1.h(377) : error C2143: syntax error : missing ';' before '{' e:\Sandeep\VC++ .Net\Test1\Form1.h(379) : error C2065: 'gcnew' : undeclared identifier e:\Sandeep\VC++ .Net\Test1\Form1.h(379) : error C2146: syntax error : missing ';' before identifier 'ListViewItem' e:\Sandeep\VC++ .Net\

            T 1 Reply Last reply
            0
            • K ksandy45

              Hi, Yeah kindly find the list of errors below which are fired when i use this piece of code. This is my code : private: System::Void Form1_Load(System::Object * sender, System::EventArgs e) { System::String * SqlStr; ListView * listView1 = new ListView(); try { SqlStr = "Select IC, Part_Code, PART_NUMBER, PART_NAME from vdcsssp_PARTS_mst" ; SqlCommand* objCmd = new SqlCommand(); SqlDataAdapter* objDA = new SqlDataAdapter(); DataSet* objDS = new DataSet(); SqlConnection* objCon = new SqlConnection(); objCon->ConnectionString = "User ID=sa;pwd=qil2004;Data Source=qipl-server;Initial Catalog=Qvdcsssp"; if(objCon->get_ConnectionString() != "") { objCon->Open(); objCmd->CommandText = SqlStr; objCmd->Connection = objCon; objDA->SelectCommand = objCmd; objDA->Fill(objDS); } else MessageBox::Show("Error connection Failed"); return; for each (DataRow* r in objDS->Tables[0]->Rows) { //Create a listView Item and add the first column of the dataset to it ListViewItem* lvindex = gcnew ListViewItem(r[0]->ToString()); //Create a subitem and add the seconed column to it ListViewItem::ListViewSubItem* sublvindex = gcnew ListViewItem::ListViewSubItem(lvindex,r[1]->ToString()); //Add the 1st subitem to the listview item listView1->SubItems->Add(sublvindex); //Create the seconed ListView SubItem and add the third column of the Dataset to it. sublvindex = gcnew ListViewItem::ListViewSubItem(lvindex,r[2]->ToString()); //Add the 2nd subitem to the listview item listView1->SubItems->Add(sublvindex); //Add the entire ListViewItem to the next available row in myListView listView1->Items->Add(lvindex); } } catch(Exception* ex) { MessageBox::Show(ex->Message); } ----------------------------------------------------------------------------- This is my errors: e:\Sandeep\VC++ .Net\Test1\Form1.h(376) : error C2061: syntax error : identifier 'each' e:\Sandeep\VC++ .Net\Test1\Form1.h(377) : error C2143: syntax error : missing ';' before '{' e:\Sandeep\VC++ .Net\Test1\Form1.h(379) : error C2065: 'gcnew' : undeclared identifier e:\Sandeep\VC++ .Net\Test1\Form1.h(379) : error C2146: syntax error : missing ';' before identifier 'ListViewItem' e:\Sandeep\VC++ .Net\

              T Offline
              T Offline
              teejayem
              wrote on last edited by
              #6

              I think (don't quote me on it) the problem was that you were trying to create a control using a pointer (*) and that is why alot of the error's came up. The snippet below compiled fine for me give it a try and let me know how it works for you. If you have anymore problems you can also email me djcarter2326@aim.com. I'm more used to using C# but i am teaching myself Managed C++.

              			 System::String ^ SqlStr;
              			 ListView ^ listView1 = gcnew ListView();
              			 try
              			 {
              				 SqlStr = "Select IC, Part\_Code, PART\_NUMBER, PART\_NAME from vdcsssp\_PARTS\_mst" ;
              
                               SqlCommand^ objCmd = gcnew SqlCommand();
                               SqlDataAdapter^ objDA = gcnew SqlDataAdapter();
                               DataSet^ objDS = gcnew DataSet();
                               SqlConnection^ objCon = gcnew SqlConnection();
                               objCon->ConnectionString = "User ID=sa;pwd=qil2004;Data Source=qipl-server;Initial Catalog=Qvdcsssp";
                               if(objCon->ConnectionString != "") 
              				 { 
              					 objCon->Open();
                                   objCmd->CommandText = SqlStr;
                                   objCmd->Connection = objCon;
                                   objDA->SelectCommand = objCmd;
                                   objDA->Fill(objDS); 
              				 }
              				 else
                                   MessageBox::Show("Error connection Failed");
                               return;
              				 
              				 for each (DataRow^ r in objDS->Tables\[0\]->Rows)
              				 {
              					 //Create a listView Item and add the first column of the dataset to it
                                   ListViewItem^ lvindex = gcnew ListViewItem(r\[0\]->ToString()); 
                                   //Create a subitem and add the seconed column to it 
                                   ListViewItem::ListViewSubItem^ sublvindex = gcnew ListViewItem::ListViewSubItem(lvindex,r\[1\]->ToString()); 
                                   //Add the 1st subitem to the listview item 
                                   lvindex->SubItems->Add(sublvindex);
                                   //Create the seconed ListView SubItem and add the third column of the Dataset to it.
                                   sublvindex = gcnew ListViewItem::ListViewSubItem(lvindex,r\[2\]->ToString()); 
                                   //Add the 2nd subitem to the listview item 
                                   lvindex->SubItems->Add(sublvindex);
                                   //Add the entire ListViewItem to the next available row in myListView
                                   listView1->Items->Add(lvindex); 
              				 }
              			 }
                           catch(Exception^ ex)
                           {
              
              K 1 Reply Last reply
              0
              • T teejayem

                I think (don't quote me on it) the problem was that you were trying to create a control using a pointer (*) and that is why alot of the error's came up. The snippet below compiled fine for me give it a try and let me know how it works for you. If you have anymore problems you can also email me djcarter2326@aim.com. I'm more used to using C# but i am teaching myself Managed C++.

                			 System::String ^ SqlStr;
                			 ListView ^ listView1 = gcnew ListView();
                			 try
                			 {
                				 SqlStr = "Select IC, Part\_Code, PART\_NUMBER, PART\_NAME from vdcsssp\_PARTS\_mst" ;
                
                                 SqlCommand^ objCmd = gcnew SqlCommand();
                                 SqlDataAdapter^ objDA = gcnew SqlDataAdapter();
                                 DataSet^ objDS = gcnew DataSet();
                                 SqlConnection^ objCon = gcnew SqlConnection();
                                 objCon->ConnectionString = "User ID=sa;pwd=qil2004;Data Source=qipl-server;Initial Catalog=Qvdcsssp";
                                 if(objCon->ConnectionString != "") 
                				 { 
                					 objCon->Open();
                                     objCmd->CommandText = SqlStr;
                                     objCmd->Connection = objCon;
                                     objDA->SelectCommand = objCmd;
                                     objDA->Fill(objDS); 
                				 }
                				 else
                                     MessageBox::Show("Error connection Failed");
                                 return;
                				 
                				 for each (DataRow^ r in objDS->Tables\[0\]->Rows)
                				 {
                					 //Create a listView Item and add the first column of the dataset to it
                                     ListViewItem^ lvindex = gcnew ListViewItem(r\[0\]->ToString()); 
                                     //Create a subitem and add the seconed column to it 
                                     ListViewItem::ListViewSubItem^ sublvindex = gcnew ListViewItem::ListViewSubItem(lvindex,r\[1\]->ToString()); 
                                     //Add the 1st subitem to the listview item 
                                     lvindex->SubItems->Add(sublvindex);
                                     //Create the seconed ListView SubItem and add the third column of the Dataset to it.
                                     sublvindex = gcnew ListViewItem::ListViewSubItem(lvindex,r\[2\]->ToString()); 
                                     //Add the 2nd subitem to the listview item 
                                     lvindex->SubItems->Add(sublvindex);
                                     //Add the entire ListViewItem to the next available row in myListView
                                     listView1->Items->Add(lvindex); 
                				 }
                			 }
                             catch(Exception^ ex)
                             {
                
                K Offline
                K Offline
                ksandy45
                wrote on last edited by
                #7

                Hi I dont know if i can use the char '^' works here i am getting errors on using this... The errors are... e:\Sandeep\VC++ .Net\Test1\Form1.h(386) : error C2143: syntax error : missing ';' before '^' e:\Sandeep\VC++ .Net\Test1\Form1.h(386) : error C2143: syntax error : missing ';' before '^' e:\Sandeep\VC++ .Net\Test1\Form1.h(387) : error C2143: syntax error : missing ';' before '^' e:\Sandeep\VC++ .Net\Test1\Form1.h(387) : error C2143: syntax error : missing ';' before '^' e:\Sandeep\VC++ .Net\Test1\Form1.h(390) : error C2065: 'SqlStr' : undeclared identifier e:\Sandeep\VC++ .Net\Test1\Form1.h(391) : error C2143: syntax error : missing ';' before '^' e:\Sandeep\VC++ .Net\Test1\Form1.h(391) : error C2143: syntax error : missing ';' before '^' e:\Sandeep\VC++ .Net\Test1\Form1.h(392) : error C2143: syntax error : missing ';' before '^' e:\Sandeep\VC++ .Net\Test1\Form1.h(392) : error C2143: syntax error : missing ';' before '^' e:\Sandeep\VC++ .Net\Test1\Form1.h(393) : error C2143: syntax error : missing ';' before '^' e:\Sandeep\VC++ .Net\Test1\Form1.h(393) : error C2143: syntax error : missing ';' before '^' e:\Sandeep\VC++ .Net\Test1\Form1.h(394) : error C2143: syntax error : missing ';' before '^' e:\Sandeep\VC++ .Net\Test1\Form1.h(394) : error C2143: syntax error : missing ';' before '^' e:\Sandeep\VC++ .Net\Test1\Form1.h(399) : error C3861: 'SqlStr': identifier not found, even with argument-dependent lookup e:\Sandeep\VC++ .Net\Test1\Form1.h(407) : error C2061: syntax error : identifier 'each' e:\Sandeep\VC++ .Net\Test1\Form1.h(408) : error C2143: syntax error : missing ';' before '{' e:\Sandeep\VC++ .Net\Test1\Form1.h(410) : error C2143: syntax error : missing ';' before '^' e:\Sandeep\VC++ .Net\Test1\Form1.h(410) : error C2143: syntax error : missing ';' before '^' e:\Sandeep\VC++ .Net\Test1\Form1.h(412) : error C2143: syntax error : missing ';' before '^' e:\Sandeep\VC++ .Net\Test1\Form1.h(412) : error C2143: syntax error : missing ';' before '^' e:\Sandeep\VC++ .Net\Test1\Form1.h(414) : error C2065: 'lvindex' : undeclared identifier e:\Sandeep\VC++ .Net\Test1\Form1.h(414) : error C2227: left of '->SubItems' must point to class/struct/union type is ''unknown-type'' e:\Sandeep\VC++ .Net\Test1\Form1.h(414) : error C2227: left of '->Add' must point to class/struct/union e:\Sandeep\VC++ .Net\Test1\Form1.h(414) : error C2065: 'sublvindex' : undeclared identifier e:\Sandeep\VC++ .Net\Test1\Form1.h(416) : error C2065: 'gcnew' : undeclared identifier e:\Sandeep\VC++ .N

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups