Reputation: 452
As checking some article about this, I find at the reference below load .txt file into gridview c# . But it seems not the same which I want to do, transfer data from row to column. I have a text file like this (about 6 rows)
1010
Apple
Fruit
15
$10
Gala, Green
I will binding it to gridview with the following six columns: ID, Name, Category, Qty, Price, Remark
.
I tried the below code:
System.IO.FileInfo fi = new System.IO.FileInfo("file_data.txt");
using (StreamReader sr = new StreamReader(file))
{
string line;
while ((line = sr.ReadLine()) != null)
{
if (line != "")
{
for (int i = 0; i < dgv_data.ColumnCount; i++)
{
dgv_data.Columns[i].ToString() = line;
}
}
}
}
Any advice for this and what should I do for better results.
Upvotes: 1
Views: 995
Reputation: 42483
You need to build a datasource that you can assign to the DataGridView.Datasource property.
Let's create a class for Fruit.
class Fruit
{
public string ID {get;set;}
public string Name {get;set;}
public string Category {get;set;}
public string Qty {get;set;}
public string Price {get;set;}
public string Remark {get;set;}
}
Then create a method that reads lines from any textreader, keep track of the read state and assign the line to the correct property of the current Fruit instance. Store each instance in a list of Fruit.
List<Fruit> GetFruits(TextReader reader)
{
string line;
var rowCount = 0;
Fruit currentFruit = null;
var fruits = new List<Fruit>();
while ((line = reader.ReadLine()) != null)
{
switch(rowCount)
{
case 0:
// create a new Fruit
currentFruit = new Fruit();
// keep this fruit
fruits.Add(currentFruit);
// populate our currentFruit instance
currentFruit.ID = line;
break;
case 1:
currentFruit.Name = line;
break;
case 2:
currentFruit.Category = line;
break;
case 3:
currentFruit.Qty = line;
break;
case 4:
currentFruit.Price = line;
break;
case 5:
currentFruit.Remark = line;
// reset state
rowCount = -1;
break;
}
rowCount++;
}
return fruits;
}
Putting it together:
using (StreamReader sr = new StreamReader("file_data.txt"))
{
dgv_data.DataSource = GetFruits(sr);
}
Upvotes: 2
Reputation: 16129
System.IO.FileInfo fi = new System.IO.FileInfo("file_data.txt");
using (StreamReader sr = new StreamReader(file))
{
string line;
while ((line = sr.ReadLine()) != null) // you are looping the lines ...
{
if (line != "")
{
for (int i = 0; i < dgv_data.ColumnCount; i++) // and set _each col_ to that line value!
// => makes no sense!
{
dgv_data.Columns[i].ToString() = line; // .ToString() makes no sense here
}
}
}
}
So, you could make this simpler and actually work:
var lines = File.ReadAllLines("file_data.txt");
// if you are going async:
// var lines = await File.ReadAllLinesAsync("file_data.txt");
for( int i = 0; i < Math.Min(lines.Length, dgv_data.ColumnCount); i++ )
{
if(string.IsNullOrWhiteSpace(line[i])) continue; // Skips empty lines, doesn't set their column.
dgv_data.Columns[i] = line[i];
}
For reference:
Outside of that, I'd like to recommend using some proper serialization format like XML, JSON, CSV, maybe and a respective model class.
Mind: I left dgv_data.Columns[i] = line[i];
like this, because it is unclear what type it is.
If this is a DataRow
, then the line should probably be dgv_data.Cells[i].Value = line[i];
.
If it is something else, you might want to drop a comment and I'll fix the answer.
Upvotes: 1