标题:数据存储疑问...求大侠解答
只看楼主
BlizzardKen
Rank: 1
来 自:广西桂林
等 级:新手上路
帖 子:36
专家分:0
注 册:2011-4-26
结帖率:33.33%
已结贴  问题点数:20 回复次数:1 
数据存储疑问...求大侠解答


using System;

using System.Collections.Generic;

using

using System.Data;

using System.Drawing;

using System.Text;

using System.Windows.Forms;

using

using System.Runtime.InteropServices;
using MySql.Data;
using MySql.Data.MySqlClient;

namespace Post
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
   
        }
     //   public struct Information
    //    {
    //        public string Name;
    //        public int Number;
    //        public int SellPrice;
    //        public int UnitPrice;
    //        public int BarCode;
        
     //   }
        private void button1_Click(object sender, EventArgs e)
        {
            string filepath =@"D:\My Documents\Visual Studio 2008\Projects\Post\Post\Data\2012720.013";
            StreamWriter sw = new StreamWriter(filepath, true, System.Text.Encoding.GetEncoding("GB2312"));
            string fs = "";
            for (int i = 0; i < (dataGridView1.Rows.Count - 1); i++)
            {
                fs = "";
                for (int j = 0; j < dataGridView1.Columns.Count; j++)
                {
                    //currentcell :访问当前表格;
                    this.dataGridView1.CurrentCell = this.dataGridView1.Rows[i].Cells[j];
                    Clipboard.SetDataObject(dataGridView1.GetClipboardContent());
                    fs =fs+Clipboard.GetText().ToString()+" || ";
                }
                sw.WriteLine(fs);
            }
            sw.Flush();
            sw.Close();
        }



    }
}
这是直接存到txt文档中的,我想知道怎么通过结构体数组来存储数据,然后再转存到TXT文本中,哪位高手帮忙修改下代码,让我参考下
[local]1[/local]


[ 本帖最后由 BlizzardKen 于 2012-7-21 11:39 编辑 ]
搜索更多相关主题的帖子: private public 
2012-07-21 11:37
yhlvht
Rank: 13Rank: 13Rank: 13Rank: 13
等 级:贵宾
威 望:36
帖 子:707
专家分:4405
注 册:2011-9-30
得分:20 
private void button1_Click(object sender, EventArgs e)
{
    string filepath =@"D:\My Documents\Visual Studio 2008\Projects\Post\Post\Data\2012720.013";
    StreamWriter sw = new StreamWriter(filepath, true, System.Text.Encoding.GetEncoding("GB2312"));
    string fs = "";
    //new一个结构体数组,用于存放结构体
    Information[] arrIfm = new Information[dataGridView1.Rows.Count - 1];
    for (int i = 0; i < (dataGridView1.Rows.Count - 1); i++)
    {
        fs = "";
        //因为要把值赋给结构体,所以就不用循环列了
        //for (int j = 0; j < dataGridView1.Columns.Count; j++)
        //{
            //currentcell :访问当前表格;
            //每行new一个结构体实例
            Information ifm = new Information();
            //因为只是要取单元格文本值,所以取value就可以了
            ifm.Name = this.dataGridView1.Rows[i].Cells[0].Value.ToString();
            ifm.Number = (int)this.dataGridView1.Rows[i].Cells[1].Value;
            ifm.SellPrice = (int)this.dataGridView1.Rows[i].Cells[2].Value;
            ifm.UnitPrice = (int)this.dataGridView1.Rows[i].Cells[3].Value;
            ifm.BarCode = (int)this.dataGridView1.Rows[i].Cells[4].Value;
            //把结构体存入结构体数组
            arrIfm[i] = ifm;
            //this.dataGridView1.CurrentCell = this.dataGridView1.Rows[i].Cells[j];
            //Clipboard.SetDataObject(dataGridView1.GetClipboardContent());
            //fs =fs+Clipboard.GetText().ToString()+" || ";
        //}
        //sw.WriteLine(fs);
    }
    //循环结构体数组,把结构体取出来,再把值取出来写入txt
    for(int i = 0; i < arrIfm.Length; i++)
    {
        Information ifm = arrIfm[i];
        fs = ifm.Name + " || " + ifm.Number + " || " + ifm.SellPrice + " || " + ifm.UnitPrice + " || " + ifm.BarCode;
        sw.WriteLine(fs);
    }
    sw.Flush();
    sw.Close();
}

[ 本帖最后由 yhlvht 于 2012-7-22 17:03 编辑 ]
2012-07-22 17:01



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-373818-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.048357 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved