[codes=C#] if (txt_filename.Text.Trim() == "")
            {
                lb_showmsg.Text = "文件名為空不能創建";

            }
            else
            {


                txt_filename.Enabled = false;  
                lb_showmsg.Text = "文件名:" + txt_filename.Text + ".txt 創建成功";

                StreamWriter s_SW = new StreamWriter(@"D:\" + txt_filename.Text + ".txt", true);

                string myfile = "\n";

                DataGridView dv = dataGridView1;
              

                for (int i = 0; i < dv.RowCount; i++)
                {
                    for (int j = 0; j < dv.ColumnCount; j++)
                    {
                       // myfile += i.ToString() + "\t" + j.ToString() + "\t" + "\t";
                        label1.Text = i.ToString() + "\t" + j.ToString() + "\t" + "\t";
                        if (j + 1 < dv.ColumnCount)
                      {
                          myfile += dv[j, i].Value == null ? "null\t," : "\"" + dv[j, i].Value.ToString() + "\",";
                      }
                      else
                      {

                          myfile += dv[j, i].Value == null ? "null\t" : "\"" + dv[j, i].Value.ToString() + "\"";
                      }
                      
                      
                    
                    }
                    myfile += "\n";
                
                }




                    //write now
                    s_SW.WriteLine(myfile);
                s_SW.Close();
                txt_filename.Enabled = true;  

            }
                
              
            [/codes]

点击下面,查看已改的方法,谢谢csdn的朋友 白荷师傅

[codes=C#]      ///
        /// 把dgv寫到txt中
        ///

        ///
        ///
        private void btn_2txt_Click(object sender, EventArgs e)
        {
            int[] index = new int[this.dataGridView1.Columns.Count];
            int i = 0;
            foreach (DataGridViewColumn column in dataGridView1.Columns)
            {
                index[i] = column.DisplayIndex;
                i++;
            }

            if (txt_filename.Text.Trim() == "")
            {
                lb_showmsg.Text = "文件名為空不能創建";

            }
            else
            {
                txt_filename.Enabled = false;
                lb_showmsg.Text = "文件名:" + txt_filename.Text + ".txt 創建成功";

                StreamWriter s_SW = new StreamWriter(txt_filename.Text + ".txt", true);
                string myfile = "\n";


                for (i = 0; i < dataGridView1.RowCount; i++)
                {
                    for (int j = 0; j < dataGridView1.ColumnCount; j++)
                    {
                        // myfile += i.ToString() + "\t" + j.ToString() + "\t" + "\t";
                        label1.Text = i.ToString() + "\t" + j.ToString() + "\t" + "\t";
                        if (j + 1 < dataGridView1.ColumnCount)
                        {
                            myfile += dataGridView1[index[j], i].Value == null ? "null\t," : "\"" + dataGridView1[index[j], i].Value.ToString() + "\",";
                        }
                        else
                        {

                            myfile += dataGridView1[index[j], i].Value == null ? "null\t" : "\"" + dataGridView1[index[j], i].Value.ToString() + "\"";
                        }
                    }
                    myfile += "\n";

                }
                //write now
                s_SW.WriteLine(myfile);
                s_SW.Close();
                txt_filename.Enabled = true;
            }
        }[/codes]

作者:Else 's Blog
地址:http://www.aixq.com/post/1052/
版权所有。转载时必须链接形式注明作者和原始出处及本声明!
最后编辑: Else 编辑于2008/06/03 17:34
现在 工作 学习 | 评论(0) | 引用(0) | 阅读(496)
发表评论
表情
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
打开HTML
打开UBB
打开表情
隐藏
昵称   密码   游客无需密码
网址   电邮   [注册]