我正在从excel文件(.csv)中读取数据并将该数据存储到List<String>
。我想将此数据显示给DataGridView
,但由于某种原因,我看到length of String
中的Grid
总数。
以下是我的代码:
CODE:
private void btnImportFile_Click(object sender, EventArgs e)
{
OpenFileDialog dialog = new OpenFileDialog();
dialog.Filter = "CSV (Comma delimited) (*.csv)|*.csv";
dialog.FilterIndex = 1;
dialog.Multiselect = false;
var userClickedOK = dialog.ShowDialog();
if (userClickedOK.ToString().Equals("OK"))
{
this.lblImportedFileName.Text = dialog.SafeFileName;
System.IO.Stream fileStream = dialog.OpenFile();
using (System.IO.StreamReader reader = new System.IO.StreamReader(fileStream))
{
var list = new List<String>();
var line = String.Empty;
while ((line = reader.ReadLine()) != null) //Reading value and adding it to the list.
{
list.Add(line);
}
BindingList<String> view = new BindingList<String>(list);
this.gridExcelResults.DataSource = view; // Binding list to grid
//Also tried
this.gridExcelResults.DataSource = list; // This also gives me same result.
}
}
}
输出:
EXCEL DATA:
预期结果:
我只想在Grid
上显示这三个字段。我在这里做错了什么?
答案 0 :(得分:3)
看到这个问题: How to bind a List<string> to a DataGridView control?
在您的情况下,您使用的是BindingList<string>
,但逻辑是相同的。 DataGridView正在寻找要绑定的对象属性,String
但是,只有属性长度。