• 【整理】C# WinFrom 中如何txt内容与dataGridView互动


      题目:  txt内容为固定格式:帐号----密码----备注(用4个“-”分隔)

                 TXT内容如下:
                    baidu----123456----百度帐号
                    hao123----123456----hao123帐号
                    duowan----123456----多玩帐号


              要求显示在dataGridView效果如下表格

                   帐号      密码       备注
                baidu     123456     baidu帐号
                hao123    123456     hao123帐号
                duowan    123456     多玩帐号   

    txt文档---》datagridview代码:(已验证)

    private void button15_Click(object sender, EventArgs e)
            {
                        //新建一个datatable用于保存读入的数据
                        DataTable dt = new DataTable();

                        //给datatable添加三个列
                        dt.Columns.Add("帐号", typeof(String));
                        dt.Columns.Add("密码", typeof(String));
                        dt.Columns.Add("备注", typeof(String));

                        //读入文件
                       StreamReader reader = new StreamReader(@"11\11.txt", Encoding.Default);
                      
                            //循环读取所有行
                            while (!reader.EndOfStream)
                            {
                                //将每行数据,用-分割成3段
                                string[] data = reader.ReadLine().Replace("----", "-").Split('-');

                                //新建一行,并将读出的数据分段,分别存入3个对应的列中
                                DataRow dr = dt.NewRow();
                                dr[0] = data[0];
                                dr[1] = data[1];
                                dr[2] = data[2];

                                //将这行数据加入到datatable中
                                dt.Rows.Add(dr);
                            }
                      

                        //将datatable绑定到datagridview上显示结果
                        dataGridView2.DataSource = dt;

            }

    datagridview---》txt文档代码:(已验证)

     

            private void button9_Click_1(object sender, EventArgs e)
            {


                FileStream fileStream = new FileStream(@"11\11.txt", FileMode.OpenOrCreate);
                StreamWriter streamWriter = new StreamWriter(fileStream, System.Text.Encoding.Unicode);

                StringBuilder strBuilder = new StringBuilder();

                try
                {
                    for (int i = 0; i < dataGridView2.Rows.Count; i++)
                    {
                        strBuilder = new StringBuilder();
                        for (int j = 0; j < dataGridView2.Columns.Count; j++)
                        {
                            strBuilder.Append(dataGridView2.Rows[i].Cells[j].Value.ToString() +"----");
                        }
                        strBuilder.Remove(strBuilder.Length - 1, 1);
                        streamWriter.WriteLine(strBuilder.ToString());
                    }
                }
                catch (Exception ex)
                {
                    string strErrorMessage = ex.Message;
                }
                finally
                {
                    streamWriter.Close();
                    fileStream.Close();
                }

              
            }

  • 相关阅读:
    块级元素与行级元素(内联元素)
    css中属性值继承小解
    form表单
    html,xhtml和xml
    html中的标签分类
    如何把HTML标记分类
    实现对HashMap的value排序
    装饰者模式
    实现一个简单的二叉树容器,并且实现中序、先序、后续遍历
    Java中java.util.concurrent包下的4中线程池代码示例
  • 原文地址:https://www.cnblogs.com/rainuu/p/1638033.html
Copyright © 2020-2023  润新知