c# - 将 DataGrid 导出到文本文件

标签 c# sql wpf text-files

我是编程新手(在大学学习的第一年),我正在开发一个小型应用程序。

我有一个窗口,用户可以在其中将数据从 SQL 检索到 DataGrid,还有一个 Button 用于将一些数据从 DataGrid 数据导出到文本文件。

这是我用来从 SQL 获取数据的代码:

SqlConnection con = new SqlConnection("Server = localhost;Database = autoser; Integrated Security = true");
SqlCommand cmd = new SqlCommand("selectproduct", con); // Using a Store Procedure.
cmd.CommandType = CommandType.StoredProcedure;
DataTable dt = new DataTable("dtList");
cmd.Parameters.AddWithValue("@Code", txtbarcode.Text);

SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(dt);
data.ItemsSource = dt.DefaultView;
SqlDataAdapter adapt = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
adapt.Fill(ds);
con.Close();
int count = ds.Tables[0].Rows.Count;
if (count == 0)
{
    MessageBox.Show("This product doesn't excist");
    SystemSounds.Hand.Play();
}
else if (count == 1)
{
    lblinfo.Visibility = Visibility.Visible;
    SystemSounds.Asterisk.Play();

}

这是我用来编写文本文件的代码:

{
    using (StreamWriter writer = new StreamWriter("D:\\test.txt", true))
    {
        writer.WriteLine("Welcome");
        writer.WriteLine("E N T E R N E T");
    }

    using (StreamWriter writer = new StreamWriter("D:\\test.txt", true))
    {
        writer.WriteLine(data.Items);
    }


    using (StreamWriter writer = new StreamWriter("D:\\test.txt", true))
    {
        writer.WriteLine(data.Items);
    }
    // Append line to the file.
    using (StreamWriter writer = new StreamWriter("D:\\test.txt", true))
    {
        writer.WriteLine("---------------------------------------");
        writer.WriteLine("             Thank You!   ");
        writer.WriteLine("        " + DateTime.Now + "              ");
    }
}

当我打开文本文件时,我得到了这些数据

Welcome
E N T E R N E T
System.Windows.Controls.ItemCollection -   Why isn't show the data grid 
data

---------------------------------------
    Thank You   
    7/26/2018 12:38:37 PM   

我的问题是:我的错误在哪里导致 DataGrid 中的数据没有以正确的方式显示?
提前致谢

最佳答案

您目前正在使用 WriteLine 方法的以下重载:

public virtual void WriteLine(object value)

如果您查看 StreamWriter.WriteLine(object) 的文档它说:

Writes the text representation of an object by calling the ToString method on that object, followed by a line terminator to the text string or stream.

这就是为什么您在文件中得到以下漂亮行的原因:

System.Windows.Controls.ItemCollection

Object.ToString() 的文档方法揭示了

default implementations of the Object.ToString method return the fully qualified name of the object's type.

您需要遍历集合并将每个条目分别写入文件。我还建议直接使用数据源,而不是从 DataGrid 写入。

foreach (DataRow row in dt.Rows)
{  
    object[] array = row.ItemArray;
    writer.WriteLine(string.Join(" | ", array));        
}

关于c# - 将 DataGrid 导出到文本文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51536927/

相关文章:

mysql - SELECT WITH LEFT JOIN 和 WHERE 子句

wpf - 网格单元内堆栈面板上的边框

c# - SharpPcap 问题

sqlite查询对子查询的结果进行简单的计算

c# - Azure函数返回已弃用的API header

java - MySQL 语法错误 |您的 SQL 语法有错误;

c# - Caliburn Micro 中 TextBox 的 GotFocus 事件?

WPF - 绑定(bind)到用户控件之间列表框的选定项

c# - 在 C# 中的每个派生类中都有一个更具体的接口(interface)

c# - 如何在完成之前访问 DirectoryInfo.EnumerateFiles