[英]Join two DataTable one from excel data and other from text file to display it in datagridview in c#
[英]C# datagridview didnt show full data from text file after merge two text file together
将两个文本文件合并在一起并尝试在datagridview中读取合并的文件后,我的确意识到我的datagridview没有显示文本文件的完整数据。
这是我调试后的datagridview。 在此处输入图片说明
这是合并后的文本文件格式。(由于编码尚不完善,我无法按顺序合并文件)在此处输入图片描述
这是我的代码。
private bool loadTxt()
{
// try locate the txt file, exist or not
try
{
StreamReader rrr = new StreamReader("merge.txt");
rrr.Close();
}
catch (FileNotFoundException ex)
{
return false;
}
// read all line from txt file
var file = File.ReadAllLines("merge.txt");
string DateTag = "<Date>";
string NIDTag = "<News ID>";
string StockIDTag = "<StockID>";
string LatestPriceTag = "<Latest Price>";
string TargetPriceTag = "<Target Price>";
string StockComTag = "<StockCompany>";
string Date = "";
string NID = "";
string StockID = "";
string LatestPrice = "";
string TargetPrice = "";
string StockCom = "";
int row = dataGridView2.Rows.Count - 1;
// Loop through the file and find targeted data to store in datagridview1
for (int i = 0; i < file.Length - 1; i++)
{
if (file[i] == DateTag)
{
dataGridView2.Rows.Add();
Date = file[i + 1];
dataGridView2.Rows[row].Cells[0].Value = Date;
}
if (file[i] == NIDTag)
{
NID = file[i + 1];
dataGridView2.Rows[row].Cells[1].Value = NID;
}
if (file[i] == StockIDTag)
{
StockID = file[i + 1];
dataGridView2.Rows[row].Cells[2].Value = StockID;
}
if (file[i] == LatestPriceTag)
{
LatestPrice = file[i + 1];
dataGridView2.Rows[row].Cells[3].Value = LatestPrice;
}
if (file[i] == TargetPriceTag)
{
TargetPrice = file[i + 1];
dataGridView2.Rows[row].Cells[4].Value = TargetPrice;
}
if (file[i] == StockComTag)
{
StockCom = file[i + 1];
dataGridView2.Rows[row].Cells[5].Value = StockCom;
row++;
}
}
return true;
}
谢谢。
我认为您正在将数据添加到同一行
int row = dataGridView2.Rows.Count - 1; //your row
并使用row
每个值添加到dataGridView2
if (file[i] == DateTag)
{
dataGridView2.Rows.Add();
Date = file[i + 1];
dataGridView2.Rows[row].Cells[0].Value = Date; //same [row]
}
希望能帮助到你 !!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.