在 ASP.NET 中将 DataTable 导出到 .CSV 文件时,对 DataTable 的更改不会保留

Changes to DataTable not persisting when exporting DataTable to .CSV file in ASP.NET

以下代码仅发送我的 DataTable 在 .csv 中的最后一行 file.I 在这里看不到我哪里错了,我是新手,请帮助我。

DataTable dt22 = new DataTable(); 
dt22.Columns.Add("Tasks");
DataRow dr = dt22.NewRow(); 
dr[0] = TextBox2.Text;

dt22.Rows.Add(dr);

GridView1.DataSource = dt22; 
GridView1.DataBind();

Session["Data"] = dt22;

StringBuilder sb = new StringBuilder();

IEnumerable<string> columnNames = dt22.Columns.Cast<DataColumn>().
                                  Select(column => column.ColumnName);
sb.AppendLine(string.Join(",", columnNames));

foreach (DataRow row in dt22.Rows)
{
    IEnumerable<string> fields = row.ItemArray.Select(field => field.ToString());
    sb.AppendLine(string.Join(",", fields));
}

File.WriteAllText(@"c:\Tasks.csv", sb.ToString());

更新:

很难准确理解这个问题,但我认为这是 post-backs 之间的数据持久性。当用户向网格中添加一行时,您是否正在更新基础数据源,然后继续使用该数据源创建 CSV?

看起来您正在尝试使用 Session 来持久化数据,您是否尝试过这样的操作:

protected void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
        InitialDataLoad();
}

private void InitialDataLoad()
{
    var dt = new DataTable();
    dt.Columns.Add("Tasks");
    dt.Columns.Add("AnotherColumn");

    var dr = dt.NewRow();
    dr[0] = "task1";
    dr[1] = "c1";
    dt.Rows.Add(dr);

    GridView1.DataSource = dt;
    GridView1.DataBind();

    Session["dt"] = dt;
}

protected void Button1_Click(object sender, EventArgs e)
{
    // adds a row

    var dt = Session["dt"] as DataTable;

    if (dt != null)
    {
        var dr = dt.NewRow();
        dr[0] = "anothertask";
        dr[1] = "c2";
        dt.Rows.Add(dr);

        GridView1.DataSource = dt;
        GridView1.DataBind();

        Session["dt"] = dt;
    }
}

protected void Button2_Click(object sender, EventArgs e)
{
    // create csv

    var dt = Session["dt"] as DataTable;

    var sb = new StringBuilder();

    IEnumerable<string> columnNames = dt.Columns.Cast<DataColumn>().
                                      Select(column => column.ColumnName);

    sb.AppendLine(string.Join(",", columnNames));

    foreach (DataRow row in dt.Rows)
    {
        IEnumerable<string> fields = row.ItemArray.Select(field => field.ToString());
        sb.AppendLine(string.Join(",", fields));
    }

    File.WriteAllText(@"c:\Tasks.csv", sb.ToString());
}

较早的回答:

您的代码的最后一个循环能够打印的不仅仅是最后一行,所以我看不出有什么问题。问题可能与您在 dt22 中的预期有关。看起来您只是向代码顶部的 dt22 DataTable 添加了一行数据,那么您期望有多少行?

这是一个使用相同代码的更多数据示例:

DataTable dt22 = new DataTable();
dt22.Columns.Add("Tasks");
dt22.Columns.Add("AnotherColumn");

DataRow dr = dt22.NewRow();
dr[0] = "task1";
dr[1] = "c1";
dt22.Rows.Add(dr);

DataRow dr2 = dt22.NewRow();
dr2[0] = "test2";
dr2[1] = "c2";
dt22.Rows.Add(dr2);

StringBuilder sb = new StringBuilder();

IEnumerable<string> columnNames = dt22.Columns.Cast<DataColumn>().
                                  Select(column => column.ColumnName);
sb.AppendLine(string.Join(",", columnNames));

foreach (DataRow row in dt22.Rows)
{
    IEnumerable<string> fields = row.ItemArray.Select(field => field.ToString());
    sb.AppendLine(string.Join(",", fields));
}

File.WriteAllText(@"c:\Tasks.csv", sb.ToString());

打印:

Tasks,AnotherColumn
task1,c1
test2,c2