c#中两个列名相同的datatable,如何将他们数据加到一起?
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了c#中两个列名相同的datatable,如何将他们数据加到一起?相关的知识,希望对你有一定的参考价值。
有例子就好了
//原本的两个datatable1和datatable2DataTable 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#中同一个Datatable中的如何把重复的记录合并起来,
c#.net 如何将两个datatable拼接成一个Datatable
c#.net 如何将两个datatable拼接成一个Datatable
C#两个datagridview分别绑定了相同结构的内容不同的DATATABLE,求两个datagridview的横纵滚动条同步滚动