Click here to Skip to main content
15,914,165 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
Dear all,

I got a problem, I use Visual studio 2010. I always show my result in Listview, but now I want to save it in a file. Can you help me? How to save a data to a file?

Thanks
Posted
Comments
Al Moje 3-Jan-12 20:37pm    
Could you show us codes that you had been started?...
phanny 2011 3-Jan-12 21:21pm    
Here is my code in Form.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Practice_3
{
public partial class Form1 : Form
{
private frmGlass _frmGlass;
private frShow _frmshow;
private Turism[] indexclass = new Turism[100];
int idex = 0;
public Form1()
{
InitializeComponent();
}

Turism add = new Turism();


private void btnAdd_Click(object sender, EventArgs e)
{
if (txtid.Text == "" || txtname.Text == "" || txtgender.Text == "" || txtaddress.Text == "" || txtpassport.Text == "" || datestart.Text == "" || enddate.Text == "")
{
MessageBox.Show("Sorry!Don't have value for insert ."+"\n"+"Please insert value");
}
else
{
indexclass[idex] = new Turism();
indexclass[idex].ID = txtid.Text;
indexclass[idex].Name = txtname.Text;
indexclass[idex].Gender = txtgender.Text;
indexclass[idex].Address = txtaddress.Text;
indexclass[idex].Phone = txtphone.Text;
indexclass[idex].Passport = txtpassport.Text;
indexclass[idex].Stardate = datestart.Text;
indexclass[idex].Enddate = enddate.Text;
indexclass[idex].Discrpition = txtDetail.Text;
++idex;



clear();
btnShow.Enabled = true;
}

}

private void btnShow_Click(object sender, EventArgs e)
{

_frmGlass = new frmGlass();
_frmshow = new frShow(_frmGlass, indexclass, idex);
_frmGlass.Show();
_frmshow.Show(_frmGlass);




}
internal void clear()
{
txtid.Text = "";
txtaddress.Text = "";
txtgender.Text = "";
txtname.Text = "";
txtpassport.Text = "";
txtphone.Text = "";
txtDetail.Text = "";
}


private void btnClears_Click(object sender, EventArgs e)
{
clear();
}

private void button1_Click(object sender, EventArgs e)
{
Close();


}

private void Form1_Load(object sender, EventArgs e)
{
txtgender.Text = "Male";
btnShow.Enabled = false;
}



}
}

1 solution

It sounds like you will want to store your results in a List<t> as well as your ListView and then write the contents of the list out to a file. i.e.
C#
List<string> resultData = new List<string>();
resultData.Add("Some result data");

etc...
C#
using (StreamWriter writer = new StreamWriter("pathOfFileToSaveDataTo"))
{
    for (int i = 0; i < resultData.Count; i++)
    {
        writer.WriteLine(resultData[i]);
    }

    writer.Close();
}


hope this helps.

EDIT:
You could also just write the contents of the ListView out to the file without storing the results in a List<T> if you wanted to.
 
Share this answer
 
v4

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