1

I have a form application on which multiple text boxes and list view given... I want to show list view data into csv file and then read data from csv and displayng onto listview. Code is given here... in this code m not getting desire output

private void btnUserInformation_Click(object sender, EventArgs e)
    {
        ListView view=new ListView();
        string filepath=@"E:\Visual Studio Project\UserInfoTestApp\UserInfoTestApp\UserInfo.csv";
        InformationInsertion();
        ClearFields();
        ListViewtoCSV(view, filepath);

    }

    public void ListViewtoCSV(ListView lv,string path)
    {
        string Info ="";
        for (int i = 0; i < lv.Items.Count; i++)
        {
            for (int j = 1; j < lv.Items.Count; j++)
            {
                Info += lvUserInformation.Items[i].SubItems[j].Text + ",";
            }
            Info = Info.TrimEnd(',');
        }           

        if (!File.Exists(path))
        {
            File.WriteAllText(path, Info);
        }
        File.AppendAllText(path, Info);
    }

plz write simple code which can be understand easily

enter image description here

4

1 回答 1

1

我希望我已经理解并编码了这个:

  private void saveInCSVFILE()
    {
        if (txtName.Text != string.Empty && txtLastName.Text != string.Empty && txtAge.Text != string.Empty && txtAdress.Text != string.Empty && txtContactNumber.Text != string.Empty)
        {
            using (System.IO.StreamWriter sw = new System.IO.StreamWriter("filename.csv", true))
            { sw.WriteLine(string.Format("{0};{1};{2};{3};{4}", txtName.Text, txtLastName.Text, txtAge.Text, txtAdress.Text, txtContactNumber.Text)); }
            updateTable();
        }

    }
    private void updateTable()
    {
        dgv.Rows.Clear();
        dgv.Columns.Clear();
        using (System.IO.StreamReader sr = new System.IO.StreamReader("filename.csv"))
        {
            while (sr.Peek() > -1)
            {
                string setRow = string.Empty;
                string[] tmp = sr.ReadLine().Split(';');
                setRow = tmp[0] + ';' + tmp[1] + ';' + tmp[2];
                dgv.Rows.Add(setRow.Split(';'));
            }
        }
    }

1)如果你想导出这个项目永远不要使用相对路径,但你必须使用绝对路径

2)我使用了 using 构造,因此循环完成后文件将自动关闭。

3)这不是最好的解决方案,因为每次调用更新表之前,它都会从列和行中清除表,然后用数据填充 dgv。

编辑:

我认为这是一个更好的解决方案:

  private void SaveInCSVFILE()
    {
        if (txtName.Text != string.Empty && txtLastName.Text != string.Empty && txtAge.Text != string.Empty && txtAdress.Text != string.Empty && txtContactNumber.Text != string.Empty)
        {
            using (System.IO.StreamWriter sw = new System.IO.StreamWriter("filename.csv", true))
            { sw.WriteLine(string.Format("{0};{1};{2};{3};{4}", txtName.Text, txtLastName.Text, txtAge.Text, txtAdress.Text, txtContactNumber.Text)); }
            UpdateTable();
        }

    }
    private void UpdateTable()
    {
        using (System.IO.StreamReader sr = new System.IO.StreamReader("filename.csv"))
        {
            int tmpCountRow = 0;
            while (sr.Peek() > -1)
            {
                tmpCountRow++;
                if (tmpCountRow > this.rowCountFile)
                {
                    AddRow(sr.ReadLine());
                    this.rowCountFile++;
                }
                else
                    sr.ReadLine();
            }
        }
    }
    private void ClearDataGridView()
    {
        dgv.Rows.Clear();
        dgv.Columns.Clear();
    }
    private void AddRow(string s)
    {
        string setRow = string.Empty;
        string[] tmp = s.Split(';');
        setRow = tmp[0] + ';' + tmp[1] + ';' + tmp[2];
        dgv.Rows.Add(setRow.Split(';'));
    }
    private void LoadTable()
    {
        ClearDataGridView();
        dgv.ColumnCount = 3;
        dgv.Columns[0].HeaderCell.Value = "First Name";
        dgv.Columns[1].HeaderCell.Value = "Last Name";
        dgv.Columns[2].HeaderCell.Value = "Age";
        using (System.IO.StreamReader sr=new System.IO.StreamReader("filename.csv"))
        {
            while (sr.Peek() > -1)
            {
                this.rowCountFile++;
                while (sr.Peek() > -1)
                {
                    AddRow(sr.ReadLine());
                }
            }
        }
    }

在第二个解决方案中,我实现了该功能LoadTable() AddRow(string s) ClearDataGrdView()并进行了编辑UpdateTable()

1) 该方法LoadTable()将在 form_load 中调用:此方法用 csv 文件中的数据填充您的 datagridview(dgv)。这种方法保存文件中的行

2)该方法AddRow(string s)只需在dgv中添加一行

3)该方法ClearDataGridView()被调用LoadTable():从那一刻的行和列中清除datagridview

4)UpdateTable()现在的方法更好,因为只需添加之前没有的行。

于 2013-10-12T20:43:20.180 回答