c#中两个列名相同的datatable,如何将他们数据加到一起?

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了c#中两个列名相同的datatable,如何将他们数据加到一起?相关的知识,希望对你有一定的参考价值。

有例子就好了

//原本的两个datatable1和datatable2
DataTable DataTable1, DataTable2
//两个结构一样的DT合并
DataTable newDataTable = DataTable1.Clone();
object[] obj = new object[newDataTable.Columns.Count];
for (int i = 0; i < DataTable1.Rows.Count; i++)

DataTable1.Rows[i].ItemArray.CopyTo(obj,0);
newDataTable.Rows.Add(obj);

for (int i = 0; i < DataTable2.Rows.Count; i++)

DataTable2.Rows[i].ItemArray.CopyTo(obj,0);
newDataTable.Rows.Add(obj);

//newDataTable就是你要的了

参考技术A 你这个问题真混乱, 参考技术B 你是要把两个合起来 还是拼到后面

[将数据导出到CSV时如何获取DataTable列名?

我具有以下代码,可用于将大量数据从DataTable导出到CSV。由于某些原因,DataTable的列未与行一起导出。

我在以下代码中缺少什么吗?获取行时,DataTable列不会自动导出吗?

 private void ExportToCSV()
    {
        try
        {
            string delimiter = ",";
            DataTable dt = new DataTable();
            foreach (DataGridViewColumn col in dgvSearchFilter.Columns)
            {
                dt.Columns.Add(new DataColumn(col.Name, typeof(string)));
            }

            for (int i = 0; i < dgvSearchFilter.RowCount; i++)
            {
                DataRow dr = dt.NewRow();
                for (int j = 0; j < dt.Columns.Count; j++)
                {
                    dr[j] = dgvSearchFilter.Rows[i].Cells[j].Value;
                }
                dt.Rows.Add(dr);
            }

            string unique = DateTime.Now.ToString("yyyyMMddHHmmssffff");
            string fileName = "SQLQueryOutput_" + unique + ".csv";
            using (StreamWriter swr = new StreamWriter(File.Open(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), fileName), FileMode.CreateNew), Encoding.Default, 1000000))
            { 
                foreach (DataRow dr in dt.Rows)
                {
                    swr.WriteLine(string.Join(delimiter, dr.ItemArray));
                }
            }
            MessageBox.Show("Your file was generated and its ready for use.");
}
答案

这样的事情。

    private void ExportToCSV()
    {
        List<string> headers = new List<string>();
        try
        {
            string delimiter = ",";
            DataTable dt = new DataTable();
            foreach (DataGridViewColumn col in dgvSearchFilter.Columns)
            {
                dt.Columns.Add(new DataColumn(col.Name, typeof(string)));
                headers.Add(col.Name);
            }

            for (int i = 0; i < dgvSearchFilter.RowCount; i++)
            {
                DataRow dr = dt.NewRow();
                for (int j = 0; j < dt.Columns.Count; j++)
                {
                    dr[j] = dgvSearchFilter.Rows[i].Cells[j].Value;
                }
                dt.Rows.Add(dr);
            }

            string unique = DateTime.Now.ToString("yyyyMMddHHmmssffff");
            string fileName = "SQLQueryOutput_" + unique + ".csv";
            using (StreamWriter swr = new StreamWriter(File.Open(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), fileName), FileMode.CreateNew), Encoding.Default, 1000000))
            { 
                swr.WriteLine(string.Join(delimiter, headers));
                foreach (DataRow dr in dt.Rows)
                {
                    swr.WriteLine(string.Join(delimiter, dr.ItemArray));
                }
            }
            MessageBox.Show("Your file was generated and its ready for use.");
}
另一答案
private void ExportToCSV()
{
    string delimiter = ",";

    var columns = dgSearchFilter.Columns.Select(c => c.Name.Cointains(deliminter)?""" + c.Name + """: c.Name);
    string header = string.Join(delimiter, columns);

    var unique =  DateTime.Now.ToString("yyyyMMddHHmmssffff");
    using (var swr = new StreamWriter($"SQLQueryOutput_{unique}.csv"))
    {
        swr.WriteLine(header);

        var rows = dgvSearchFilter.Rows.Select(r => r.Cells.Select(c => c.Value.ToString()));
        foreach (var row in rows)
        { 
             var line = row.Select(r => r.Contains(delimiter) || r.Contains("
")?""" + r + """:r);
             swr.WriteLine(string.Join(delimiter, line));
        }
    }

    MessageBox.Show("Your file was generated and its ready for use.");
}

以上是关于c#中两个列名相同的datatable,如何将他们数据加到一起?的主要内容,如果未能解决你的问题,请参考以下文章

来自具有相同列名的两个表的数据

如何绘制来自具有相同列名的两个数据框的数据

在 C# 中比较两个数据表的列名

如何匹配两个数据框的架构

如何查询多个表以获取表具有相同列名的记录

如何在 MS Access 中使用 C# 获取所有表名和列名?