| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 356 人关注过本帖
标题:数据存储疑问...求大侠解答
只看楼主 加入收藏
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
快速回复:数据存储疑问...求大侠解答
数据加载中...
 
   



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

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