473,804 Members | 3,029 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Save Listview items to Dataset > XML

I have found plenty of details on filling a listview from a dataset, but I
need to do the opposite. I have a ListView and I would like to save out the
information it contains to xml (or anything else I guess) that can then
easily be imported into Excel for example.

Any pointers? I see DataSets have a WriteXML method which should be fine,
I've used it before, but I can't figure out how to get the Listview contents
into the dataset!

Thanks

Nov 14 '07 #1
2 5211
So just reverse the logic... I've used DataTable here, but "csv" or
"tsv" would be better choices if Excel is your intent (I didn't spot
this until just now) - plus they're very easy to construct (much
easier than DataTable)...

using System;
using System.Windows. Forms;
using System.Xml;
using System.Data;

class Program {
static void Main() {
Application.Ena bleVisualStyles ();
using(Form f = new Form())
using (ListView lv = new ListView())
using (Button b = new Button()) {
lv.Columns.Add( "Bloop");
lv.Columns.Add( "Blap");
lv.Columns.Add( "Blip Blop");
lv.View = View.Details;
int j = 1;
for (int i = 0; i < 10; i++) {
string[] items = {j++.ToString() , j++.ToString(),
j++.ToString()} ;
lv.Items.Add(ne w ListViewItem(it ems));
}

b.Click += delegate {
DataTable dt = new DataTable("MyDa ta");
foreach (ColumnHeader col in lv.Columns) {
dt.Columns.Add( col.Text, typeof(string)) ;
}
object[] vals = new object[lv.Columns.Coun t];
foreach (ListViewItem item in lv.Items) {
ListViewItem.Li stViewSubItemCo llection items =
item.SubItems;
for (int i = 0; i < vals.Length; i++) {
vals[i] = items[i].Text;
}
dt.Rows.Add(val s);
}
dt.WriteXml(@"c :\out_dt.xml");
};
b.Dock = DockStyle.Top;
lv.Dock = DockStyle.Fill;
f.Controls.Add( b);
f.Controls.Add( lv);
Application.Run (f);
}
}
}

Nov 14 '07 #2

"Marc Gravell" <ma**********@g mail.comwrote in message
news:uP******** ******@TK2MSFTN GP04.phx.gbl...
So just reverse the logic... I've used DataTable here, but "csv" or "tsv"
would be better choices if Excel is your intent (I didn't spot this until
just now) - plus they're very easy to construct (much easier than
DataTable)...

using System;
using System.Windows. Forms;
using System.Xml;
using System.Data;

class Program {
static void Main() {
Application.Ena bleVisualStyles ();
using(Form f = new Form())
using (ListView lv = new ListView())
using (Button b = new Button()) {
lv.Columns.Add( "Bloop");
lv.Columns.Add( "Blap");
lv.Columns.Add( "Blip Blop");
lv.View = View.Details;
int j = 1;
for (int i = 0; i < 10; i++) {
string[] items = {j++.ToString() , j++.ToString(),
j++.ToString()} ;
lv.Items.Add(ne w ListViewItem(it ems));
}

b.Click += delegate {
DataTable dt = new DataTable("MyDa ta");
foreach (ColumnHeader col in lv.Columns) {
dt.Columns.Add( col.Text, typeof(string)) ;
}
object[] vals = new object[lv.Columns.Coun t];
foreach (ListViewItem item in lv.Items) {
ListViewItem.Li stViewSubItemCo llection items =
item.SubItems;
for (int i = 0; i < vals.Length; i++) {
vals[i] = items[i].Text;
}
dt.Rows.Add(val s);
}
dt.WriteXml(@"c :\out_dt.xml");
};
b.Dock = DockStyle.Top;
lv.Dock = DockStyle.Fill;
f.Controls.Add( b);
f.Controls.Add( lv);
Application.Run (f);
}
}
}


That seemed to work a treat!
Thanks

James

Nov 14 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
2148
by: ExDelphi | last post by:
Hi All I am fairly new to VB.Net, and have gotten stuck with an invoicing application. My database (Access 2000 / 2003) is used to store records of our products, customers, suppliers etc. The user can select customer details which form the proposal / invoice header. The user can also select certain products which can be added to the proposal "dataset". The selected product items are displayed in a listview control. My question is as...
7
6452
by: Dave Y | last post by:
I am a newbie to C# and am having trouble trying to override a ListView property method. I have created a new class derived from the Forms.Listview and I cannot figure out the syntax to override ListView.Items.Add(), . I see that it is a virtual method so it should be easy to do. If anyone can help I would appreciate it greatly. I can do what I need to do in a different way this would just make everything significantly cleaner and eaasier...
4
17096
by: Lasse Eskildsen | last post by:
Hi, Is there an easy way to load the content of a dataset into a listview with a few columns? Thanks! -- Lasse
3
2234
by: amber | last post by:
I have a listview (lstvwAmend) (containing 2 columns) that is populated by a filtered dataview (dvRPA) that is based on a dataset (dsRPAmend1) pulling data from a SQL Server database This dataview will contain 0-10 records. The dataview is displaying field 1 & field 2 from my dataview When a user clicks on one of the items in the listview, I want to set the value of a textbox on my form to equal field 3 from the same row in my dataview I...
3
4271
by: simchajoy2000 | last post by:
Hi, I am trying to use a VB.NET listview object to display information from a datatable. I need to have two columns of information but I don't want the user to be able to select each column individually - that's why I am attempting to use a listview rather than a datagrid. I have been reading a lot of postings in this newsgroup concerning multiple columns in listviews and I have tried to follow all the advice I have found - but I am...
7
15009
by: BobAchgill | last post by:
I am trying to decide which of these controls to use to implement letting my user select a full row from MyList. The MyList has several columns which would be nice to sort by at run time. The MyList data is resident in a dataset table. I'm stuck and can't choose either because. If I choose ListView as my control I don't understand how to programmatically get the data from the dataset table
1
2344
by: Hareth | last post by:
how do I save and open checked and unchecked items in my listview to a txt file this is kinda what I have in mind but doesnt work; I need it for listview w/ checked items and unchecked private void btnSave_Click(object sender, System.EventArgs e)
2
6727
by: Lars Erik Nes | last post by:
Hi all. I have a problem. I get all my data from a dataset (not mssql) and in that dataset i have a table called "Activity". In this table i have a few date fields. When i display the dataset "Activity" in a listview the date fields is displayed like : dd.mm.yy 00:00:00. How can i format this date. I do not want the time. Just the date. In my dataset "activity" all date fields are formatted as: dd.mm.yyyy. Why do i get the 00:00:00 ??...
1
2061
by: samoore33 | last post by:
I found this code on MSDN, and it works great. It creates a ListView dynamically and add items to it and all. It is great. I have changed a few of the column names to suit me. Dim listView1 As New ListView listView1.Bounds = New Rectangle(New Point(10, 10), New Size(300, 200)) ' Set the view to show details.
0
9706
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10571
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10326
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10317
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9143
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7615
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6851
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5651
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2990
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.