Fill the ListView using DataSet
-
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
-
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
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
-
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
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
-
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
-
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
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\
-
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\
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) {
-
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) {
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