本文介绍了如何在文本文件中写入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();
}
推荐答案
这篇关于如何在文本文件中写入datagrindview中的内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!