如何在文本文件中写入datagrindview中的内容 [英] How to write in a text file what's in the datagrindview

查看:249
本文介绍了如何在文本文件中写入datagrindview中的内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何在文本修复程序中写入在datagrindview中写入的数据.

这是我用来在datagrindview中编写的代码:

I would like to know how I can write in a text fixer the data that is written in my datagrindview.

This is the code I use to write in my datagrindview:

string connString = @"Data source = teste1234 ; Database=1234 ; User Id=test ; Password=test123";

            using (SqlConnection sqlConn = new SqlConnection(connString))
            {
                string sqlQuery = @"SELECT * from Zone";
                SqlCommand cmd = new SqlCommand(sqlQuery, sqlConn);
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                DataTable table = new DataTable();
                da.Fill(table);
                dataGridView1.DataSource = new BindingSource(table, null);
            }



我尝试过的事情:

我尝试过了,但是没有用.



What I have tried:

I tryed this but it dont work.

public void WriteToTextFile (DataGridView dgv)
        {
            string file_name = "D:\\test1.txt";

        System.IO.StreamWriter objWriter;

        objWriter = new System.IO.StreamWriter(file_name);

        int count = dgv.Rows.Count;
        for (int row = 0; row < count-1; row++)
        {
            int colCount = dgv.Rows[row].Cells.Count; 

            for ( int col = 0; col < colCount-1; col++)  
            {
                objWriter.WriteLine(dgv.Rows[row].Cells[col].Value.ToString());
            }

        }
        objWriter.Close();
        }

推荐答案

您的WriteToTextFile()方法有效,没有问题!
因此,肯定还有其他问题,您尝试调试了吗?
Your WriteToTextFile() method works, nothing wrong with it !
So there must be something else going wrong, did you try debugging ?


它不起作用"不是有用的错误消息,它告诉我们任何我们不知道的信息-这是您有一些描述问题...
在这里尝试您的代码,我从DataGridView中获取了一个充满数据的文件.因此,问题可能出在DGV中的数据,还是您要写入的文件或路径有问题-我们都无法访问这两个文件.

因此,取决于您.
在函数的第一行上放置一个断点,然后通过调试器运行代码.然后查看您的代码和数据,并确定应该手动执行的操作.然后,单行检查每一行,以确保您期望发生的事情确实是正确的.如果不是,那就是您遇到问题时,可以回溯(或再次运行并仔细查看)以找出原因.使用获得的错误消息(如果有)作为缩小"问题范围的指南.

抱歉,但是我们不能为您做到这一点-是时候让您学习一种新的(也是非常非常有用的)技能:调试!
"It dont work" is not a helpful error message, it tells us nothing we don''t know - which is that you have a problem of some description...
Trying your code works here, I get a file full of data from my DataGridView. So the problem is either the data in your DGV, or a problem with the file or path you are trying to write to - neither of which we have any access to.

So, its going to be up to you.
Put a breakpoint on the first line in the function, and run your code through the debugger. Then look at your code, and at your data and work out what should happen manually. Then single step each line checking that what you expected to happen is exactly what did. When it isn''t, that''s when you have a problem, and you can back-track (or run it again and look more closely) to find out why. Use the error message that you get (if any) as a guide to "narrow down" what the problem might be.

Sorry, but we can''t do that for you - time for you to learn a new (and very, very useful) skill: debugging!


这篇关于如何在文本文件中写入datagrindview中的内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆