C# 创建多个实例并设置特性值
我正在访问一个.CSV文件并转换要在DataGrid中显示的值,我的问题是我只有一个定义的实例,它过去在循环之外,我没有设置它的属性 如何在循环中创建多个实例并设置属性值 CSV格式为: “标题” 'string,string,int,string'(最多300个条目) 以下是从单击菜单->打开调用的主代码:C# 创建多个实例并设置特性值,c#,list,datagridview,instantiation,C#,List,Datagridview,Instantiation,我正在访问一个.CSV文件并转换要在DataGrid中显示的值,我的问题是我只有一个定义的实例,它过去在循环之外,我没有设置它的属性 如何在循环中创建多个实例并设置属性值 CSV格式为: “标题” 'string,string,int,string'(最多300个条目) 以下是从单击菜单->打开调用的主代码: // Function to open a .CSV file and assign the values within to a List. private vo
// Function to open a .CSV file and assign the values within to a List.
private void openToolStripMenuItem_Click(object sender, EventArgs e)
{
List<Stock> stocks = new List<Stock>();
// Set strings to retrieve current directory and store stocklist.csv as "filename"
string filename = @"C:\StockFile\stocklist.csv";
// Read all lines in file and set indexer to 0.
string[] linesInFile = File.ReadAllLines(filename);
string[] StockDetails = new string[0];
for (int i = 1; i < linesInFile.Length; i++)
{
// Load lines in file and increment by 1.
string currentLine1 = linesInFile[i];
// Split the current line by separator ,
StockDetails = currentLine1.Split(',');
Stock item = new Stock();
stocks.Add(item);
}
var list = new BindingList<Stock>(stocks);
stockGridView.DataSource = list;
}
如果我理解您试图做什么,您需要初始化Stock item的实例,并且每次循环中new关键字都会创建一个新实例,并将创建的实例添加到列表中
private void openToolStripMenuItem_Click(object sender, EventArgs e)
{
List<Stock> stocks = new List<Stock>();
// Set strings to retrieve current directory and store stocklist.csv as "filename"
string filename = @"C:\StockFile\stocklist.csv";
// Read all lines in file and set indexer to 0.
string[] linesInFile = File.ReadAllLines(filename);
string[] StockDetails = new string[0];
for (int i = 1; i < linesInFile.Length; i++)
{
// Load lines in file and increment by 1.
string currentLine1 = linesInFile[i];
// Split the current line by separator ,
StockDetails = currentLine1.Split(',');
Stock item = new Stock(StockDetails[0],StockDetails[1],StockDetails[2],StockDetails[3]);
stocks.Add(item);
}
var list = new BindingList<Stock>(stocks);
stockGridView.DataSource = list;
}
private void openToolStripMenuItem\u单击(对象发送方,事件参数e)
{
上市股票=新上市();
//设置字符串以检索当前目录并将stocklist.csv存储为“文件名”
字符串文件名=@“C:\StockFile\stocklist.csv”;
//读取文件中的所有行并将indexer设置为0。
string[]linesInFile=File.ReadAllLines(文件名);
字符串[]StockDetails=新字符串[0];
对于(int i=1;i
如果我理解您正在尝试做什么,您需要初始化Stock item的实例,并且每次循环中new关键字都会创建一个新实例,并将创建的实例添加到列表中
private void openToolStripMenuItem_Click(object sender, EventArgs e)
{
List<Stock> stocks = new List<Stock>();
// Set strings to retrieve current directory and store stocklist.csv as "filename"
string filename = @"C:\StockFile\stocklist.csv";
// Read all lines in file and set indexer to 0.
string[] linesInFile = File.ReadAllLines(filename);
string[] StockDetails = new string[0];
for (int i = 1; i < linesInFile.Length; i++)
{
// Load lines in file and increment by 1.
string currentLine1 = linesInFile[i];
// Split the current line by separator ,
StockDetails = currentLine1.Split(',');
Stock item = new Stock(StockDetails[0],StockDetails[1],StockDetails[2],StockDetails[3]);
stocks.Add(item);
}
var list = new BindingList<Stock>(stocks);
stockGridView.DataSource = list;
}
private void openToolStripMenuItem\u单击(对象发送方,事件参数e)
{
上市股票=新上市();
//设置字符串以检索当前目录并将stocklist.csv存储为“文件名”
字符串文件名=@“C:\StockFile\stocklist.csv”;
//读取文件中的所有行并将indexer设置为0。
string[]linesInFile=File.ReadAllLines(文件名);
字符串[]StockDetails=新字符串[0];
对于(int i=1;i
太好了,谢谢你,我错过的东西现在非常明显,我很高兴我可以说我只是这方面的新手。哈哈。数据网格现在从列表中填充得非常完美(在将列名调整为不仅仅是占位符之后)。非常完美,感谢这一点,我缺少的东西现在非常明显,我很高兴我可以说我只是这方面的新手。哈哈。数据网格现在完全从列表中填充(在将列名调整为不仅仅是占位符之后)。