0
我的DataTable行像40行的DataGridView
顯示我很困惑,爲什麼我的方法只保存One Row
在TextFile
:C#寫上我的<code>DataTable</code>在TXT文件
private void SaveBtn_Click(object sender, EventArgs e)
{
String outputFile;
List<String> ListData = new List<String>();
using (SaveFileDialog sfd = new SaveFileDialog())
{
sfd.Filter = "Txt File|*.Txt";
if (sfd.ShowDialog() != DialogResult.OK)
return;
outputFile = sfd.FileName;
}
DataTable tb = pw.SavedInfo(User_info.UserID);
for (int i = 0; i < tb.Rows.Count; i++)
{
ListData.Add("Name==> " + tb.Rows[i][1].ToString() + " LastName ==> " + tb.Rows[i][2].ToString() + " Email ==> " + tb.Rows[i][3].ToString());
}
foreach (String s in ListData)
{
using (TextWriter Tw = new StreamWriter(outputFile))
{
Tw.WriteLine(s);
}
}
}
難道我錯過了什麼?造成這是一個非常漫長的一天,以保持被聚焦
我v先使用Foreach然後TextWriter我不得不在循環之前使用textwriter,但是謝謝先生! –
'File.WriteAllLines(...)'這個重載需要'IEnumerable'所以你不需要'foreach'或'StreamWriter' https://msdn.microsoft.com/en-us/library /dd383693(v=vs.110).aspx –